Cool WordPress Snippets

Cool WordPress Snippets

The great thing about WordPress is the fact that you can customize it to fit your needs in a variety of ways. If you know some simple PHP, you can add custom functionality to any WordPress theme. Adding simple scripts, called snippets, to your WordPress theme allows you to customize how WordPress handles the data that it contains in its database. In this post, I’ve compiled some cool WordPress snippets you wanted to use in your WordPress site.

Require a Featured Image Before Posting

I don’t know how many times I’ve written an article in WordPress and accidentally hit publish before I added a featured image. The snippet below checks to make sure that you have selected a featured image for your post before publishing it. If you haven’t selected a featured image, it will save the post as a draft until you do.

add_action('save_post', 'wdb_check_thumbnail');
add_action('admin_notices', 'wdb_thumbnail_error');

function wdb_check_thumbnail($post_id) {

    // convert to a custom post type 
    if(get_post_type($post_id) != 'post')
    if ( !has_post_thumbnail( $post_id ) ) {
        // Show the users an admin message
        set_transient( "has_post_thumbnail", "no" );
        remove_action('save_post', 'wdb_check_thumbnail');
        // make the post a draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));

        add_action('save_post', 'wdb_check_thumbnail');
    } else {
        delete_transient( "has_post_thumbnail" );

function wdb_thumbnail_error()
    if ( get_transient( "has_post_thumbnail" ) == "no" ) {
        echo "<div id='message' class='error'><p>You must select featured image in order for your post to be published.</p></div>";
        delete_transient( "has_post_thumbnail" );


Add Font Selection to Kitchen Sink in Post Editor

You’ve always been able to select whether your text is paragraph text, or a heading. However, if you use the WordPress snippet below, you can select a font of your choosing as well.

function add_fontselect_row_3( $mce_buttons ) {
$mce_buttons[] = 'fontselect';
return $mce_buttons;

Add Favicon to a Theme

Add your favicon directly into your WordPress theme with this cool WordPress snippet.

<span style="font-weight: normal;"> </span>
function your_favicon() { 
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" />'; 
add_action('wp_head', 'your_favicon');

Embed Google Analytics Code Into Your Theme

Are you tired of manually inserting your Google Analytics code into your WordPress theme? With this snippet, you can embed the Google analytics code in your theme for good.

add_action('wp_footer', 'ga'); 
function ga() { ?> 
// Paste Google Analytics code from Your Google Analytics Account (a.k.a. the script) below
<?php } ?>

List Random Posts

If you’re like me, you’ve written hundreds, maybe even thousands of posts. Getting people to look at your older posts is nearly impossible. People just don’t want to dig that far into your website. However, if you use the WordPress snippet shown below, you can display a list of random posts. This can breathe life back into your older posts. This can also increase your page views, because people get a chance to see a great mix of your content.

<ul><li><h2>A random selection of my writing</h2> 
$rand_posts = get_posts('numberposts=5&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;orderby=rand'); 
foreach( $rand_posts as $post ) : 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endforeach; ?> 

 Automatic Twitter Mentions

I saved the best for last. If you want to mention different users on Twitter, you can add this WordPress snippet to make life easier. All you have to do is enter their Twitter handle, and this snippet will create an automatically to that Twitter account.

function content_twitter_mention($content) {
	return preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/', "$1<a href=\"$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>", $content);

add_filter('the_content', 'content_twitter_mention');   
add_filter('comment_text', 'content_twitter_mention');


Which one of these cool WordPress snippets is your favorite? I’d have to say that my favorite is the Twitter snippet. You can mention and the link to any twitter follower, just by mentioning their Twitter handle. Do you have a cool WordPress snippet you would like to share? If so, feel free to leave it in the comments section below.