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!






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.
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;" ) );
}
I'm in the process of learning more about wordpress and I found this very helpful. Kudos to you pal! Thanks
Thanks,again!
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.