Top 10 Functions for functions.php

Development

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
    add_filter('gallery_style',
        create_function(
            '$css',
            '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
    remove_shortcode('gallery');
    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' );

3 responses to “Top 10 Functions for functions.php

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

    1. Ah, no you’re right. I’ve updated the post now – cheers!

      1. You only half fixed it…

Leave a Reply

Your email address will not be published. Required fields are marked *