Removed Categories Dropdown Filter

On the All Posts page, there are a series of filters for narrowing down what posts are displayed.

To remove the categories filter, use this:

add_action( 'load-edit.php', 'no_category_dropdown' );
function no_category_dropdown() {
	add_filter( 'wp_dropdown_cats', '__return_false' );
}

In the action hook, change ‘edit.php’ if you want to remove the dropdown on other pages.

Default All Posts to the Current User’s Posts

When clicking on All Posts in the main menu, Administrators and Editors will normall see all posts. Use this to change the default view to the user’s own posts. See comments to limit the change to users of a certain roll.

add_action( 'admin_menu', 'show_users_posts_by_default' );
function show_users_posts_by_default() {

	/*if current user is an 'administrator' do nothing*/
	//if ( current_user_can( 'add_users' ) ) return;

	/*if current user is an 'administrator' or 'editor' do nothing*/
	//if ( current_user_can( 'edit_others_pages' ) ) return;

	global $submenu, $user_ID;
	$submenu['edit.php'][5][2] = 'edit.php?author='. $user_ID;
}

Require Additional Profile Fields at Registration

<?php

add_action( 'register_form', 'additional_profile_fields' );
function additional_profile_fields() { ?>
    <p>
        <label><?php _e('First Name') ?><br />
        <input type="text" name="first_name" id="first_name" class="input" size="25" tabindex="20" /></label>
    </p>
    <p>
        <label><?php _e('Last Name') ?><br />
        <input type="text" name="last_name" id="last_name" class="input" size="25" tabindex="20" /></label>
    </p>
<?php }
 
add_action( 'register_post', 'add_register_field_validate_first_name', 10, 3 );
function add_register_field_validate_first_name( $sanitized_user_login, $user_email, $errors) {
    if (!isset($_POST[ 'first_name' ]) || empty($_POST[ 'first_name' ])) {
    	return $errors->add( 'firstnameempty', '<strong>ERROR</strong>: Please provide a first name.' );
    }    
}
add_action( 'register_post', 'add_register_field_validate_last_name', 10, 3 );
function add_register_field_validate_last_name( $sanitized_user_login, $user_email, $errors) {
    if (!isset($_POST[ 'last_name' ]) || empty($_POST[ 'last_name' ])) {
    	return $errors->add( 'lastnameempty', '<strong>ERROR</strong>: Please provide a last name.' );
    }    
}

add_action( 'user_register', 'insert_register_fields' );
function insert_register_fields( $user_id ) {
	
	$first_name = apply_filters('pre_user_first_name', $_POST['first_name']);
	$last_name = apply_filters('pre_user_last_name', $_POST['last_name']);
	
	update_user_meta( $user_id, 'first_name', $first_name );
	update_user_meta( $user_id, 'last_name', $last_name );
}

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 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>';
    }
}