WordPress Icon Logo
me@grafxflow

Written by me@grafxflow

18 Mar, 2013

16

7,918

Add widget to header in WordPress

You may want to add a widget to your WordPress theme header (or any page template) but have noticed that it doesn't appear in admin (Appearance->Widgets). Luckily it's possible with the following PHP code which makes it appear in admin and on your website page. The code is also based on a custom/from scratch theme.

1. First add this to your functions.php file.

(wp-content/themes/YOURTHEME/functions.php)

// The function to group all the widget areas //
function create_widgets_init() {
    // The custom widget //
    register_sidebars( 1,
    array(
    'name' => 'Head column',
    'id' => 'head-column',
    'description' => __('The header widget area, used for placing your custom widgets.'),
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h3 class="widgettitle">',
    'after_title' => '</h3>')
    );
}

// ACTIVATE THE WIDGET(S) //
add_action( 'widgets_init', 'create_widgets_init' );

2. Then in your header.php file add. (wp-content/themes/YOURTHEME/header.php)

<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('head-column') ) : ?>

<?php endif; ?>

Add comment

16 Response

Smart Search

133 Following
50 Followers

me@grafxflow

Hull, United Kingdom

I am a Full-stack Developer who also started delving into the world of UX/UI Design a few years back. I blog and tweet to hopefully share a little bit of knowledge that can help others around the web. Thanks for stopping by!

Follow