Jump Menu : Content | Explore | Comments | Search | Home | Sitemap | Contact | Login | Access.

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 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 a couple of 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-]+)?/?$ 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!

Related articles

About this article

This is article #463, posted by Jeff Starr on Tuesday, December 18, 2007 @ 02:49pm. Categorized as WordPress, and tagged with code, htaccess, images, optimize, php, plugins, WordPress. Updated on January 23, 2008. Visited 20751 times. 25 Responses »

BookmarkTrackbackCommentSubscribeExplore

« How to Enable PHP Error Logging via htaccess • Up • December 22nd, 2007 »


25 Responses

1 • December 19, 2007 at 2:33 am — John @ Anime Online says:

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 • December 19, 2007 at 8:36 am — Lisa says:

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 • December 19, 2007 at 10:33 am — Perishable says:

@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 • December 25, 2007 at 4:22 am — Ibnu Asad says:

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 • December 26, 2007 at 7:11 am — DeepFreeze says:

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

6 • December 26, 2007 at 11:39 am — Perishable says:

@Ibnu Asad:

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

@DeepFreeze:

ABSOLUTELY ;)

7 • December 28, 2007 at 11:16 am — D.P.Lockhart says:

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 • December 28, 2007 at 11:22 am — D.P.Lockhart says:

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 • December 29, 2007 at 9:13 am — Perishable says:

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 • April 20, 2008 at 1:00 pm — gladbeast says:

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 • April 20, 2008 at 2:57 pm — Perishable says:

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 • April 21, 2008 at 4:07 am — Submarine says:

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… ;-)

13 • April 21, 2008 at 8:11 am — Perishable says:

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 ;)

14 • April 23, 2008 at 1:11 am — Submarine says:

Thank you very much !!!

15 • April 23, 2008 at 7:28 am — Perishable says:

My pleasure! ;)

16 • June 16, 2008 at 12:17 am — Erika says:

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.

17 • June 16, 2008 at 9:11 am — Perishable says:

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

18 • June 16, 2008 at 9:13 am — Erika says:

I’m working with WP 2.5.1 right now.

19 • June 17, 2008 at 10:07 am — Perishable says:

@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.

20 • June 17, 2008 at 10:12 am — Erika says:

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. :)

21 • August 2, 2008 at 7:40 am — Patrice Albertus says:

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 !

22 • August 3, 2008 at 7:50 am — Jeff Starr says:

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.

23 • September 3, 2008 at 11:49 am — bob says:

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.

24 • September 6, 2008 at 3:47 pm — Jeff Starr says:

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 alternate themes for your improved code-viewing pleasure! ;)

Drop a comment


Trackbacks / Pingbacks

  1. Buffer Dump 19DEC07 « Feet up, eyes closed, head back

Set CSS to lite theme
Set CSS to dark theme