With so many websites being powered by my favourite CMS WordPress, the majority all function in the same way. But today I will show you a selection of code snippets that will boost functionality on your WordPress powered website.

If you are having any issues getting any of these functions working, just drop a comment below and we will be happy to help.








Random Post

Whilst working on Inspired, our up-coming inspiration gallery, one of our features was for you to browse random posts. To do this a simple query is required.

<?php
$random = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY rand() LIMIT 1");
echo '<a href="'.$random.'">Random</a>';
?>

Separate Trackbacks/Pingbacks From User Comments

By default, trackbacks and pingbacks would be displayed within user comments, this should be inserted in comments.php in your themes folder.

Find the following:

<ol class="commentlist">
		<?php wp_list_comments();
                ?>
	</ol>

Add the below after it:

<h1>Trackbacks</h1>
<ul>
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type != 'comment') { ?>
<li><?php comment_author_link() ?></li>

<?php } ?>

<?php endforeach; ?>

Use TweetMeme without a Plugin

It’s always nice to use a very important feature without the need of a plugin.

<script type="text/javascript">
tweetmeme_style = 'compact';
tweetmeme_service = 'bit.ly';
tweetmeme_source = 'twitterusername';
tweetmeme_url = '<?php the_permalink() ?>'
</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>

Display Information About Your Authors

You see on nearly on creative and designer blogs/magazine information about the author of the post. All you need to do is update your profile in the WordPress admin panel and it will feed directly into the post.

The CSS

#author-wrapper {
margin:0 30px 10px;
overflow:hidden;
}
#author-wrapper h2 {
margin-bottom:20px;
}
#author-wrapper .avatar {
float:left;
margin-right:20px;
padding:5px;
}

The HTML/PHP

This will need to go in single.php in your themes root directory.

<div id="author-wrapper">
	<a href="<?php the_author_meta('user_url'); ?>"><?php echo get_avatar( get_the_author_meta('user_email'), '80', '' ); ?></a>
	<p><?php the_author_meta('description'); ?></p>
        <p>See all posts by  <?php the_author_posts_link(); ?> </p>
</div>

Display The Number Of RSS Subscribers For Your Blog

It’s always nice to show your readers how popular your blog is, so to show off how many RSS feed subscribers you have use the following code. This is most effective as a sidebar widget.

<?php
//Where it says YourFeedburnerID, replace with the name of your feed. For example ours is nenunocreative

$url = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=YourFeedburnerID";

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
 echo $data;
 //filters unwanted characters
 preg_match('/circulation=\"([0-9]+)\"/',$data, $matches);
 if ($matches[1] != 0) {
 $rsscount = $matches[1];
 }
}
?>

 <?php
 //Prints the number of Feedburner Subscribers
 echo "$rsscount";
 ?>

</a>

Get The WordPress Slug

This maybe have little to no use, but when I had to style CSS for certain WordPress pages this became a must. This needs to be in the WordPress Loop.

<?php $slug = basename(get_permalink());
echo "$slug";
 ?>

Add Your Content To Social Networks Without A Plugin

It’s always nice to have an easy way to submit your posts and resources to large social networks and this snippet of code will make life easier. Add in single.php under your content:

The CSS

#social-wrapper {
margin:0 30px 10px;
overflow:hidden;
}
#social-wrapper h2 {
margin-bottom:20px;
}
#social-wrapper ul li {
float:left;
margin-right:10px;
list-style:none;
}
#social-wrapper ul li a:hover {
opacity: .7;
}

The HTML/PHP

Be sure to check out 30 Fantastic Social Bookmark Sets For Bloggers for a large resource of social bookmarking icons.

<div id="social-wrapper">
	<ul>

             <li><a target="_blank" rel="nofollow" title="Bookmark at Twitter" href="http://api.tweetmeme.com/share?url=<?php the_permalink(); ?>&amp;source=yourtwitterusername"><img alt="Twitter" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/images/Twitter_64.png"></a> </li></li>

        <li><a target="_blank" rel="nofollow" title="Bookmark at Design Bump" href="http://designbump.com/submit?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>"><img alt="Design Bump" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a> </li>

       <li> <a target="_blank" rel="nofollow" title="Bookmark at Web Blend" href="http://thewebblend.com/node/add/drigg/?url=<?php the_permalink(); ?>"><img alt="Web Blend" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a></li>

        <li> <a target="_blank" rel="nofollow" title="Bookmark at Facebook" href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&amp;src=sp"><img alt="Facebook" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a> </li>

        <li> <a target="_blank" rel="nofollow" title="Bookmark at Design Float" href="http://www.designfloat.com/pliggit.php?url=<?php the_permalink(); ?>"><img alt="DesignFloat" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a></li>

         <li> <a target="_blank" rel="nofollow" title="Bookmark at Reddit" href="http://reddit.com/submit?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>"><img alt="Reddit" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/images/Reddit_64.png"></a> </li>

         <li> <a target="_blank" rel="nofollow" title="Bookmark at Delicious" href="http://del.icio.us/post?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>"><img alt="Delicious" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a> </li>

          <li> <a target="_blank" rel="nofollow" title="Bookmark at StumbleUpon" href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>"><img alt="Stumble" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a> </li>

           <li><a target="_blank" rel="nofollow" title="Energize It!" href="http://www.designgizer.com/submit?url=<?php the_permalink();?>&title=<?php the_title(); ?>"><img alt="Icon" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a></li>

 <li><a target="_blank" rel="nofollow" title="Poke It!" href="http://www.designpoke.com/submit.php?url=<?php the_permalink();?>"><img alt="Icon" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a></li>

   <li><a target="_blank" rel="nofollow" title="Bookmark at Digg" href="http://digg.com/submit?phase=2&amp;url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>"><img alt="Digg It" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/path/to/icons></a></li>

            <li> <a target="_blank" rel="nofollow" title="RSS Feed" href="http://feeds.feedburner.com/yourFeed"><img alt="RSS" width="32px" height="32px" src="<?php bloginfo('template_directory') ?>/path/to/icons"></a></li>
           </ul>
            </div>

Be sure to check back next week as I continue this series with more useful WordPress code snippets that will help boost your websites functionality and once again, if you have any queries about the above snippets please leave a comment below!