WordPress Tweaks

I will fully admit that I have cribbed the following optimisation recommendations from the brilliant Labnol site: http://www.labnol.org/software/must-have-wordpress-plugins/14034/

This is a great site which is one of my ‘daily visits’, it regularly has superb how-to guides and it coincidentally had these WordPress articles at just the right time I needed them. Both the ‘Optimisations’ and ‘Must Have Plugins’ articles are rather lengthy and I only needed a handful of the options mentioned. I have only listed the ones here which I actually use so you should click on the link and check out Labnol’s entire list. This may seem like I’m just ‘stealing’ Labnol’s content (and I’ll remove it if Labnol wants me to) but I have only copied a portion of it and only because I am worried about a future date when I need to make reference to these sections of the articles and they have been deleted.

A note on editing the functions.php file

This post recommends editing the functions.php file to improve aspects of your WordPress site. Note that the functions.php file is actually tied to your theme so you will lose any customisations you make to it if you change themes. Where I recommend you edit the functions.php file it is probably better to use a plugin (I do!) such as ‘Code Snippets’ (https://wordpress.org/plugins/code-snippets/screenshots/) to manage any code that you would otherwise add to functions.php. This plugin then lets you managed each piece of code like its own plugin and you can turn it on and off and it will survive across theme changes. It adds its own section to the sidebar which acts like the ‘Plugins’ section.

Remove unnecessary meta tags from WordPress header
If you look at the HTML source code of your WordPress site, you will find a couple of meta tags in the header that aren’t really required. For instance, the version of WordPress software running on your server can be easily retrieved by looking at your source header.

<meta name="generator" content="WordPress 4.1" />

This information is a good hint to WordPress hackers who are looking to target blogs that are using the older and less secure versions of WordPress software. To completely remove the version number and other non-essential meta-data from your WordPress header, add this snippet to the functions.php file found in your WordPress themes folder.

remove_action( 'wp_head', 'wp_generator' ) ;
remove_action( 'wp_head', 'wlwmanifest_link' ) ;
remove_action( 'wp_head', 'rsd_link' ) ;

Disable HTML in WordPress comments
The comment box in WordPress allows commenters to use HTML tags and they can even add hyperlinks in their comment. The comments have rel=nofollow but if you would like to completely disallow HTML in WordPress comments, add this snippet to your functions.php file.

add_filter( 'pre_comment_content', 'esc_html' );

Turn off Post Revisions in WordPress
WordPress includes a helpful document revisions feature to help you track changes to post edits and you can also revert to any previous version of your blog posts. Post revisions do however increase the size of your WordPress wp_posts table as each revision means an additional row.

To disable post revisions in WordPress, open the wp-config.php file in your WordPress directory and add the following line:

// Turn off post revisions
define( 'WP_POST_REVISIONS', false);

Alternatively, if you would like to retain the Post Revisions functionality, you may just limit the number of posts revisions that WordPress stores in the MySQL database. Add this line to the wp-config file to only store the recent 3 edits.

// Limit post revisions to 3
define( 'WP_POST_REVISIONS', 3);

 

Hide the non-essential WordPress RSS Feeds
Your WordPress installation generates multiple RSS Feeds – the blog feed, article feeds, comments feed, category feeds, archive feeds, etc. – and these are auto-discoverable as they are included in the HTML header of your blog pages using the <link> meta tag. If you just want to publicize your main RSS feed and remove the other feeds from the , add a line to your functions.php file:

remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );

 

Change the Permalink Structure
Do not use the default Permalink structure of WordPress since it is bad for SEO. Go to Options -> Permalinks inside your WordPress dashboard and change your WordPress Permalink structure to something like:

Option 1. /%post_id%/%postname%
Option 2. /%category%/%postname%/%post_id%/

 

Add Favicon and Touch Icons
Your WordPress theme may not even include references to the favicon (favicon.ico) or the Apple touch icons but web browsers and feed readers may still request them from your server. It’s always better to serve a file than returning a 404.

First, create a 16×16 favicon.ico and a 144×144 apple-touch.png file and upload them to the home directory of your blog. Then add this line to your .htaccess to redirect all apple touch icon requests to that particular file.

RedirectMatch 301 /apple-touch-icon(.*)?.png http://example.com/apple-touch.png

 

Disallow Indexing of WordPress scripts
You want Google and other search engines to crawl and index your blog pages but not the various PHP files of your WordPress installation. Open the robots.txt file in your WordPress home directory and add these lines to block the bots from indexing the backend stuff of WordPress.

User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Disallow: /wp-content/plugins/
Disallow: /wp-content/themes/
Disallow: /feed/
Disallow: */feed/

 

Hide XML Sitemaps from Search Engines
XML Sitemaps will help search engines better crawl your site but you don’t want search engines to actually show your sitemap in search results pages. Add this to your .htaccess to prevent indexing of XML sitemaps.

<IfModule mod_rewrite.c>
<Files sitemap.xml>
Header set X-Robots-Tag "noindex"
</Files>
</IfModule>

 

Log 404 Errors in Google Analytics
404 errors are a missed opportunity. You can use events in Google Analytics to log your 404 errors including details about the referring site that is pointing to that 404 page of your site. Add this snippet in your 404.php file.

<? if (is_404()) { ?>
_gaq.push(['_trackEvent', '404',
document.location.pathname + document.location.search,
document.referrer, 0, true]);
<? } ?>

 

Stop WordPress from Guessing URLs
WordPress has a strange habit of guessing URLs and it does make mistakes in most cases. Let me explain. If a user request labnol.org/hello URL but if that page doesn’t exist, WordPress may redirect that user to labnol.org/hello-world just because the URLs have some common words.

If you would like WordPress to stop guessing URLs and instead issue a 404 Not Found error for missing pages, put this snippet in the functions.php file:

add_filter('redirect_canonical', 'stop_guessing');
function stop_guessing($url) {
if (is_404()) {
return false;
}
return $url;
}

 

Disable File Editing inside WordPress
When you are logged into your WordPress dashboard as an admin, you can easily edit any of the PHP files associated with your WordPress plugins and themes. If you would like to remove the file editing functionality (one missing semicolon can take down your WordPress site), add this line to your wp-config.php file. On the other hand, you might be somewhere where you don’t have SSH access and this could be the only way to edit your files. Choose carefully!

define( 'DISALLOW_FILE_EDIT', true );

 

Remove extra Query Parameters from URLs
If the web address of your WordPress site is abc.com, people can still reach your site if they add a few query parameters to the URL. For instance, abc.com/?utm=ga or abc.com/?ref=feedly are, technically speaking, completely different URLs but will work just fine.

This is bad because it dilutes your link equity (SEO) and, in an ideal situation, you would like all URLs to point to the canonical version. Add this little snippet to your .htaccess file and it will strip the unnecessary query parameters from all incoming requests.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !^p=.*
RewriteCond %{QUERY_STRING} !^s=.*
RewriteCond %{REQUEST_URI} !^/wp-admin.*
RewriteRule ^(.*)$ /$1? [R=301,L]
</IfModule>

 

Remove the Admin Bar
This is an annoying feature of WordPress – it adds an admin bar on top of all pages and that is visible to all users who are logged into their WordPress.com accounts. This can however be removed by adding a line to your functions.php file.

add_filter('show_admin_bar', '__return_false');

 

Remove the WordPress Emojis
Starting with v4.2, WordPress now inserts Emoji related files in the header of your website. If you are not planning to use the emoticons and emojis in your blog, you may easily get rid of these extra files by adding the following lines to your functions.php file:

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );

 

Set Expiry Headers for Static Content
It is worth turning this on if your site gets a lot of traffic. The static files hosted on your WordPress website – like images, CSS and JavaScript- won’t change often and thus you may set Expire Headers for them so that the files get cached on the user’s browser. Thus, on subsequent visits, your site will load relatively faster as the JS and CSS files would be fetched from the local cache.

Refer to the HTML5 Boilerplate for details on setting up expiry and compression headers for performance. If you are using a caching plugin like W3 Total Cache, the cache control is managed by the plugin itself.

ExpiresActive On
ExpiresByType image/gif "access plus 30 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"

 

Deal with Ad Blockers
If you support your income by serving ads, it can be pretty annoying if people are blocking your ads. Some of your blog readers may be using ad-blocking software to block ad serving from your site. You can serve alternate content like a list of your popular WordPress posts or embed a YouTube video instead.

Leave a Reply

(email optional)


Warning: Undefined array key "rerror" in /home/public/blog/wp-content/plugins/wp-recaptcha/recaptcha.php on line 291