Add Some Fancy to the Edit Page/Post Link

Inspired by this tweet.

Add a little bit of fancy to your edit link in the Admin Bar with this:

add_action( 'wp_before_admin_bar_render', 'adminbar_tweaks');

function adminbar_tweaks() {
    global $wp_admin_bar;

    if (isset( $wp_admin_bar->menu->edit['title'] ) ) {
        $title = $wp_admin_bar->menu->edit['title'];
        if ( substr( $title, 0, 5 ) == 'Edit ' ) {
            $hand = '<span style="font-size:20px;margin:-1px 3px 0 0;float:left;">✍</span>';
            $wp_admin_bar->menu->edit['title'] = $hand.$title;
        }
    }

}

Move Privacy Notice in 3.2

If you’ve already started poking around in WordPress 3.2, you may have noticed that the privacy notice that used to appear in the WordPress header has been moved to the Right Now box on the dashboard.

And if you’re like me, you probably wish it was back at the top. Fortunately, with just a few lines, we can do that:

add_action('in_admin_header', 'privacy_notice_in_header');
function privacy_notice_in_header() {
    if (1 != get_option('blog_public')) {
        $title = apply_filters('privacy_on_link_title', __('Your site is asking search engines not to index its content') );
        $content = apply_filters('privacy_on_link_text', __('Search Engines Blocked') );
        echo "<p class='alignleft' style='padding:9px 0 0;margin:0;'><a href='options-privacy.php' title='$title'>$content</a></p>";
    }
}

Redirect When Search Query Only Returns One Match

Be careful when using this, as users may not expect this functionality.

If there’s only one match for a particular search query, you can save users the hassle of clicking by simply redirecting them to the page/post.

add_action('template_redirect', 'single_result');
function single_result() {
	if (is_search()) {
		global $wp_query;
		if ( $wp_query->post_count == 1 && $wp_query->max_num_pages == 1 ) {
			wp_redirect( get_permalink( $wp_query->posts[0]->ID ) );
			exit;
		}
	}
}

Redirect When Search Query is an Exact Title Match

You probably shouldn’t use this if you have really generic page/post titles, but it can be a really handy feature.

Basically, if a user searches for something that happens to be an exact match to a page or post you have, it’ll redirect the user to the page, rather than displaying search results.

add_action('template_redirect', 'seach_query_is_title');
function seach_query_is_title() {
	if (is_search()) {
		global $wp_query;
		if ( get_page_by_title( get_search_query(), 'OBJECT', 'post' ) ) {
			wp_redirect( get_permalink( get_page_by_title (get_search_query() )->ID ) );
		}
		elseif ( get_page_by_title( get_search_query(), 'OBJECT', 'page' ) ) {
			wp_redirect( get_permalink( get_page_by_title( get_search_query() )->ID ) );
		}
	}
}

Sort “My Sites” Menu Alphabetically

If you have a WordPress Network and you use the My Sites menu a lot, you might wish the list was sorted alphabetically, rather than by site ID.

Here’s how you can reorder that list

Method 1: Modify the Admin Bar/Toolbar directly

add_action('admin_bar_menu', 'reorder_my_sites');
function reorder_my_sites( $wp_admin_bar ) {

    //get "my sites"
    $mysites = $wp_admin_bar->user->{'blogs'};
    //and remove the unsorted list
    unset( $wp_admin_bar->user->{'blogs'} );

    //loop thru mysites to get id and title
    $pairs = array();
    foreach($mysites as $id => $info) {
        $pairs[$id] = strip_tags($info->blogname);
    }
    $pairs = array_map( 'strtoupper', $pairs );
    //sort by title, keep id
    asort($pairs);

    //loop thru sorted sites, and put back in menu
    foreach ($pairs as $id => $title) {
        $wp_admin_bar->user->{'blogs'}->$id = $mysites[$id];
    }
}

Method 2: Reorder the user’s blogs, before the menu is rendered

add_filter('get_blogs_of_user','reorder_users_sites');
function reorder_users_sites( $blogs ) {
	$f = create_function('$a,$b','return strcasecmp($a->blogname, $b->blogname);');
	uasort( $blogs, $f );
	return $blogs;
}

Method 2 credit

 

Add More to User Row Actions

Need to quickly see more than the default information on the Users page? Here’s how to get and display each users website:

add_filter( 'user_row_actions', 'user_row_actions_show_website', 10, 2 );
function user_row_actions_show_website( $actions, $user_object ) {
	$actions['website'] = make_clickable( $user_object->user_url );
	return $actions;
}

Add Custom Post Type Totals to the “Right Now” Box

There’s a pretty convenient “at a glance” widget in the Dashboard called Right Now. But if you’re using any custom post types, you may have noticed they won’t show up there.

Naturally, we can fix that:

add_action( 'right_now_content_table_end', 'post_type_totals_rightnow' );
function post_type_totals_rightnow() {    
    $post_types = get_post_types( array( '_builtin' => false ), 'objects' ); 
    if (count($post_types) > 0)
    foreach( $post_types as $pt => $args ) {
        $url = 'edit.php?post_type='.$pt;
        echo '<tr><td class="b"><a href="'. $url .'">'. wp_count_posts( $pt )->publish .'</a></td><td class="t"><a href="'. $url .'">'. $args->labels->name .'</a></td></tr>';
    }
}

Upload to the Media Library From Your Plugin

There are a variety of reasons you may want or need to include an upload form inside your theme or plugin. One thing that bugs me, though, is when the image (or whatever) that I’ve uploaded isn’t available in the Media Library. I don’t want to have to reupload an image just so I can reuse it in a blog post.

The solution is pretty easy – let WordPress do the dirty work for you.

<?php
if ( isset( $_FILES[ 'some-file' ][ 'name' ] ) && !empty( $_FILES[ 'some-file' ][ 'name' ] ) ) {
    $upl_id = media_handle_upload( 'some-file', 0 );
    if (is_wp_error( $upl_id )) {
        echo '<div class="error"><p>'. $upl_id->errors['upload_error']['0'] .'</p></div>';
    }
    else {
        echo '<div class="updated"><p>"'. get_the_title( $upl_id ) .'" was uploaded!';
        echo wp_attachment_is_image( $upl_id ) ? '<br />' . wp_get_attachment_image( $upl_id ) : '';
        echo '</p></div>';
    }
}
?>
<form action="" method="post" enctype="multipart/form-data">
<p><input type="file" class="button" name="some-file" id="some-file" />
<input type="submit" class="button-primary" value="Upload"/>
<input type="hidden" name="submitted-some-file" /></p>
</form>

This is a very simple example, of course. Go play with it