Top 10 Functions for functions.php

I thought I’d share some of the functions that go into most of our custom WordPress themes. If you have your own favourites then get commenting!

  1. Removing the auto capitalisation of ‘WordPress’

    When version  3.0 was released a fairly simple filter appeared called capital_P_dangit. This was committed to the core by Matt Mullenweg for the purposes of brand reinforcement. Suffice to say many in the community weren’t thrilled by this passive-aggressive pedantry for a multitude of very good reasons but that’s another story (you can read all about it in Justin Tadlock’s article on the subject).

    // remove capital_P_dangit
    foreach( array( 'the_content', 'the_title', 'comment_text' ) as $filter )
        remove_filter( $filter, 'capital_P_dangit' );
  2. Change excerpt length

    The default excerpts in WordPress are reasonable but rarely fit with the design you are working to. Alter the number of characters returned in an excerpt with the following snippet.

    function theme_excerpt_length( $length ) {
        return 80; // 80 words long
    add_filter('excerpt_length', 'theme_excerpt_length');
  3. Change the excerpt truncation text

    You can alter the text that appears at the point of truncation, ie. ‘[...]‘, to anything you want, including a useful link to the post page.

    function theme_excerpt_more( $more ) {
        global $post;
        return '&hellip; <a class="read-more" href="'. get_permalink($post->ID) . '">' . __('Continue reading') . '</a>';
    add_filter('excerpt_more', 'theme_excerpt_more');
  4. Adding contact methods to the user profile

    On every recent project we have had there has been a need for extra user data, such as Twitter username, FaceBook URL and so on. The user profile screen can be edited using a combination of action hooks but WordPress also has a very simple way to add extra contact methods.

    function more_contactmethods( $contactmethods ) {
        $contactmethods['twitter'] = 'Twitter username';
        $contactmethods['facebook'] = 'Facebook URL';
        return $contactmethods;
    add_filter( 'user_contactmethods', 'more_contactmethods' );
  5. Removing the inline stylesheet from galleries

    By default WordPress spits out an inline stylesheet whenever the gallery shortcode is used within a post. The reasoning is that users can control the number of columns each gallery has and the sizes of the images within them as well as a few other options. While it is a noble quest to give users more power over their images it also allows them to screw things up in new and exciting ways when using a custom theme. Adding style tags within the body of an HTML document is also invalid markup.

    Thanks to Safirul Alredha for this code:

    // remove gallery shortcode styling
            'return preg_replace("#<style type=\'text/css\'>(.*?)</style>#s", "", $css);'
  6. Replacing the gallery shortcode handler

    While the above snippet does a good enough job in many cases I still find the built in gallery shortcode function to be questionable. For example you can change what HTML tags the gallery outputs with (the default being a semantically poor series of definition lists) but there is no way to set theme defaults without parsing post/page content and adding the attributes to the shortcode programmatically. This is a bad idea for general use themes because it will screw up peoples sites if they switch to another theme in future.

    The shortcode can be rescued however by simply removing it and replacing it with your own callback:

    // replace gallery shortcode
    add_shortcode('gallery', 'theme_gallery_shortcode');
    function theme_gallery_shortcode( $attr ) {
        global $post, $wp_locale;
        // create your own gallery output...

    You can alternatively use the built in shortcode’s internal filter ‘post_gallery’ to change the output and handling of the standard shortcode attributes.

  7. Adding custom post types to search results

    Hopefully any custom post types that are set as publicly queryable will be included in the search results by default in future releases of WordPress but for now you can use the following code:

    function search_post_types( $query ) {
        if ( $query->is_search )
            $query->set( 'post_type', get_post_types( array( 'publicly_queryable' => 1 ) ) );
        return $query;
    add_filter( 'the_search_query', 'search_post_types' );
  8. Adding custom post types to the main RSS feed

    This can make sense for some sites if you want an amalgamated feed of all your content. We use an array filter in this case to get only those post types that have taxonomies so page-like content is ignored.

    function feed_post_types( $vars ) {
        if ( isset($vars['feed']) && !isset($vars['post_type']) )
            $vars['post_type'] = array_filter( get_post_types( array( 'publicly_queryable' => 1 ) ), 'get_object_taxonomies' );
        return $vars;
    add_filter( 'request', 'feed_post_types' );
  9. Enable post thumbnails

    This is one is pretty straightforward. Make use of WordPress 3.0′s post thumbnail feature in your theme.

    add_theme_support( 'post-thumbnails' );
  10. Enable shortcodes in the text widget

    This little trick makes the text widget into a much more powerful tool. It’s used on this site to add an author list to the widget space in the footer, avoiding the need to code a specific author list widget.

    add_filter( 'widget_text', 'do_shortcode' );
  • Shaun Bent 31 / May / 2012 at 8:17 pm

    I think the filter ‘excerpt_length’ sets the number of words returned not characters returned. (I think, might be wrong)

  • Fredrik 16 / Oct / 2011 at 12:38 am

    Gash! I’ve been baning my head to a wall, trying to add a custom option to the built in gallery-shortcode for compatibility issues that you mention. You are the first one that points out that add_filter() just deals with the built in options and the simple solution is just to remove the built in code and then add it again.. so thanks for for showing this obvious and simple solution, that I just was too stupid to figure out myself! :)

    • Robert O'Rourke
      Robert O'Rourke 17 / Oct / 2011 at 11:03 am

      Ha! No worries. You can get very creative with modifiying the behaviour of wordpress using the hooks and filters. Always remember if you really have to you can remove them and start again.

  • Robert O'Rourke
    Robert O'Rourke 28 / Feb / 2011 at 4:20 pm

    Another one for the list:

    // prev next titles
    function icit_adjacent_link_title( $format, $link ) {
        return preg_replace( "/\>([^\< ]+)\<\//", ' title="$1">$1
  • [...] Nun ich bin ziemlich k.o., daher nur kurz, die Nokia Plan B Leute haben aufgegeben am Stuhl von Mr. Elop zu sägen, wer einen MSDN oder TechNet Zugang hat der kann schon das SP1 für Windows 7 saugen und in Sachen PartedMagic gibts Version 5.1. Wer bei seinem WordPress bissel was tunen will, hier gibts 10 Funktionen für functions.php von WordPress. [...]

  • [...] Top 10 Functions for functions.php ob es sich hierbei wirklich um die Top-10 Funktionen handelt, muss jeder für sich selbst entscheiden. Einige sind auf jeden Fall interessant [...]

  • Mark Wilkinson 12 / Feb / 2011 at 7:55 pm

    I think when custom post types came out we all thought they would open up new directions for WordPress. They still have their uses but most of the sites that I have used them on I have either gone back to using regular posts with categories or I wish I hadn’t bothered. They have potential but certainly need a lot of tweaks to get them to play correctly.

    • David Coveney
      David Coveney 17 / Feb / 2011 at 10:16 am

      It’s way too easy to get carried away with custom post types when a custom taxonomy and some extra fields will do the job just as well.

      But they do have their uses, and we’ve found them useful with complex data.

  • Robert O'Rourke
    Robert O'Rourke 11 / Feb / 2011 at 11:27 am

    Thanks Kimb, we’ve got quite a few more but they’re a bit lengthy for a simple top 10!

  • mkjones 11 / Feb / 2011 at 10:13 am

    Shame custom post types need so many little tweaks here and there to get them fully integrated.

    Great list, keep em’ commin :)

  • [...] This post was mentioned on Twitter by David Coveney, Robert O 'Rourke,, Marco Galasso, Mark Wilkinson and others. Mark Wilkinson said: RT @davecoveney: Top ten WordPress code snippets we use at interconnect/it, by @sanchothefat – What's your favourite? [...]