Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
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.
GA Pro: Add Google Analytics to WordPress like a pro.

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

  1. John @ Anime Online 2007/12/19 2:33 am

    If you are a beginner like I am :) it’s better to stick to WP plugin, I see that the 7 method you mentioned on the article looks very simple and easy to understand. Maybe I’ll try 1 or 2 that suit on 1 of my WP blog.

  2. Cool! Thanks for sharing :) I’m currently using the Feedsmith plugin and I’ll be deleting that plugin and using your .htaccess method :D

  3. @John: That is definitely a wise move — until you get a better “feel” for WordPress, it is far safer to use plugins for extra functionality. However, as you say, many of these plugin-replacements are relatively simple and thus ideal for experimentation and learning, if nothing else.

    @Lisa: Yay! That is really great! Glad to hear you found the article useful — thanks for the feedback :)

  4. Ibnu Asad 2007/12/25 4:22 am

    Thanks for the post…especially the ‘Display random images’ part.

    I’ve been looking for a very simple script but most scrips out there have too much functions…your’s are plain simple and easy to implement.

    Thank you again!

  5. DeepFreeze 2007/12/26 7:11 am

    Really neat. You forgot to mention a little fact, “PLEASE TAKE BACKUPS”, lol. That can really help.

  6. @Ibnu Asad:

    Happy to help, Ibnu — thanks for the positive feedback :)

    @DeepFreeze:

    ABSOLUTELY ;)

  7. D.P.Lockhart 2007/12/28 11:16 am

    Love your site, very useful! I just have something to add to your “Post Last Updated” code. The code you provide doesn’t offer an option to if you haven’t updated the post yet and just leaves a blank space. n.n()

    Last Updated:

    If the post hasn’t been edited yet then it will post:

    Last Updated: never

    Otherwise It will display as normal.

  8. D.P.Lockhart 2007/12/28 11:22 am

    Sorry completely spaced and forgot about code filters…

    Just add the following to the bottom of the code, after the if statement and before the closing php tags

    else {print ('never');}

  9. Perishable 2007/12/29 9:13 am

    D.P.Lockhart,

    You are absolutely right about the code flopping under such circumstance. I will be implementing your solution on my site today — thank you for sharing it with us!

  10. gladbeast 2008/04/20 1:00 pm

    hi there, big ups on the work you’re doing, i’ve used your random text code to stick random html in a text widget, but it’s very temperamental, is there anything i should know about ‘injecting’ html in this way?

    cheers,

  11. Perishable 2008/04/20 2:57 pm

    Hmmm.. not too sure about what the issue might be.. the script itself is fairly straightforward, and I have not yet heard of any specific problems related directly to the code itself. Have you tried this method of randomizing images? It takes a slightly different approach, and may resolve any issues you are experiencing.

  12. Submarine 2008/04/21 4:07 am

    Great tutorial !

    Have a look to my plugin : Category Icons. A widget is included to display icons in the sidebar. You should have a look… ;-)

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 disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.