Top 5 Must-Have WordPress Theme Functions

I decided to do a little “TOP 5” of the WordPress theme functions (in functions.php) I use most often…
Here we go:
[ad]

  1. Widgetizing the WordPress Theme templates:
    Notorious one, for adding widgets to the WP Theme, in functions.php:

    // some widgetized sidebar
    if ( function_exists('register_sidebar') ):
    register_sidebar( array(
    		'name' => __( 'Some Sidebar', 'theme' ),
    		'id' => 'some-sidebar',
    		'description' => __( 'Place Here Content to Appear in this Sidebar', 'theme' ),
    		'before_widget' => '<div class="moonpixel-widget">',
    		'after_widget' => '</div>',
    		'before_title' => '<div class="moonpixel-title">',
    		'after_title' => '</div>',
    	) );
    endif;

    Then in the WordPress Theme:

    <?php if ( is_active_sidebar( 'some-sidebar' ) ) : ?>
    	<?php dynamic_sidebar( 'some-sidebar' ); ?>
    <?php endif; ?>
  2. Permalinks within Posts/Pages editable content:
    I use this always for links within the Posts or Pages, it does save lots of trouble when the URL of the linked page changes:

    // permalink shortcode
    function permalink_shortcode($atts) {
    	extract(shortcode_atts(array(
    		'id' => 1,
    		'text' => "" 
        ), $atts));
     
        if ($text) {
            $url = get_permalink($id);
            return "<a href='$url'>$text</a>";
        } else {
    	   return get_permalink($id);
    	}
    }
    add_shortcode('permalink', 'permalink_shortcode');

    [ad]

    Then I can just use it as a shortcode in WordPress Posts or Pages:

    [permalink id=12] or
    [permalink id=12 text='some text to be wrapped in a link']
  3. Bloginfo function within Posts/Pages editable content:
    Another useful one is this WP bloginfo function shortcode, for cases where I need to place the Blog URL, Blog Description, theme URL (say for images in text widgets) etc, and having these dynamically updated in Posts and Pages when any changes made.

    //bloginfo shortcode
    function bloginfo_shortcode( $atts ) {
        extract(shortcode_atts(array(
            'key' => '',
        ), $atts));
        return get_bloginfo($key);
    }
    add_shortcode('bloginfo', 'bloginfo_shortcode');

    and then similarly shortcode in WordPress Posts or Pages:

    [bloginfo key='template_url']
    or any of these : http://codex.wordpress.org/Function_Reference/bloginfo
  4. Add Shortcodes to Text Widgets:
    A small one, but handy when need to use shortcodes in wordpress text widgets
    (works well with the ones mentioned above…)
    ideally placed above the shortcode functions in functions.php 🙂

    add_filter('widget_text', 'do_shortcode');
  5. Custom Menus for WordPress Theme
    Using this one more and more these days, in functions.php:

    	// register top menu
    	register_nav_menus( array(
    		'primary' => __( 'Top Menu', 'theme' ),
    	) );

    Then in the WordPress Theme:

      <?php wp_nav_menu( array( 'container_id' => 'moonpixel-top', 'menu_class' => 'sf-menu', 'theme_location' => 'primary' ) ); ?>
  • Thanks for the cool tips, will be useful for my new blog…

  • I am totally bookmarking your site for wordpress tips! Thanx for posting all the code to do this stuff too.
    Next time Katz and I talk about WordPress on Ustream, we’ll make sure to talk about Moonpixel!

    • hey StarrWulfe, glad you like it, I got loads free downloads prepared for the future…
      I am planning cite your article about connecting the social media comments
      with blog as it’s a great idea… will let you know & give credits