Yoast: Building Page.ly Part4: Scalable and Fast Managed WordPress Hosting | Page.ly Blog http://t.co/ba32Hemn
![]()
WordPress functions to supercharge your Theme!
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!




by Jean-Baptiste Jung on 2 December, 2008 at 21:59
That's what I call an awesome post! Thank you for sharing your WP-knowledge :)
by archshrk on 3 December, 2008 at 00:29
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.
by wesley on 3 December, 2008 at 09:13
Doesn't removing the kses filter open you up to XSS attacks?
by Erik on 3 December, 2008 at 10:12
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.
by Alex on 4 December, 2008 at 13:30
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;" ) );
}
by Alex on 4 December, 2008 at 13:48
PS. I just realised I got that from your website haha! Proves how much I use your site ;-)
by Norhafidz on 4 December, 2008 at 23:44
Awesome post!
I'm in the process of learning more about wordpress and I found this very helpful. Kudos to you pal! Thanks
by Acan on 5 December, 2008 at 12:43
Thank you, This wp knowledge is very useful for me,
Thanks,again!
by Shanker Bakshi on 5 December, 2008 at 14:11
I have just started Tweaking WordPress Themes - Its always good to learn new things from here
by Rezki on 6 December, 2008 at 03:18
great post. i just want to know, is it safely for wordpress security?
by John Congdon on 8 December, 2008 at 19:19
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.
by John Congdon on 8 December, 2008 at 19:21
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.
by Tertius on 9 December, 2008 at 17:37
Crazy! The info your come up with is awesome. Slowly working on making my site better. Another addition.
by CJ on 10 December, 2008 at 12:40
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!
by Joost de Valk on 12 December, 2008 at 22:24
Thx, CJ!
by Joost de Valk on 12 December, 2008 at 22:24
BTW, yoast.com, now on 2.7, with threaded comments!
by John Congdon on 12 December, 2008 at 22:34
What are you using for your threaded comments?
by Joost de Valk on 12 December, 2008 at 22:55
That's actually built in in WP 2.7 :) (you'll have to adapt your theme though)
by Rich on 18 December, 2008 at 01:44
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!
by anton on 8 September, 2009 at 19:28
Thanks very much...
by Berita Harian on 15 October, 2009 at 17:49
great great post.. I've been looking for type post regarding WP and you got it here..Thanks for sharing pal