8 Useful Code Snippets for WordPress

A WordPress code snippet is a small block of reusable code that you can use to extend the functionality of a WordPress-powered website; it is essentially a mini-plugin with less code.

WordPress code snippets can be used in different ways. Most of the time this involves going to the functions.php file, but you might want to think about that because this file is theme specific. If you decide to change your WordPress theme later and have non-theme specific functions in this file, you will have to start from scratch.

In this article, We would like to present you with eight useful code snippets that will enhance your WordPress website.

Please back up your site before you start making changes – especially make sure you have a copy of your original functions.php file.

Redirect new registered users to a specific page

If you require a new user to register on your WordPress site, you might want to redirect them to a specific page upon successful completion. Maybe you want to provide them with some important information or thank them for registering on your site. Add the following function to your functions.php file.

function wps_registration_redirect(){
return home_url( '/thankyoupage/' );
}
add_filter( 'registration_redirect', 'wps_registration_redirect' );

Limiting WordPress post revisions

Keeping track of post revisions in WordPress is a great feature but if you do much editing within the WordPress editor it’s easy to end up with dozens or even hundreds of revisions stored in the database. An easy fix is to limit the number of revisions per post to something more reasonable like 5-10.

if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 5);
if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', false);

Show a post date and modified date

Sometimes few months after you write a post you decide to update it with some new information. This simple snippet will add a modified date to your posts in the event that you make a change sometime after the initial publication date.

Posted on <?php the_time('F jS, Y') ?>
<?php
          $u_time = get_the_time('U');
          $u_modified_time = get_the_modified_time('U');
      if ($u_modified_time != $u_time) {
                echo "and last modified on ";
                the_modified_time('F jS, Y');
                echo ". ";
          }
?>

Remove the WordPress version number

You should always keep your WordPress version up to date but in case you want to hide your current WordPress version number, from visitors just add the following function to your functions.php file:

 function wpbeginner_remove_version() {
 return '';
 }
 add_filter('the_generator', 'wpbeginner_remove_version');

Put an end to automatic JPEG compression

If you’re using a third-party image optimizer plugins like Optimus or WP Smush, you might decide you want to disable the built-in WordPress image optimizer which is set to 90% out of the box.

add_filter( 'jpeg_quality', 'smashing_jpeg_quality' );
function smashing_jpeg_quality() {
return 100;
}

Show X results on the search results page

If you want to change the default number of search results returned to something greater or less that the default this snippet will let you do just that.

function limit_posts_per_search_page() {
	if ( is_search() )
		set_query_var('posts_per_archive_page', 20); 
}

add_filter('pre_get_posts', 'limit_posts_per_search_page');

Promote your RSS feed on every post

Adding this snippet to the functions.php of your WordPress theme will add a link to your RSS feed at the bottom of every post.

function wps_promote_feed($content,$class = "promote") {
    echo $content;
    if (is_single()) {
?>
        <div class="<?php echo $class; ?>">
            <h3>Enjoyed this article?</h3>
            <p>Please consider subscribing to our <a class="feed" href="<?php bloginfo('rss2_url'); ?>" title="Subscribe via RSS">RSS feed!</a></p>
        </div>
<?php
    }
}
add_filter('the_content','wps_promote_feed');

Disable hyperlinks in WordPress comments

Restrict commenters from posting links in WordPress comments by using a few lines of PHP code. Add the below code in your WordPress theme’s functions.php.

 add_filter('pre_comment_content', 'strip_comment_links');
 function strip_comment_links($content) {
 global $allowedtags;
 $tags = $allowedtags;
 unset($tags['a']);
 $content = addslashes(wp_kses(stripslashes($content), $tags));
 return $content;
 }
WebAnalytics Made Easy - StatCounter
scroll to top