11 Useful Snippets/Hacks for your WordPress functions

Ever wondered on how to change the excerpt length, add multiple sidebars or even add shortcodes in Widgets? No? This article will answer these questions because nowadays to be efficient, a web developer has to have a toolbox with code snippets he can use over and over again. WordPress has lots of hacks that you maybe unaware of. Just read this article and discover some nice snippets that can help you in your web-development.

In this article, we have compiled 11 new WordPress hacks that you should definitely add to your library.

Removing The Meta Generator Tag

The wp head() function adds a meta tag showing which version of WordPress you’re using. For security reasons this should be disable.

// Removing the meta generator tag
remove_action('wp_head', 'wp_generator');

Changing excerpt lenght

In WordPress it is possible to change the excerpt length by simply returning the desired number of words as an integer. In this example we will output the first 50 words. We strongly recommend to create a variable an put it on top of your functions.php so you can quickly change it.

// Changing excerpt length to 50 Words
$wp_excerpt_words_length = 50;
function change_excerpt_length($length) {
   return $wp_excerpt_words_length;
add_filter('excerpt_length', 'change_excerpt_length');

Changing excerpt more

The default text appended to auto-generated excerpts is (‘[…]’). Sometimes it is more suitable to change it with something else for example a link. Here below is the code to change the excerpt more with a link.

// Changing excerpt more
function change_excerpt_more($more) {
   return '' . 'Continue reading' . '';
add_filter('excerpt_more', 'change_excerpt_more');

Integrate Custom Navigation Menus inside your WordPress Theme

Navigation menus are a one of the big new feature in version 3.0 of WordPress. If you want to support them in your new theme, or if you’re modifying an older theme for 3.0, you’ll need to add the code in Listing 6-33 to your theme files.

// Add support for menus
add_theme_support( 'nav-menus' );
// put this in your theme template where you want to display it
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) );

Add Support for Post Thumbnails (Featured Images)

Featured images were known as post thumbnails prior to version 3.0, to enable support for Features Images just add the following line to your functions.php file.

// Add support for thumbnails
add_theme_support( 'post-thumbnails' );

Custom Backgrounds and Headers in your Theme

Enabling custom backgrounds for your theme is very easy. Just add the line below to your theme’s functions.php file.

// Add support for backgrounds

Make Your Theme Widget Ready

In a modern WordPress Theme we could consider a widget ready sidebar as essential. For the end user the widgets allow to easily and quickly customize specific content on their WordPress theme. In order to do that we need to define each widget area you want in your theme. To do that you need to register an array telling WordPress which areas should be called and what code should be displayed before and after the widget block and title.
Look at the example below and add code to your functions.php file:

// Defining two widgets
function my_widgets_init() {
	// Widget 1, specifying the position of the widget is always good!
	register_sidebar( array(
		'name' => __( 'Primary Widget Area', 'twentyten' ),
		'id' => 'primary-widget-area',
		'description' => __( 'The primary widget area', 'twentyten' ),
		'before_widget' => '

  • ', 'after_widget' => '
  • ', 'before_title' => '

    ', 'after_title' => '

    ', ) ); // Widget 2, specifying the position of the widget is always good! register_sidebar( array( 'name' => __( 'Secondary Widget Area', 'twentyten' ), 'id' => 'secondary-widget-area', 'description' => __( 'The secondary widget area', 'twentyten' ), 'before_widget' => '
  • ', 'after_widget' => '
  • ', 'before_title' => '

    ', 'after_title' => '

    ', ) ); // A third Widget would be the same! Just change the 'id', 'name' and 'description' } // Register sidebars widgets by running my_widgets_init() on the widgets_init hook. add_action( 'widgets_init', 'my_widgets_init' );

    Now, all we need to do is add a conditional php statement in our sidebar (or wherever you want), like so:

    if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('primary-widget-area') ) {
    	// non widget and sidebar information go here
    if ( is_active_sidebar( 'secondary-widget-area' ) )  {
    	dynamic_sidebar( 'secondary-widget-area' );

    Now the theme is widget ready, if there are no widgets enabled, the code you entered in the first if will be displayed. Note that you need to enter the name of your widget to display it.

    How to use WordPress shortcode in theme files

    Creating shortcodes in WordPress is to create a function that prints out what you want, and then defining it as a shortcode. In your php code just put this line to print the desired shortcode.

    do_shortcode( $content )

    For more information about shortcode: http://codex.wordpress.org/Shortcode_API

    Use WordPress shortcodes in widgets

    WordPress shortcodes are bracketed placeholders you can use while editing your content that will be replaced with some other content. Here are some example of what you can do with shortcodes: add content such as galleries, rss feeds, google maps etc…

    // Enable shortcodes in widgets
    add filter('widget_text', 'shortcode_unautop');
    add filter('widget_text'', 'do_shortcode');

    Modifying Roles to allow subscribers to view private posts and pages

    Somethimes you want to give some users more rights. In this example we want to allow any logged-in user to view our private posts and pages. In order to do that we need to grant them two additional capabilities listed here below.

    // Allow subscribers to view private posts and pages
    $PrivateRole = get_role('subscriber'); $PrivateRole -> add_cap('read_private_pages');
    $PrivateRole -> add_cap('read_private_posts');

    Extending User Profiles

    WordPress user profiles include several contact fields (e-mail address, a URL, AIM, Yahoo, and Google Talk) and these built-in contact fields are not always terribly useful. It is possible to extend or remove some fields with just a few lines of code in functions file. In the following code we will add a Twitter field and remove AIM, Yahoo IM, Google Talk/Jabber.

    // Change user contact fields profile
    function change_user_profile( $contactmethods ) {
    // Add Twitter field
    $contactmethods['twitter'] = 'Twitter Name (no @)';
    // Remove AIM, Yahoo IM, Google Talk/Jabber
    return $contactmethods;
    add filter('user_contactmethods','change_user_profile',10,1);
    This entry was posted in Wordpress and tagged , , , , , , .

    10 Responses to 11 Useful Snippets/Hacks for your WordPress functions

    1. Pingback: wp-popular.com » Blog Archive » 11 Useful Snippets/Hacks for your Wordpress functions | djavupixel.com

    2. adimaryanto says:

      Great article…. i like this

    3. Jeff Byrnes says:

      There’s a typo in the last example, for adding & removing user contact bits. On line 12, it reads:

      add filter('user contactmethods',

      and should read:

      add filter('user_contactmethods',

    4. Pingback: 11 Useful Snippets/Hacks for your Wordpress functions : Speckyboy Design Magazine

    5. Pingback: Stoff für das Wochenende #8 | daswebdesignblog.de :: Artikel, Tutorials, Freebies und Interviews

    6. Ralph Thompson says:

      great post thanks

    7. byme says:

      great job bro, wordpress is the best
      glad to meet this blog
      byme. check my blog too

    Leave a Reply

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