10 Must Have Hacks For WordPress Development

Building your own wordpress themes will open your eyes to a lot of things, mainly the fact that you’ll be retyping a lot of code over and over and over again. So in this post I am going to show you ten wordpress code hacks that you can add to your arsenal that will not only save you a lot of time, but they will also set your themes apart from others who don’t come pre built with these types of features.

Adding A Post Loop Anywhere In Your Theme ↓

Putting the code below anywhere in your themes files (sidebar.php, footer.php, header.php, ect) will pull the 5 most recent posts from a category named “Featured“. You can change the showposts=5 count to whatever you’d like and also change the category name. This is especially useful if you’re building a custom home page and want to show recent posts from different categories on your blog.

< ?php $my_query = new WP_Query('category_name=Featured&showposts=5');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
< ?php endwhile; ?>

Showing Related Posts Without A Plugin (based on related tags) ↓

< ?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
	$tag_ids = array();
	foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

		'tag__in' => $tag_ids,
		'post__not_in' => array($post->ID),
		'showposts'=>5, // Number of related posts that will be shown.
	$my_query = new wp_query($args);
	if( $my_query->have_posts() ) {
		echo '
<h3>Related Posts</h3>
		while ($my_query->have_posts()) {
	<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title_attribute(); ?>">< ?php the_title(); ?></a></li>
< ?php
		echo '</ul>

Source: Bin-Blog

Display Author Information For Posts ↓

By using the codes below in your single.php file, you will pull the information from the post author’s profile on your blog and show a bit about them. This is especially useful if you’ve got a lot of authors on your blog.

<div id="author-info">
<div id="author-image">
    	<a href="<?php the_author_meta('user_url'); ?>">< ?php echo get_avatar( get_the_author_meta('user_email'), '80', '' ); ?></a></div>
<div id="author-bio">
<h4>Written by < ?php the_author_link(); ?></h4>

< ?php the_author_meta('description'); ?>
<!--Author Info-->

Source: Line 25

Showing Popular Posts In Your Sidebar ↓

Placing these codes in your sidebar.php file will show your sites most popular posts based on the comment count for the posts.

<h2>Popular Posts</h2>
< ?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
	<li><a href="<?php echo get_permalink($postid); ?>" title="< ?php echo $title ?>">< ?php echo $title ?></a> {< ?php echo $commentcount ?>}</li>
< ?php } } ?></ul>

Source: Smashing Magazine

Displaying Ads In Your RSS Feed ↓

This code will insert advertisements into your rss feeds which can help link back to your site if you see a lot of content theives hijacking your content or if you’d just like to have another form of advertising built in (google ads, ect). Paste this code into your themes functions.php file and you’re good to go.

< ?php
function insertAds($content) {
    $content = $content.'Originally found at an <a href="http://www.spyrestudios.com">Awesome Design Blog';
    return $content;
add_filter('the_excerpt_rss', 'insertAds');
add_filter('the_content_rss', 'insertAds');

A Login Form That Returns The Visitor To The Page They Logged In From ↓

If you like having a login form on your site but get upset that the login form doesn’t return the person to the page they were currently viewing, this is the perfect snippet of code for you to have in your hack bag.

< ?php if(!is_user_logged_in()) { ?>
	<form action="<?php echo wp_login_url(get_permalink()); ?>" method="post">
		<label for="log"><input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?/>" size="22" /> User</label>
		<label for="pwd"><input type="password" name="pwd" id="pwd" size="22" /> Password</label>
		<input type="submit" name="submit" value="Send" class="button" />
		<label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label>
< ?php } ?>

Adding Breadcrumbs To Your Theme Without A Plugin ↓

Breadcrumbs are a great way to allow visitors know where they currently are in your site and also give them options to easily find more posts from the category they’re in, ect. Adding breadcrumbs to your site is done in two steps – without a plugin. The code below needs to be added into your functions.php file for your theme.

function the_breadcrumb() {
		echo '
<ul id="crumbs">';
	if (!is_home()) {
		echo '
	<li><a href="';
		echo get_option('home');
		echo '">';
		echo "</a></li>
		if (is_category() || is_single()) {
			echo '
			if (is_single()) {
				echo "</li>
				echo '</li>
		} elseif (is_page()) {
			echo '
			echo the_title();
			echo '</li>
	elseif (is_tag()) {single_tag_title();}
	elseif (is_day()) {echo"
	<li>Archive for "; the_time('F jS, Y'); echo'</li>
	elseif (is_month()) {echo"
	<li>Archive for "; the_time('F, Y'); echo'</li>
	elseif (is_year()) {echo"
	<li>Archive for "; the_time('Y'); echo'</li>
	elseif (is_author()) {echo"
	<li>Author Archive"; echo'</li>
	elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "
	<li>Blog Archives"; echo'</li>
	elseif (is_search()) {echo"
	<li>Search Results"; echo'</li>

	echo '</ul>

And the code below then needs to be added in your theme’s files where you’d like the breadcrumbs to show up (ie: single.php, page.php archive.php ect

< ?php the_breadcrumb(); ?>

Insert Google Ads With WordPress Shortcodes ↓

The code below will give you the ability to insert adsense ads anywhere in your posts. This is great for people who like to add a 300×250 or 468×60 ad block in their posts but want the option to only add it into specific posts. First, we’ll add the code below into your theme’s functions.php file.

function showads() {
    return '
<div class="adsensewrap"><script type="text/javascript"><!--
	google_ad_client = "pub-XXXXXXXXXXXXXX";
	google_ad_slot = "1234567890";
	google_ad_width = 300;
	google_ad_height = 250;

<script type="text/javascript"
add_shortcode('adsense', 'showads');

Now, when you’re writing posts, you just add in the shortcode [adsense] and you’re all set. If you want to style the ad, I’ve wrapped the advertisement in a div class “adsensewrap“.

Source: Cats Who Code

Choose The Sidebar To Use On A Post By Post Basis ↓

Being able to choose which sidebar to use on a post by post basis would be a great idea for those of us who run websites that could utilize customized content in the sidebar based on category, ect. First, you’ll need to find the code below in your single.php file (and also in your index.php, page.php, etc…).

< ?php get_sidebar(); ?>

And then replace it with this:

< ?php $sidebar = get_post_meta($post->ID, "sidebar", true);

Now when you’ll write a post, create a custom field named sidebar. As a value, give it the name of the sidebar you want to include so if you’ve built three different sidebar files (sidebar-category.php, sidebar-full.php, sidebar-awesome.php) and wanted to show the sidebar-category.php file, you’d use the key “sidebar” and value “sidebar-category“.

Source: WP Recipes

Insert Social Media Submission Links In Your Theme Without A Plugin ↓

Most wordpress site owners these days use some form of social media plugin – but what if you could create your themes with the codes already built in? Well, it’s easier than you think. Add the below codes into your single.php file and you’re good to go with links for twitter, digg, delicious, reddit and stumbleupon.

<ul class="socialwrap">
	<li><a href="http://digg.com/submit?phase=2&amp;url=<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>">Digg This Post</a></li>
	<li><a href="http://twitter.com/home/?status=Reading <?php the_title(); ?> < ?php bloginfo('home'); ?>/?p=< ?php the_ID(); ?>">Tweet This Post</a></li>
	<li><a href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>">Stumble This Post</a></li>
	<li><a href="http://delicious.com/post?url=<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>">Save on Delicious</a></li>
	<li><a href="http://www.reddit.com/submit?url=<?php the_permalink(); ?>&amp;title=< ?php the_title(); ?>">Submit to Reddit</a></li>

Your Turn To Talk

I Hope you found this post useful! Any other tricks you have in your bag? Please take a minute to share them with the rest of us! :)

Pass VCP-410 exam with highest score using our cissp prep course and get certified in days. We also provide up to date dumps for mcp certification exam.


Scroll back to the top