Useful Hacks to Make WordPress Work Better

Written by Jeff Orloff on 09 Aug 2013
19,306 Views • WordPress

If you came here looking for ways to break into someone’s WordPress site, you choose the wrong definition of the word hack. No, this article is all about ways to make WordPress work better for you and your visitors by getting your hands dirty with WordPress’ actual code, you know the functions.php files, .htaccess and all that good stuff.

Now some of you might be wondering, “Why would I want to mess with all that code if I could just install a plugin to do the same thing?” And I will agree, that might seem like a valid point. Plugins are great, they make WordPress do some amazing things that someone without any development skills would never be able to accomplish. But plugins also can have some negative effects on your site:

  • Plugins can contain vulnerabilities that malicious hackers can exploit to gain access to your site and do just about whatever they please.
  • Plugins can severely slow down your site, thus having a negative effect on your SEO efforts.
  • Plugins can cause your site to break making it look ugly.
  • People who develop plugins sometimes forget about them so they stopping working properly when WordPress is updated.

Now, I am not saying don’t use any plugins. But if you can do the same thing without one, you will be better off in the long run. So, if you are ready to start making some changes, let’s jump right in.

Dealing with Content

Give your posts a shelf life

If you have been running your blog for a while then you surely have some posts that are expired that you don’t want displayed in the loop. You could unpublish them or delete them, but if they still offer your readers some value why get rid of them. Instead, take them out of the loop by replacing the WordPress Loop in your index.php file with this code:

<?php 
if (have_posts()) : 
	while (have_posts()) : the_post(); ?> 
		$expirationtime = get_post_custom_values('expiration'); 
		if (is_array($expirationtime)) { 
			$expirestring = implode($expirationtime); 
		}  
	
		$secondsbetween = strtotime($expirestring)-time(); 
		
		if ( $secondsbetween > 0 ) { 
			the_title(); 
			the_excerpt(); 
		} 
	endwhile; 
endif; 
?> 

Now you can use custom fields when you write your posts by selecting the key expiration. Set the expiration date in the following format: mm/dd/yyyy 00:00:00.

The WordPress Loop can be found between these lines of code:

<php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

and

<?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>

 

Display someone’s RSS feed on your blog

There are many reasons to display another blog’s feed on your site. It may be a related blog, or a partner site; or you just might be looking to supplement your site with some added content. Regardless of why you would need to use another RSS feed it can be done rather quickly by adding the following code to where you want the feed to be displayed:

<?php include_once(ABSPATH.WPINC.'/rss.php'); wp_rss(‘PUT URL OF FEED HERE', 5); ?>

Yes, it was just that easy.

 

Keep your most sticky posts together

Sticky posts are ones that you have featured on your site. They are generally the best your blog has to offer, so why not keep them in one place for your readers to access. Place the following code where in your theme you would want your posts to be displayed:

<?php 
$sticky = get_option('sticky_posts'); 
rsort( $sticky ); 
$sticky = array_slice( $sticky, 0, 5); 
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );  

if (have_posts()) : 
	while (have_posts()) : the_post(); 
		the_title(); 
		the_excerpt(); 
	endwhile; 
endif;  
?>

As it is written, you will display the five most recent sticky posts. Change the number accordingly if you want more or less.

 

Get More RSS Subscribers

Getting more subscribers could be as easy as reminding your readers. Place this snippet of code right above the Loop and anyone who clicks your previous entries link will see a message making it easy for them to subscribe.

<?php if(is_paged()){ ?>
	<div id="rss-remind">
		<p>Subscribe to <?php bloginfo('name'); ?> and never miss a thing:<br />
			<a href="<?php bloginfo('rss2_url'); ?>">All Posts<span> (RSS)</span></a> | 
			<a href="<?php bloginfo('comments_rss2_url'); ?>">All Comments<span> (RSS)</span></a>
		</p>
	</div><!-- #rss-remind -->
<?php } ?>

Dealing with comments

Highlight the author’s response

If your blog has a lot of comments, that can be a good thing. What can be even better is if you were to set the author’s comments and responses apart from the rest with a different background color.

To do this, first change the style.css by adding this (you can change the color to whatever you like):

.authcomment {     
	background-color: #B3FFCC !important; 
}

Now, add this to the original code for the article:

<?php         
	$oddcomment = '';
	$authID = get_the_author_meta('ID');
	if($authID == $comment->user_id) $oddcomment = 'authcomment';

?>
<li class="commentcontainer <?php	echo $oddcomment; ?>" id=”comment-<?php comment_ID(); ?>”>
...
</li>

 

Change the Mystery Man

This one is great for building your brand. When users register or comment and don’t use their own gravatar, usually the Mystery Man shows up. But he is kind of boring so instead, give your visitors the option of using your logo or some other cool gravatar by adding this code to your functions.php file (if you don’t have this file you can create it):

add_filter( ‘avatar_defaults’, ‘newgravatar’ );  

function newgravatar ($avatar_defaults) { 
	$myavatar = get_bloginfo(‘template_directory’) . ‘/images/imagefile.extension’; 
	$avatar_defaults[$myavatar] = "NAME OF YOUR GRAVATAR"; 
	return $avatar_defaults; 
}

You will need to change imagefile.extension to the image you wish to use and NAME OF YOUR GRAVATAR to whatever it is you wish to call it.

 

Show off the Most Commented Posts

The posts with the most comments are generally your more popular content. If you want to really captivate your readers, make it easy for them to see which post is really driving the conversation with this hack. All you need to do is place the following code wherever you want your list of posts to be displayed (footer, sidebar, etc.).

<?php 
$popular = new WP_Query('orderby=comment_count&amp;posts_per_page=5');
while ($popular->have_posts()) : $popular->the_post(); 
	$justanimage = get_post_meta($post->ID, 'Image', true);
	
	if ($justanimage) { 
?>
	<img data-src="<?php echo get_post_meta($post-/>ID, "Image", true); ?>" alt="<?php the_title(); ?>" />
<?php } else { ?>
	<img data-src="http://URL-TO-YOUR-THUMBNAIL.jpg" alt="" />
<?php } ?>
	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>

The URL to your thumbnail should be changed to the one you uploaded to your site.

I hope you find these hacks useful. Please feel free to share any others that you may know of with the rest of our readers.

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

5 comments
sankha 11 years ago
nice post!!
Reply
Bucur 11 years ago
Thanks Jeff for these codes,last one help me...
Reply
AviaT- 11 years ago
Very handy little quick tips. I'll be sure to implement some of these or a quick variation on my next site update.

Cheers!
Reply
Efren martinez 11 years ago
Very helpful man ! Thanks a lot ! :D
Reply
Webdesignersblog 11 years ago
Useful Tips.. Thanks for sharing Jeff ... Ill use Highlight the author’s response and Show off the Most Commented Posts in my blog...
Reply