7 Code Snippets to Improve Your WordPress Speed & Security

Paul Gallipeau Web Design & Development 4 Comments

There are two major drawbacks of using WordPress for your web design and development projects. The first is that it often loads more resources than your website needs. The second is that WordPress has significant security risks due to it being the most popular CMS. In this post, I will provide you with 7 code snippets that you can copy/paste into your functions.php file to improve your site’s speed and security.

These code snippets have been compiled from an amazing thread from buildersociety.com. This is one of the best, if not the best, digital marketing forums. You can find even more copy/pasteable code there that I left out of this post.

Setup a Child Theme in WordPress Before Editing functions.php

You need to setup a child theme in WordPress before you start editing your functions.php file otherwise all of your work will be lost when there’s a WordPress update.

Many premium theme creators have child themes prepared for you that you can just download and install like any other theme. If your theme creators don’t provide one, follow the instructions in the video below from wpbeginner. If you’re using the BuSo Lightning theme, here’s our guide on setting up the child theme.

WordPress Security Code Snippets

The first snippet removes your WordPress version footprint. This makes it more difficult for a hacker or a bot to scrape your website and detect what version of WordPress you run.

remove_action('wp_head', 'wp_generator');

// Removes the rest of the WordPress version footprints throughout your site.

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

This snippet removes login error messages which might trip up unsophisticated bots that depend on the failure string to attempt another automated login.

add_filter('login_errors',create_function('$a', "return null;"));

Next up is a snippet that removed the admin name from your comments (if your website uses comments). This helps to protect you because before cracking an admin’s password, a hacker needs the admin’s username, This makes finding that username more difficult.

function remove_comment_author_class( $classes ) {
    foreach( $classes as $key => $class ) {
        if(strstr($class, "comment-author-")) {
            unset( $classes[$key] );
        }
    }
    return $classes;
}
add_filter( 'comment_class' , 'remove_comment_author_class' );

Here is a snippet to remove more footprints and unimportant elements from your wp_head function.

remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'start_post_rel_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'feed_links_extra', 3 );
remove_action('wp_head', 'feed_links', 2 );
remove_action('wp_head', 'parent_post_rel_link', 10, 0 );
remove_action('wp_head', 'start_post_rel_link', 10, 0 );
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

WordPress Code Snippets to Improve Page Speed

This code replaces the WP Jquery with Google’s CDN version. This improves your website’s speed and reliability under heavy traffic because you get to use Google’s closest server to load Jquery instead of your own. Chances are, Google has better servers than you (no offense).

add_action('init','jquery_register');

function jquery_register() {
    if(!is_admin()) {
    wp_deregister_script('jquery');
    wp_register_script('jquery',('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'),false,null,true);
    wp_enqueue_script('jquery');
    }
}

WordPress stores a ton of bloat in your database by keeping old post and page revisions. This code snippet sets the maximum to 10 to reduce database bloat.

if(!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS',10);

More database bloat removal, this time in the form of virtually never used messaging apps (YIM, AIM, and Jabber).

function remove_default_userfields( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
return $contactmethods;
}
add_filter('user_contactmethods','remove_default_userfields',10,1);

If you’re still craving more code, you can check out this thread for more. Thanks for reading!

Continue to Get Your Free Web Design Consultation!

Comments 4

    1. Post
      Author
    1. Post
      Author

      Yeah, I agree that a plugin would be better for a user who doesn’t know how to use a child theme. The embedded video goes over how to setup a child theme so that you can updated the child theme’s functions.php and prevent the reversion when you update WordPress.

Leave a Reply

Your email address will not be published. Required fields are marked *