Top WordPress Functions for your functions.php File

Published 26th August 2009

I’ve recently been looking at the kind of functions that I regularly see in WordPress theme’s functions.php file. Here are some of the ones that I would recommend you include in your WordPress functions.php file:

Plain Text Feedburner Subscriber Count

Here is the classic: get your feedburner subscriber count in plain text. Note most of these functions make use of cURL and this one requires PHP v5+ (so you can use SimpleXMLElement).

function get_subscriber_count() { $id = "YourFeedId"; $url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=". $id; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); $xml = new SimpleXMLElement($data); return $xml->feed->entry['circulation']; }

List Popular Posts

This is a good one courtesy of WooThemes. This function will output a list of your recently popular posts based on the number of comments. Note that you can change the number of links output by changing the LIMIT statement and the end of the query.

function get_popular_posts() { global $wpdb; $now = gmdate("Y-m-d H:i:s",time()); $lastmonth = gmdate("Y-m-d H:i:s",gmmktime(date("H"), date("i"), date("s"), date("m")-12,date("d"),date("Y"))); $popularposts = "SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS 'stammy' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish' AND post_date '$lastmonth' AND comment_status = 'open' GROUP BY $wpdb->comments.comment_post_ID ORDER BY stammy DESC LIMIT 6"; $posts = $wpdb->get_results($popularposts); $popular = ''; if($posts){ foreach($posts as $post){ $post_title = stripslashes($post->post_title); $guid = get_permalink($post->ID); $popular .= '<li><a href="'.$guid.'" title="'.$post_title.'">'.$post_title.'</a></li>'; $i++; } } echo $popular; }

Get TinyURL

This one can be useful if you want to generate a short URL of your post for people to share, or to include in a “Tweet This” link.

function get_tiny_url($url) { if (function_exists('curl_init')) { $url = 'http://tinyurl.com/api-create.php?url=' . $url; $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $tinyurl = curl_exec($ch); curl_close($ch); return $tinyurl; } else { //cURL disabled on server; Return long URL instead. return $url; } }

Get Ping/Trackback Count

Here is an interesting one I used recently. This function returns the number of pings/trackbacks for a post. This can be useful if you only want to show a certain section if there are any pings/trackbacks.

function pings_count($post_id) { global $wpdb; $count = "SELECT COUNT(*) FROM $wpdb->comments WHERE (comment_type = 'pingback' OR comment_type = 'trackback') AND comment_post_ID = '$post_id'"; return $wpdb->get_var($count); }

Conclusion

So there you have it. Functions that should be in every WordPress theme’s functions.php file. If there are any you know of that I haven’t mentioned here then please let me know.