Handy WordPress Code Snippets

 Handy WordPress Code Snippets

Handy WordPress Code Snippets

In the past few months, I’ve shared some handy WordPress code snippets to help you during the development process. These code snippets and custom functionality to your WordPress site. Sometimes, they handle difficult tasks with just a couple of lines of code. That’s why they’re considered such handy WordPress code snippets. This week’s collection of code snippets will give you control over different aspects of your WordPress site. Check out these handy WordPress code snippets.

Declare Minimum Comment Length

Sometimes, you get spammers or spambots that will leave false comments on your blog. This can be a real pain, because they’ll come and leave garbage comments, such as just a few letters or numbers on your posts. If you use this code snippet, you can set a minimum comment length for your comments section. If someone comes and leads a couple of words, or a bunch of numbers, their comment will be rejected.

add_filter( 'preprocess_comment', 'minimal_comment_length' );
function minimal_comment_length( $commentdata ) {
    $minimalCommentLength = 20;
    if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength ){
    wp_die( 'All comments must be at least ' . $minimalCommentLength . ' characters long.' );
    }
    return $commentdata;
}

Add Google +1 Button to Your Posts

Google plus is an important social media platform. This means that it’s really important to include Google plus on your site. To automatically embed a Google plus share button in your posts, use the code snippet shown below.

add_filter('the_content', 'google_plusone');
function google_plusone($content) {
        $content = $content.'<div class="plusone"><g:plusone size="tall" href="'.get_permalink().'"></g:plusone></div>';
        return $content;
}
add_action ('wp_enqueue_scripts','google_plusone_script');
function google_plusone_script() {
        wp_enqueue_script('google-plusone', 'https://apis.google.com/js/plusone.js', array(), null);
}

Remove the Admin Bar (I can’t stand this)

This has to be one of the most annoying features in WordPress. Use this code snippet to remove the admin bar from the front end of your site. This means that your view on no longer be obscured by the admin bar showing up while you’re logged in.

remove_action('init', 'wp_admin_bar_init');

Hide Update Messages From Clients

The last thing you want your clients to do is login to their WordPress site and break something. Whenever a client sees an update message for WordPress, they can’t wait to click that button. Use this snippet to hide the WordPress update message from your clients, saving you from having to fix their website when they break it.

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

 Set Minimum Word Requirement for Posts

This handy WordPress code snippet is especially useful when you have guest posters on your site. Use this snippet to set a minimum word requirement for all posts. I set my minimum word requirement to 300, because that is the point where Google begins to index the page.

function minWord($content){ 
 
global $post;
$num = 100; //set this to the minimum number of words
$content = $post->post_content;
 
if (str_word_count($content) <  $num) 	    wp_die( __('Error: your post is below the minimum word count.') );
} add_action('publish_post', 'minWord');

Email Site Members About New Post (Automatic)

This snippet is extremely handy for when you run a membership website using WordPress. Instead of having to keep up with the database, and in email marketing platform, you can set it so WordPress automatically emails all of the users of your website when you create a new post. The function is set to activate once you publish a post.

<?php
function email_members($post_ID)  {
	$wp_user_search = new WP_User_Query( array( 'fields' => array('user_email') ) );
	$usersarray = $wp_user_search->get_results();
	$arrUsers = array ();
	for ($arr = $usersarray, $mU = count ($arr), $iU = 0; $iU < $mU; $iU++) {
		$arrUsers[] = $arr[$iU]->user_email;
	} // for
	$users = implode(",", $arrUsers);
	
	mail($users, "New post notification : " . get_bloginfo('name') , "A new post has been published on " . get_bloginfo('siteurl') );
    return $post_ID;
}
add_action('publish_post', 'email_members');
?>

 Change Image Sizes In Admin

The last snippet allows you to change the image sizes that you can choose from in the drop-down menu in the admin section. This enables you to customize the formatting for your own images.

<?php 
if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'new-size', 300, 100, true ); //(cropped)
} 
add_filter('image_size_names_choose', 'my_image_sizes');
function my_image_sizes($sizes) {
        $addsizes = array(
                "new-size" => __( "New Size")
                );
        $newsizes = array_merge($sizes, $addsizes);
        return $newsizes;
} 
?>

Conclusion

Using this list of handy WordPress code snippets, you can have complete control over your WordPress site. You can control comments, post lengths, and you can even email the members of your website, directly from WordPress. Which one of these handy WordPress code snippets is your favorite? Have one of your own, feel free to share it with the rest of us in the comments section below.

2 Comments

  1. Pingback: Handy WordPress Code Snippets | Design News October 10, 2014
  2. Hassaan October 10, 2014
    • jgeorge October 25, 2014
Select Language