10 Tricks For Your WordPress Functions.php File


Using your theme’s functions.php file, you can make WordPress sit up and beg and do lots of other cool tricks. We are going to show you ten things you can do today to add functionality to your WordPress site.

  1. Remove WP Version Reporting — You should do this for security reasons anyway.
    function pix_remove_version() {
    return '';
    add_filter('the_generator', 'pix_remove_version')
  2. Change WordPress Default Gravatar — Don’t forget to add your custom image to the theme and feel free to alter the image path to suit.
    add_filter( 'avatar_defaults', 'newgravatar' );
    function newgravatar ($avatar_defaults) {
    $myavatar = get_bloginfo('template_directory') . '/images/gravatar.gif';
    $avatar_defaults[$myavatar] = "Pixelita";
    return $avatar_defaults;
  3. Add Dynamic Copyright to Your Footer — This code takes the year of your first post and the year of your latest post to create a copyright date range. Paste the first code snippet into your functions.php file and the second code snippet into your footer.php file:
    function pixelita_copyright() {
    global $wpdb;
    $copyright_dates = $wpdb->get_results("
    YEAR(min(post_date_gmt)) AS firstdate,
    YEAR(max(post_date_gmt)) AS lastdate
    post_status = 'publish'
    $output = '';
    if($copyright_dates) {
    $copyright = "© " . $copyright_dates[0]->firstdate;
    if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
    $copyright .= '-' . $copyright_dates[0]->lastdate;
    $output = $copyright;
    return $output;

    Then place this code into your footer.php file:

    <?php echo pixelita_copyright(); ?>
  4. Enable Post Thumbnails — Post thumbnails are not emabled by default in WordPress 2.9 and later. You must enable them via the functions.php file. Here’s how:
    add_theme_support( 'post-thumbnails' );

    Then, place this code in your theme file (usually index.php or single.php) where you want the thumbnail to appear:

    <?php the_post_thumbnail(); ?>
  5. Add Custom Menus — WordPress 3.x adds the capability of custom navigation and other menus that can be edited right from the Appearance > Menus area in the dashboard. This is an elegant solution if you want to be sure older themes won’t break with the new code. The source for this code is an article by Nicolas Kuttler.
    function mytheme_addmenus() {
    			'main_nav' => 'The Main Menu',
    add_action( 'init', 'mytheme_addmenus' );
    function mytheme_nav() {
        if ( function_exists( 'wp_nav_menu' ) )
            wp_nav_menu( 'menu=main_nav&container_class=pagemenu&fallback_cb=mytheme_nav_fallback' );
    function mytheme_nav_fallback() {
        wp_page_menu( 'show_home=Start&menu_class=pagemenu' );

    Then, you can call your shiny new menu system from your theme’s teplate like this:

    <?php mytheme_nav(); ?>
  6. Enable Threaded Comments — While you can enable threaded comments from the WP Dashboard under Discussion, you have to add Javascript to the header.php file. Why clutter up your theme. The most efficient way to enable threaded commenting is through your functions.php file:
    // enable threaded comments
    function enable_threaded_comments(){
    if (!is_admin()) {
    if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
    add_action('get_header', 'enable_threaded_comments');
  7. Hide Login Notifications — Often key information can be exposed in an error message and an unscrupulous person could use that information to guess your username or password or otherwise gain unauthorized access to your site. To hide login error messages, simply put the following code in your functions.php file:
    add_filter('login_errors',create_function('$a', "return null;"));
  8. Change Excerpt Length — The default excerpt length is 55 characters. You can change this to whatever you like (the example uses 75 characters) by placing this code in your functions.php file:
    function new_excerpt_length($length) {
    return 75;
    add_filter('excerpt_length', 'new_excerpt_length');
  9. Add Widgetized Areas to Your Theme — I could have said, “Add Widgets to Your Sidebar,” but it’s not just for sidebars anymore. You can actually add widgets to any area, sidebar, footer, header, etc. The beauty is in naming them so you know what their function is:
    if ( function_exists('register_sidebar') )
    register_sidebar(array('name'=>'Left Sidebar'));
    register_sidebar(array('name'=>'Right Sidebar'));
    register_sidebar(array('name'=>'Left Footer'));
    register_sidebar(array('name'=>'Middle Footer'));
    register_sidebar(array('name'=>'Right Footer'));

    Then add the call to the appropriate widget area in your theme:

    <?php if ( !function_exists('dynamic_sidebar')
            || !dynamic_sidebar('Left Sidebar') ) : ?>
  10. Add Social Media Contacts to User Profiles — WordPress has built in a great way to add additional social media contact information to a user’s profile:
    function more_contactmethods( $contactmethods ) {
        $contactmethods['twitter'] = 'Twitter username';
        $contactmethods['facebook'] = 'Facebook URL';
        return $contactmethods;
    add_filter( 'user_contactmethods', 'more_contactmethods' );

Read On…

If you want to dive into the power of the functions.php file, read these excellent articles.

Joni Mueller has been designing web sites for hire since 2003, when she first blew up her web host's server by insisting on running Greymatter. Since then, Joni has designed for Blogger and Movable Type, TextPattern, WordPress and CMS Made Simple. She lives with her cat and shoe collection in a bucolic old section of Houston called Idylwood. For some strange reason, Joni likes to refer to herself in the third person. When she's not working on web design, she's ordering lawyers around. And blogging about it. Or both.

Leave a Reply

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