WordPress is well known for its plugins and themes, but not enough people know and love what you can do in your themes functions.php. Chris Pearson showed his love for them back in May, and he did a great job of explaining how you can bring your own functions.php file to each and every theme you use, so I’m not going to repeat that, just go read it.
Then Matt Varone started releasing a whole array of useful functions in three consecutive posts over the last couple of months:
- Taking Advantage of Functions.php in WordPress Themes
- Useful custom functions for WordPress
- is_subpage() – Custom Conditional Function
So I thought it was about time I started doing the same and started sharing a few of the custom functions & hacks I’m using in my functions.php.
Useful hacks
I’ll start with the hacks, basically all of them have to do with disabling default functionality in WordPress that annoyed me. The first free are to remove some output from the head of WordPress’ output:
// Clean up wp_head
// Remove Really simple discovery link
remove_action('wp_head', 'rsd_link');
// Remove Windows Live Writer link
remove_action('wp_head', 'wlwmanifest_link');
// Remove the version number
remove_action('wp_head', 'wp_generator');
The second set is to remove the curly quotes from both my own text and the comments:
// Remove curly quotes
remove_filter('the_content', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
The last hack is to allow the use of HTML in user profiles:
// Allow HTML in user profiles
remove_filter('pre_user_description', 'wp_filter_kses');
Useful custom functions
Then, of course, I have some useful custom functions for WordPress too. One of my favorites is this one, which generates a delete comment link:
function delete_comment_link($id) {
if (current_user_can('edit_post')) {
global $post;
echo '| <a href="'.admin_url("comment.php?action=cdc&c=$id&redirect_to=/".$post->post_name."/").'">del</a> ';
echo '| <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id&redirect_to=/".$post->post_name."/").'">spam</a>';
}
}
Another one I borrowed & adapted from CakePHP, it returns the time since a specified time in “human readable” fashion:
function TimeAgoInWords($from_time, $include_seconds = true) {
$to_time = time();
$mindist = round(abs($to_time - $from_time) / 60);
$secdist = round(abs($to_time - $from_time));
if ($mindist >= 0 and $mindist <= 1) {
if (!$include_seconds) {
return ($mindist == 0) ? 'less than a minute' : '1 minute';
} else {
if ($secdist >= 0 and $secdist <= 4) {
return 'less than 5 seconds';
} elseif ($secdist >= 5 and $secdist <= 9) {
return 'less than 10 seconds';
} elseif ($secdist >= 10 and $secdist <= 19) {
return 'less than 20 seconds';
} elseif ($secdist >= 20 and $secdist <= 39) {
return 'half a minute';
} elseif ($secdist >= 40 and $secdist <= 59) {
return 'less than a minute';
} else {
return '1 minute';
}
}
} elseif ($mindist >= 2 and $mindist <= 44) {
return $mindist . ' minutes';
} elseif ($mindist >= 45 and $mindist <= 89) {
return 'about 1 hour';
} elseif ($mindist >= 90 and $mindist <= 1439) {
return 'about ' . round(floatval($mindist) / 60.0) . ' hours';
} elseif ($mindist >= 1440 and $mindist <= 2879) {
return '1 day';
} elseif ($mindist >= 2880 and $mindist <= 43199) {
return 'about ' . round(floatval($mindist) / 1440) . ' days';
} elseif ($mindist >= 43200 and $mindist <= 86399) {
return 'about 1 month';
} elseif ($mindist >= 86400 and $mindist <= 525599) {
return round(floatval($mindist) / 43200) . ' months';
} elseif ($mindist >= 525600 and $mindist <= 1051199) {
return 'about 1 year';
} else {
return 'over ' . round(floatval($mindist) / 525600) . ' years';
}
}
I shared some other useful functions you could add in your functions.php in previous posts:
- Easily display your last Tweet
- Showing off your FeedBurner subscribers
- Using the Technorati API on your blog
I’ve added these 5 functions into one file for you to download: functions.txt. Enjoy, and if you have any functions to share yourself, please share!



That’s what I call an awesome post! Thank you for sharing your WP-knowledge :)
Very useful post. One request – can you explain why you use these functions. Not everyone knows about rsd, wlw, etc. so they may be hesitant to make that change even though it’s good for them.
Doesn’t removing the kses filter open you up to XSS attacks?
Also take a look at <a href=”http://www.smashingmagazine.com/2008/12/02/10-useful-rss-hacks-for-wordpress/”smashing magazines who posted several nice hacks.
One of the best is the delay in posting rss feeds. ONce and a while I hit publish to quick and must change the post. My RSS feed keeps the mistake.
Very useful post! Sometimes I forget functions.php and what it can do.
Here’s one that I can add to the list. This code removes the update warning from all users apart from username ‘admin’. You could change the username to whichever you want.
global $user_login , $user_email;
get_currentuserinfo();
if ($user_login == "admin") {
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}
PS. I just realised I got that from your website haha! Proves how much I use your site ;-)
Awesome post!
I’m in the process of learning more about wordpress and I found this very helpful. Kudos to you pal! Thanks
Thank you, This wp knowledge is very useful for me,
Thanks,again!
I have just started Tweaking WordPress Themes – Its always good to learn new things from here
great post. i just want to know, is it safely for wordpress security?
Thanks for this info. I didn’t know about the functions.php file. I have hacked around in the WP too much, when I should have been using this file. How frustratingly easy.
Rezki,
I would assume it’s as safe as any code you write. The security will be up to you on your end as the programmer. I can see it adding possible security holes for sure, but not because of WordPress.
Crazy! The info your come up with is awesome. Slowly working on making my site better. Another addition.
This is absolutely fantastic and YOU ROCK for sharing it! I’ve only just recently become serious about blogging, and had absolutely no idea what functions.php was even for. I feel like this post was written for me. Thank you!
Thx, CJ!
BTW, yoast.com, now on 2.7, with threaded comments!
What are you using for your threaded comments?
That’s actually built in in WP 2.7 :) (you’ll have to adapt your theme though)
You’ve opened my eyes to the power of functions.php! I’ve never even though of using in any themes I’ve made and have ignored it in themes I’ve edited etc. Definitely going to give it a shot and see what it can do for me now though. Cheers Joost!
Thanks very much…
great great post.. I’ve been looking for type post regarding WP and you got it here..Thanks for sharing pal