Optimize WordPress: Pure Code Alternatives for 7 Unnecessary Plugins

[ Photo: Macro shot of a Yttrium claw ] In this article, my goal is to help you optimize WordPress by replacing a few common plugins with their correspondingly effective code equivalents. As we all know, WordPress can be a very resource-hungry piece of software, especially when running a million extraneous plugins. Often, many common plugins are designed to perform relatively simple tasks, such as redirect a feed, display a random image, or return a database value. For those of us comfortable with editing PHP and htaccess code, there is no need to bloat WordPress with additional plugins for the sake of a few routine tasks. For each of the “pure code” alternatives presented below, we are able to drop an unnecessary plugin without editing the WordPress core. In fact, all of the plugin replacements presented here affect only theme files, thereby keeping WordPress updates nice and easy. Well, okay, one or two methods require editing your root htaccess file, but we are all okay with that, right?

Next post and previous post in same category

Enabling users to navigate to the next or previous post within the category that they happen to be viewing can be a very helpful feature. Before WordPress 2, providing such intra-categorical links required a plugin such as scriptygoddess’ Next/Previous posts in same category (404 link removed 2014/04/18) to do the job. Since WP version 2, WordPress users may simply employ the following code, which is now built-in to the WordPress core:

<?php previous_post_link('format', 'link', in_same_cat, 'excluded_categories '); ?> | 
<?php next_post_link('format', 'link', in_same_cat, 'excluded_categories '); ?>

..and uses these four parameters:

  • format (string) — The format parameter enables you to format the link string by placing your code before and/or after the %link variable. For example, you could use 'Next post in same category: %link &raquo;' to generate “Next post in same category: Example Link »”. This parameter defaults to %link &raquo;.
  • link (string) — The link parameter specifies the link text that should be displayed. You may customize this string by placing code before and/or after the post title variable, %title. Otherwise, this parameter defaults to the post title.
  • in_same_cat (boolean) — This is the key to making this plugin replacement work. Set this value to 'TRUE' in order to generate links only to posts that are in the same category. This parameter defaults to 'FALSE', so remember to change it to enable intra-categorical navigation.
  • excluded_categories (string) — The string parameter enables you to omit specific categories from the navigational links that are generated by this function. For example, to navigate through posts (either same category or otherwise) that do not belong to category 6, simply enter '6' as the value for this parameter. You may also omit multiple categories by listing their respective numeric category IDs like so: 2, 4, 6, 8. There is no default for this parameter.

Display category icons

Styling your category titles with associated icons is a great way to customize your blog. While there are some great plugins for generating category icons, it is far from necessary to use them. Instead, create a set of images and name them after your categories. For best results, copy your category names exactly (e.g., include spaces, dashes, uppercase/lowercase letters, etc. as needed) and use the same file extension for all of them (e.g., .png). Once your icon images are ready, create a folder called “icons” (for example) in your default theme directory, and place the following code wherever you would like your category icons to appear:

<img src="<?php bloginfo('template_directory'); ?>/icons/<?php $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_name; ?>.png" alt="Category Icon" />

Copy, paste, upload, & go — no editing required! With that code in place, a representative category icon will appear according to the first corresponding category listed in the database. For blogs that only categorize each post into a single category, the following alternative code may be used to summon associated category icons:

<?php foreach((get_the_category()) as $cat) { echo '<img src="bloginfo('template_directory'); ?>/icons/' . $cat->cat_name . '.png" alt="Category Icon" />'; } ?>

Display post author icon

Another great customization trick is to automatically display icons for post authors. Especially if you have multiple authors or contributors at your blog, associating a custom icon for each of them may prove very helpful to your readers by facilitating recognition via visual branding. Whatever. The point is that you don’t need a plugin to implement this remarkably simple strategy. Applying the same image-creation guidelines presented in the previous section (only this time use author names instead of category names), we throw down this code to make it go:

Written by <img src="<?php bloginfo('template_directory'); ?>/icons/<?php the_author('login'); ?>.png" alt="Icon for: <?php the_author(); ?>"  />

Other than creating the images, no editing is required, however, you will probably want to tweak things delicately to suit your specific needs.

Display the most recent “post last updated” time and date

This is one of my favorites. I have always advocated providing a “last modified” date on all posts as a way of informing readers of recent changes and overall freshness. Of course, the easy way to provide this information is to use a plugin such as Last Modified, however, our goal here is to reduce our reliance on plugins and cut straight to the chase. Here is the code I have been using successfully for many moons:

Updated on <?php $x = get_the_time('U'); $m = get_the_modified_time('U'); if ($m != $x) { the_modified_time('F d, Y'); } ?>

This code checks the WordPress database for the “last modified” value of the current post. I have been using it within the loop, as I am fairly sure it will not work otherwise. To use this code, no editing is required, however, you may want to customize the date output format (i.e., 'F d, Y') to suit your needs.

Display random images

Much has been made of the ability to generate randomness via code. WordPress blogs are no exception. Displaying random banner images can keep your blog looking fresh. Indeed, randomly displayed images serve many purposes, and the good news is that you don’t need a plugin for many of them. For complex, repetitive, or otherwise advanced implementations, a plugin may be the way to go, but it is far from necessary. In fact, there are countless ways to summon random images — via JavaScript, PHP, and SQL, for example. For this plugin-replacement trick, we will use a straightforward yet highly effective snippet of PHP. First, create a directory full of images and then edit the following code to reflect their associated file names:

<?php
	$images = array(
		'image-01.png',
		'image-02.png',
		'image-03.png',
		'image-04.png',
		'image-05.png',
	);
	$image  = $images[array_rand($images)];
	$output = "<img src=\"http://domain.tld/path/to/image/directory/" . $image . "\" alt=\"Refresh browser for random image\" />";
	echo $output;
?>

To use this code, edit the filenames listed in the array, and then change the directory path (i.e., http://domain.tld/path/to/image/directory/) to match that of your own. No other editing should be required. Add as many or as few images as necessary — no plugin required!

Display random quotes

All WordPress users should be familiar with the amazingly lame “Hello Dolly” plugin that comes with the default installation of WordPress. Sure, it’s there to help new users get a feel for plugins, however, its functionality — generating random quotes — falls far short of justifying a plugin. As before, the are many ways to generate random quotes or strings of text, however, for most WordPress users, something nice and simple will work just fine. To display random quotes without a plugin, fill a text file with as many quotes as desired and upload it to your server. Then, call the random quotes by copying & pasting this code:

<?php
	$file  = "/home/public_html/path/to/random-quotes.txt";
	$quote = file($file);
	echo $quote[array_rand($quote)];
?>

Edit the code with the correct path to the text file (i.e., random-quotes.txt) containing the random quotes, which should look something like this:

This is your first quote.
This is your second quote.
This is your third quote.
This is your fourth quote.
This is your fifth quote.

Fill that puppy up with as many quotes as you can handle! Just remember to place each one on its own line :)

Automatically redirect your feeds to Feedburner

As many of us know, the most popular method for redirecting site feeds to Feedburner is to use Steve Smith’s excellent plugin, Feedsmith (404 link removed 2013/10/21). I have used the plugin for many months and have not experienced any issues with it, other than the fact that it is yet another unnecessary plugin. So, as any good htaccess enthusiast would do, I scoured my collection of stupid htaccess tricks and fashioned a set of plugin-free htaccess redirects to do the job:

Redirect Main Feeds

# temp redirect wordpress feeds to feedburner
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]
 RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
 RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/perishablepress [R=302,NC,L]
</IfModule>

Redirect Comment Feeds

# temp redirect wordpress comment feeds to feedburner
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]
 RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
 RewriteRule ^comments/feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/perishablepress [R=302,NC,L]
</IfModule>

To use, disable any Feedburner-redirection plugins that you may have, and copy & paste either chunk of htaccess code into your site’s root htaccess file. You will also want to ensure that you have edited the “http://feeds.feedburner.com/perishablepress” in either code block such that it reflects the URL of the associated Feedburner feed. Once in place, these directives will ensure that your original feed(s) are redirected to Feedburner, with similar accommodations made for the Feed Validation Service.

In closing..

While replacing WordPress plugins for their correspondingly functional code equivalents is not for everyone, many WordPress users are encouraged to “unplug” as much as possible from over-reliance on unnecessary plugins. The seven plugin replacements presented here are a great start, but there are many others. Just keep in mind that, in general, it is far more advisable to use a plugin instead of resorting to methods that involve hacking the WordPress core. The whole idea is to optimize and streamline your site, not make it more convoluted and tedious. With these seven methods, however, you can relax; none of them touch the WP core files, thereby making upgrading that much easier! :)

Peace!