Helpful WordPress Code Snippets

by Ryan Cowles August 8th, 2012 

wordpress-snippets

Throughout my experience with WordPress, I have built up a collection of useful code snippets. These snippets are in no particular order, but they all modify WordPress' default behavior to accomplish a common task. Without touching a single core file, you can modify WordPress' behavior with minor code additions — often just by adding and removing filters, or putting together a simple function.

Remove Auto Paragraphs from the excerpt

I have seen many people ask how to remove the automatically generated paragraph tags from a posts excerpt. To do so, all you need to do is remove the filter that applies the tags. Add the following to your theme's functions.php:

// Remove auto paragraphs from the excerpt
remove_filter ('the_excerpt', 'wpautop');

In some cases, I've wanted to remove the automatic paragraphs from the_content as well. All you have to do is replace the_excerpt with the_content like so:

// Remove auto paragraphs from the content
remove_filter ('the_content', 'wpautop');

Allow Shortcodes in the Default Text Widget

WordPress default text widget is useful, but simple. While simplicity isn't a bad thing, sometimes you want to be able to add more functionality, like being able to use shortcodes in the widget. You can easily add this functionality by adding the following to your theme's functions.php file:

// Allow shortcodes in default text widget
add_filter('widget_text', 'do_shortcode');

Add a shortcut to the WordPress Admin Toolbar

Since the Admin Toolbar was introduced in 3.1, it has become more robust with each release. With that said, sometimes I find myself wanting to add another link in the toolbar. You can do so by adding the following to your functions.php (I used Google as an example link, but replace the info with your own):

// Add a link to the admin toolbar
function my_toolbar_link($wp_admin_bar) {
$args = array(
'id' => 'google', // The ID for the <li> containing your link
'title' => 'Google', // The human readable label
'href' => 'http://www.google.com', // The full URL for your link
'meta' => array(
'class' => 'google', // The class of your <a> element
'title' => 'google' // The title attribute of your <a> element
)
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'my_toolbar_link', 999);

Now your admin bar will have your custom link!
Original snippet: http://digwp.com/2012/06/shortcut-links-toolbar/ for the original

Remove autolinks from comments

By default WordPress will wrap any URL's left in your comments in a nice <a> tag. While this can be a good thing, spammers will often abuse this. If you have a lot of spam comments getting by Akisment, you may want to remove the autolinking of URL's. To do so, just add the following to your functions.php:

// Remove autolinks from comments
remove_filter('comment_text', 'make_clickable', 9);

Add the_slug to body class

Sometimes you may need post-by-post or page-by-page control over the class on your body tag. One way to accomplish this is by adding the_slug. You can do so by adding the following to your functions.php file:

// Add slug to the body class
function page_bodyclass() { // add class to <body> tag
global $wp_query;
$page = '';
if (is_front_page() ) {
$page = 'home'; // If it's your Front page, apply 'home' as classs
} elseif (is_page()) {
$page = $wp_query->query_vars["pagename"]; // Otherwise use pagename
}
if ($page)
echo 'class= "'. $page. '"';
}

Conclusion

If you haven't been able to tell by now, I think WordPress is an amazing tool. With it's default behavior, it can accomplish quite a bit. Through the use of functions and filters, you can extend that functionality to do just about anything. There are countless ways you can use and manipulate WordPress, but I hope this serves as a good starting point!

If you liked this, you'll love Crazy Handy WordPress Plugins you Haven't Heard of (yet)

Ryan Cowles

Ryan Cowles is a WordPress / Front End Developer living in Los Angeles, California. Along with a passion for building creative websites, he also enjoys photography, design, travel and the great outdoors. You can view his personal website by visiting http://ryanscowles.com. To see what he has been up to lately, check out his blog at Metacom Creative.

Ryan S. Cowles

You May Also Like

Comments are closed.