usefull wordpress snippets

Useful WordPress Snippets

The thing that I love about WordPress is the fact that it is so versatile. You can do so much with it, and you have so much control over your site, what it does, and how it does it. he core functionality of WordPress alone is awesome, but sometimes you might want to change the configuration of WordPress to suit your needs. Knowing, or having a reference of useful snippets for WordPress can be a huge time saver. Sometimes you want to disable things that WordPress does. other times, you want to add to it. Useful WordPress snippets can save you hours of work, or can lighten the load on your server. Below I am listing some useful WordPress snippets and what they do.

Limit Post Revisions

You can make unlimited revisions by default, which can take up valuable space. You can limit post revisions to a number that you specify, with the code snippet below. Simply place it in your wp-config.php file:

define( 'WP_POST_REVISIONS', 2 );

Or you can disable them altogether:

define( 'WP_POST_REVISIONS', false );

Change […] Excerpt to Read More

I don’t know why some themes still have this, and I don’t know who in the world thought this looked good or made sense, but when you come across a theme that has a “[…]” in stead of a read more button with a link to the post, you can fix it with the following snippet. Just add it to your functions.php file:

function new_excerpt_more( $more ) {
    return ' <a href="'. get_permalink( get_the_ID() ) . '">Read More</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

Then, in your theme where it calls for the excerpt, you’ll have a “read more” link that links to the full post.

Featured Images in RSS Feeds

This one is a killer snippet that I’ve heard so many people asking for. WordPress does support featured images in RSS feeds by default, but with the help of this snippet, your featured images should show up. If you use Mailchimp to auto-compile a weekly newsletter, this is huge, because now, instead of a headline and text, you’ll have a pretty featured image to add the icing on the cake. Add the following snippet to your functions.php file:

function rss_post_thumbnail($content) {
	global $post;
	if(has_post_thumbnail($post->ID)) {
		$content = get_the_post_thumbnail($post->ID) . $content;
	}
	return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');

Hide WordPress Updates

How many times have you gotten that dreaded call from a client saying “I don’t know what I did! My site is broken! All I did was update WordPress” as you grimace on the other end? The truth is, you can hide WordPress Updates from your clients, so they won’t be tempted to do it themselves. Just add the following to your functions.php file:

function wp_hide_update() {
    remove_action('admin_notices', 'update_nag', 3);
}
add_action('admin_menu','wp_hide_update');

Conclusion: WordPress Snippets

I hope you find these WordPress Snippets super-handy. I know I did, especially the read more function. That one is particular is one of my biggest pet peeves. I plan on collecting more to come, but be sure to bookmark this post so that you can refer to it later. You might also consider building a library of snippets to save you time.

Select Language