6 useful WordPress tricks, hacks & snippets

What you can do with WordPress is limited only by your imagination and your ability to write or find the right piece of code. Today I’d like to share with you a couple of very useful snippets and tricks that can help you create custom functionality for your WordPress based website.

Insert ads between posts

You’ve probably seen some websites place ads after, say, the 3rd post. Here’s how you can do it with just two lines of code.

Find the following line of code in your loop (blog posts, category, tag…)

Just before this line, place the following code:

Replace Insert Code Here with your ad code or whatever you wish to appear after the 3rd post.

Of course, if you prefer to put it after a 5th post or other, just change the number after $show_ads.

Via WPHacks

Limit WP post revisions easily

Whenever you save a change in a WordPress post or page, the system saves a version of that post. This can easily bring a lot of junk in the database, especially if you tend to make many corrections or save frequently as you prepare a post.

There are several plugins for removing revisions from the database, but there’s also a very easy way to limit the number of saved revisions for each post/page.

1 line of wp_config code is all you need:

WP_POST_REVISIONS:

  • true (default), -1: store every revision
  • false, 0: do not store any revisions (except the one autosave per post)
  • (int) > 0: store that many revisions (+1 autosave) per post. Old revisions are automatically deleted.

More info at WP codex

Custom template for each category

WordPress Category templates are very easy to use, view the WordPress Codex – Category Templates and you will see that hierarchy for a category templates is:

  • 1. category-6.php
  • 2. category.php
  • 3. archive.php
  • 4. index.php

This basically just means that WordPress will first look for a template labeled category-6, if there is none, it will then look for category.php and so forth all the way down to index.php.

More info at GabeDiaz, via reSapiens

Change the page title attribute

If you want to change your pages title template, and don’t want to use a SEO plugin for that, here’s a simple code for you. It takes the title of the blog for the home page, or the category, post or page title respectively.

Via WPHacks

Change the name/path of the wp-content folder

If you’ve ever wanted to rename the wp-content folder (eg. http://mysite.com/stuff), here’s a relatively easy way to do it. You might have wanted to rename it so it’s harder to tell the site is based on WordPress, or maybe for security reasons. Now you can with just a few lines of code in your wp-config file. I’ve combined two codes to get the desired effect, one from Hongkiat and one from MCloud. The one from Hongkiat didn’t work on localhost.

Don’t forget to replace http://board.resabi.com/ with your site’s url and base with the name/path you want for your folder.

Share buttons without a plugin

These are the essential share buttons (Facebook, Twitter and Google+), but you can easily add others following the instructions on their websites. Using these can help lower the time needed for the page to load.

Got any cool snippet you would like to share?

Please add it via comments.

Photo Credit: bobbigmac via Compfight cc

Sharing is caring!

Leave a Reply