Spring Sale! Save 30% on all books w/ code: PLANET24
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:

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

Peace!

About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
WP Themes In Depth: Build and sell awesome WordPress themes.

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

  1. Perishable 2008/04/21 8:11 am

    That looks like an excellent plugin, Submarine — full of functionality and able to do all sorts of excellent category icon tricks! Especially for otherwise difficult-to-achieve tasks, like displaying icons next to a list of category names in the sidebar, your plugin is simply a must-have. Thanks for sharing it with us ;)

  2. Submarine 2008/04/23 1:11 am

    Thank you very much !!!

  3. Perishable 2008/04/23 7:28 am

    My pleasure! ;)

  4. Now, I can appreciate that category icon edit that you have there, but I wonder. Would something like that translate well for having thumbnails for a single post? Like, for instance.. if you have a photoblog and want to use a thumbnail of the next post’s picture in the “next post” navigation link. I tried something similar and failed miserably, LOL, so I got a little excited when I saw that.

  5. I’m working with WP 2.5.1 right now.

  6. Perishable 2008/06/16 9:11 am

    Hi Erika, I may be able to help you with this. Which version of WordPress are you using?
    Regards,
    Jeff

  7. Maybe we should talk about this on the side?

    I got pretty damn close, with discovering the previous_image_link and next_image_link tags in the wp-includes/media.php file. The image will appear, but it links to the attachment in the next post, not the actual post itself.

    There’s also a plugin called the “neighbor post preview” plugin that calls the image and the title in a tooltip-style tag, but I haven’t gotten around to actually trying to manipulate that.

    I do appreciate the effort, and subscribed to your blog. There’s some good stuff in here. :)

  8. @Erika: well, I gave it my best shot, tried everything I could think of, but without success. My initial plan was to call the next category via the link-template.php core file’s next_post_link and previous_post_link functions. I tried just about every template tag in the book, but with no luck. I managed to get the current post category to display, but that would be useless as we are targeting next and previous posts. I also tried fiddling with various plugin functions, database queries, and global variables — nada. So, sorry to disappoint, but I did enjoy the process of learning and experimenting with everything. Unfortunately, database queries are one of my weak points, and ultimate success with my intended implementation ultimately rests on a solid bit of SQL. Needless to say, I will continue to keep my eye out for a possible solution and report back here and with a full article if/when something pops up.

  9. Patrice Albertus 2008/08/02 7:40 am

    Hello,
    I’m a french WordPress user and was wondering if your feedburner code to avoid feedburner plugin with .htaccess redirect is still active ? I tried but nothing happens, maybe because the "RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]" condition is obsolete ?
    Thanks for your help !

  10. Jeff Starr 2008/08/03 7:50 am

    Hi Patrice,
    Have you tried placing the code directly before the rules used for your WordPress permalinks? I don’t think I mentioned it in the article, but the technique only seem to work when appearing before the permalink rules in the htaccess file.

  11. Nice article. Exactly what I need.

    Is there anyway you can make those code windows a tad bigger? some are displaying as half a line on my screen and I’m finding it a little difficult to view and select it.

  12. Jeff Starr 2008/09/06 3:47 pm

    Hi bob, thanks for the feedback; I am glad you found the article useful.

    As for the “code windows”, I assume you are using Internet Explorer, yes? If so, I feel your pain and am currently working a new design for the site that will hopefully address this issue. In the meantime, feel free to select any one of my 14 22 alternate themes for your improved code-viewing pleasure! ;)

Comments are closed for this post. Something to add? Let me know.
Welcome
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 »
SAC Pro: Unlimited chats.
Thoughts
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.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.