WordPress functions to supercharge your Theme!

Supercharge your WordPress themeWordPress 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:

  1. Taking Advantage of Functions.php in WordPress Themes
  2. Useful custom functions for WordPress
  3. 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:

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!

Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

21 Responses

  1. Jean-Baptiste JungBy Jean-Baptiste Jung on 2 December, 2008

    That’s what I call an awesome post! Thank you for sharing your WP-knowledge :)

  2. archshrkBy archshrk on 3 December, 2008

    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.

  3. wesleyBy wesley on 3 December, 2008

    Doesn’t removing the kses filter open you up to XSS attacks?

  4. ErikBy Erik on 3 December, 2008

    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.

  5. AlexBy Alex on 4 December, 2008

    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;" ) );
    }

  6. AlexBy Alex on 4 December, 2008

    PS. I just realised I got that from your website haha! Proves how much I use your site ;-)

  7. NorhafidzBy Norhafidz on 4 December, 2008

    Awesome post!

    I’m in the process of learning more about wordpress and I found this very helpful. Kudos to you pal! Thanks

  8. AcanBy Acan on 5 December, 2008

    Thank you, This wp knowledge is very useful for me,
    Thanks,again!

  9. Shanker BakshiBy Shanker Bakshi on 5 December, 2008

    I have just started Tweaking WordPress Themes – Its always good to learn new things from here

  10. RezkiBy Rezki on 6 December, 2008

    great post. i just want to know, is it safely for wordpress security?

  11. John CongdonBy John Congdon on 8 December, 2008

    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.

  12. John CongdonBy John Congdon on 8 December, 2008

    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.

  13. TertiusBy Tertius on 9 December, 2008

    Crazy! The info your come up with is awesome. Slowly working on making my site better. Another addition.

  14. CJBy CJ on 10 December, 2008

    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!

  15. Joost de ValkBy Joost de Valk on 12 December, 2008

    BTW, yoast.com, now on 2.7, with threaded comments!

    • John CongdonBy John Congdon on 12 December, 2008

      What are you using for your threaded comments?

      • Joost de ValkBy Joost de Valk on 12 December, 2008

        That’s actually built in in WP 2.7 :) (you’ll have to adapt your theme though)

  16. RichBy Rich on 18 December, 2008

    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!

  17. antonBy anton on 8 September, 2009

    Thanks very much…

  18. Berita HarianBy Berita Harian on 15 October, 2009

    great great post.. I’ve been looking for type post regarding WP and you got it here..Thanks for sharing pal