6 useful WordPress tricks, hacks & snippets

WordPress

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

<?php endwhile; ?>

Just before this line, place the following code:

<?php if(!$show_ads){ ?>
Insert Code Here
<?php $show_ads = 3; } ?>

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:

define( 'WP_POST_REVISIONS', 3 );

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.

<title><?php if (is_home () ) { bloginfo(‘name’); }
elseif ( is_category() ) { single_cat_title(); echo ‘ - ‘ ; bloginfo(‘name’); }
elseif (is_single() ) { single_post_title();}
elseif (is_page() ) { single_post_title();}
else { wp_title(‘’,true); } ?></title>

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.

define('WP_HOME', 'http://board.resabi.com/');
define('WP_SITEURL', 'http://board.resabi.com/');

define ('WP_CONTENT_FOLDERNAME', 'base');
define ('WP_CONTENT_DIR', ABSPATH . WP_CONTENT_FOLDERNAME) ;
define('WP_CONTENT_URL', WP_SITEURL . WP_CONTENT_FOLDERNAME);

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.

<style type="text/css">
.share {padding:20px; display:block; font-size:18px; text-align:center}
.share a {padding:6px 12px; margin:0 3px; color:#fff; border-radius:3px}
</style>
<div class="share">
				<a style="background:#5977B2" href="http://www.facebook.com/share.php?u=<?php echo esc_url( get_permalink() ); ?>" onclick="Popup=window.open( this.href, 'Popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=580,height=350, top=200, left=50');return false;">Share on Facebook</a>

				<a style="background:#00ACEE" href="https://twitter.com/share?url=<?php echo esc_url( get_permalink() ); ?>&text=<?php echo get_the_title(); ?>" onclick="Popup=window.open( this.href, 'Popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=580,height=350, top=200, left=50');return false;">Tweet</a>

				<a style="background:#D14128" href="https://plus.google.com/share?url=<?php echo esc_url( get_permalink() ); ?>"  onclick="Popup=window.open( this.href, 'Popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=580,height=350, top=200, left=50');return false;">Google +1</a>
			</div>

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

Your email address will not be published. Required fields are marked *