HOWTO: Add Page Slug to Body Class in a WordPress Theme

Even though the body_class() function in WordPress outputs a good deal of classes you can easy use – there are sometimes when having the actual page name as a class is helpful (custom CSS).

Usage Example:

  • Page Named: My Test Page
  • You need the ability to apply some CSS to something only on this page

In your functions.php file – add the following:

/* 
 * Add in page nicename to body class
 * @param $classes = array of class names or a string with space-separated classes names
 */
function sdac_body_page_name_class( $classes ) {
	global $post;
		$classes[] = $post->post_name;
	return $classes;
}
add_filter('body_class', 'sdac_body_page_name_class');

Once that function is in place you will then see this on the test page as a class in the body tag: “my-test-page”. With that in place you can then use something like this to make a page specific change in CSS:

.my-test-page .entry {margin:50px 0 100px 0;} 

HOWTO: Set Custom Default Gravatar in WordPress

If you ever wanted to set your own custom avatar as the default for Gravatar on your WordPress site – here is a simple way to do it. All you need to do is make your custom avatar accessible somewhere on the web by either uploading it to your site or another site, then add this code to your functions.php file in your active theme:

add_filter( 'avatar_defaults', 'sdac_custom_gravatar' );
function sdac_custom_gravatar ( $avatar_defaults ) {
     $custom_gravatar = 'http://media.sandboxdev.com/wp-content/themes/SDACINC2012/images/logo-small.png';
     $avatar_defaults[$custom_gravatar] = "SDAC Inc. Logo";
     return $avatar_defaults;
}

You will then see it in the list of gravatars in Settings > Discussion on your site as well.

This is quick and painless way to help brand your site.

HOWTO: Show Admin Bar to Admin Users Only

Since WordPress 3.1 – after you log in to a WordPress site – you now see a dark grey bar at the top. If you only want users of a certain role to see it (admins vs. subscribers) you can easily set that up by adding the following code to your functions.php file:

if (!current_user_can('manage_options')) {
	add_filter('show_admin_bar', '__return_false');
}

You can also only show the bar to editors, authors, etc by changing the first line. You can learn more about roles/capabilities: http://codex.wordpress.org/Roles_and_Capabilities

Easily Add Page Excerpts in WordPress

Ever need to add an excerpt field for WordPress when working with pages? There is an easy, one line way to do this (no plugin needed). Simply add this to your theme’s functions.php file:

add_post_type_support( 'page', 'excerpt' );

Once you have that in place – you can then use the WordPress the_excerpt() function to show the excerpt within the theme.

Remove “Private:” From WordPress Titles

I needed to be able filter out the text “Private:” for posts and pages that were password protected but did not want to edit any core WordPress files. To filter out that text, I added the following code into my functions.php file (within my theme directory):

function remove_private_prefix($title) {
$title = str_replace(
'Private:',
'',
$title);
return $title;
}
add_filter('the_title','remove_private_prefix');

(I also posted this filter in the WordPress Support Forum)