Celebrating 20 years online :)
Web Dev + WordPress + Security

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, query the database, etc.

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 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 »' to generate “Next post in same category: Example Link »”. This parameter defaults to %link ».
  • 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:

	$images = array(
	$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:

	$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. 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-]+)?/?$ https://feeds.feedburner.com/yourfeedname [R=302,NC,L]

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-]+)?/?$ https://feeds.feedburner.com/yourfeedname [R=302,NC,L]
FYI: If you don’t know the URL of your WordPress feed, check out this article »

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 “https://feeds.feedburner.com/yourfeedname” 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! :)


About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
Blackhole Pro: Trap bad bots in a virtual black hole.

36 responses to “Optimize WordPress: Pure Code Alternatives for 7 Unnecessary Plugins”

  1. Tim Dodwell 2008/12/08 5:59 am

    Could not agree more with this article. I hate plugins that make wordpress and php seem so complicated. It is better to understand the simple code, so in the end you make a website exactly to your requirements, and not have settle with half baked jobs some people do. Having said this, things like sitemap generators are great plugins, as these functionalities are not easily programmed!

  2. Hello, and thanks for the article!

    There’s one thing on my mind, though. I would love to display icons for pages, rather than categories. Could it be done in the same manner as for categories?


  3. Jeff Starr 2008/12/15 8:58 pm

    @Andy: I am not sure which version of WordPress you are using, but I think that recent versions feature page-specific class names in the list output for the list pages tag. If so, you could use CSS to style in icons for the various pages.

  4. Hi Jeff,

    Thanks for the article, a good read as usual.

    I have a question for you:

    Using this in single.php (WordPress 3.0.1) ->


    with in_same_cat set to TRUE, works beautifully when I want to display links to posts only from the same category as the current post.

    However, when I then try to view posts by author, archives or tags, no previous/next links show up, and so it appears there is only one post in each of the views, which is not the case.

    What am I missing?

  5. Hi Again,

    Obviously, the code I pasted didn’t print, but you probably figured out I was referring to the first “alternative” in my question, the one dealing with “previous_post_link/next_post_linkin_same_cat setting.

  6. Jeff Starr 2010/11/09 9:22 am

    Sunny, not sure what the exact issue could be, but check that the theme files are using the correct code, and that there aren’t any custom loops interfering with the output. Also make sure that there is actually more than one post for each of the page views you are evaluating. I hope this helps!

  7. Hi Jeff,

    Thank you for answering.
    I will try to explain more clearly.

    Single posts are displayed via “single.php”.
    “previous_post_link” and “next_post_link” appear appear in single.php above each post.

    If I set “in_same_cat” parameter ‘TRUE’ for “previous_post_link” and “next_post_link:, for example: “previous_post_link(‘format’, ‘link’, ‘TRUE’, ‘excluded_categories ‘)”, then navigation links in “single.php” will move ONLY between posts of the SAME category, which is the behavior I want when I’m browsing a specific category.

    HOWEVER, If I want to browse “October 2010 Archives”, I will first get a page with excerpts to 9 posts. THEN if I click on a post title, I will go into the single.php view, and will only get “previous” “next” links to posts within the same category as the post CURRENTLY VIEWED.

    But as opposed to viewing by category, when browsing archives I want to navigate between all October posts, REGARDLESS OF WHICH CATEGORY they belong to.

    If you visit my site, you’ll quickly see what I mean.

    If you go to the links at the top and click “Web Design” then choose “WordPress”, you’ll get a page with all “WordPress” category posts. If you click a post, you’ll be able to navigate ALL the posts in the category, and ONLY the posts in the category, which is how I want it.

    Now try to navigate the “October 2010 Archives”. You’ll see there are 9 posts, but you won’t be able to view them all with “single.php”, only those of the same category as the first post you choose to view, which is NOT how I want it.

    I hope I’ve explained clearly, and more so, I hope you have an idea, because I’m STUCK!

    Thanks in advance for taking the time to help.. :)

  8. Hi sunny, it looks like you want the single.php navigation to do 2 mutually exclusive things:

    1. navigate within same-cat posts
    2. don’t navigate within same-cat posts

    One workaround might be to setup some conditional code that distinguishes between different categories or even tags. Beyond this, you could setup a custom post template or even use cookies to track whether visitors arrive at single.php via a category view, archive view, or whatever.

  9. Well, yes.. I get what’s needed conceptually. It’s the implementation that I’m having trouble with since I’m fairly new to PHP and WordPress both. I have been playing around with a conditional, but so far no cigar. I’ve also contacted Scribu of the “Smarter Navigation” plugi,n which utilizes cookies, to see if there is a way to use his plugin to do what I want – it kind of does already, but what I do to it in order to hide the navigation div for previous/next links breaks the plugin. If you have any ideas for a conditional I’d love to hear them. Thanks again. Sunny

  10. thx for this tipps!
    But now i think about modifying your feedburner-htaccess-rule for multisite installations. Wouldn’t it be nice if one rule in one .htaccess does all for any installed WP multisite? the only problem – my regex and mod_rewrite skills are very low. Any advice?

  11. Been searching left and right for an effective way to handle random quotes without using a plugin, and this solved my problem VERY effectively. I’m also using html/css within the quotes to help style somewhat as well.

    Thanks a lot, Jeff!

Comments are closed for this post. Something to add? Let me know.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Digging Into WordPress: Take your WordPress skills to the next level.
Crazy that we’re almost halfway thru 2024.
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.