WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i created 4 header and 4 footer widgets using a site spesific plugin, the reason been that when i change themes, i keep all of my widgets. i created two files, one for the header widgets and one for the footer widgets. what i need to do now is to add these to the header and footer files.

at this stage i'm putting the following code manually in my header at the bottom after my header closing tag:

<? if ( is_front_page() ) : ?>  
        <?php   get_sidebar( 'homepage' );  ?>
        <?php endif ; ?>`

and the following code in my footer at the top:

<?php get_sidebar( 'footer1' ); ?>

How can i insert these codes automatically into my header and footer files from my plugin.

many thanks

share|improve this question
up vote 1 down vote accepted

Actually including that code in your theme template files is the only surefire way to include those widgets in various themes. It's also the best to get them where you want them.

Alternatively, you could accomplish this using hooks, but that relies on your themes actually calling the hook, and in the proper location. The safest bets are the get_header and get_footer hooks, which are called just before fetching the footer.php and header.php files, respectively.

add_action( 'get_header', 'header_widgets' );
function header_widgets() {
    if( is_front_page() ) {
        get_sidebar( 'homepage' );
add_action( 'get_footer', 'footer_widgets' );
function footer_widgets() {
    get_sidebar( 'footer1' );
share|improve this answer
hi thanks for your awnser, but i keep on getting a parse error. – Pieter Goosen Apr 16 '13 at 16:54
@PieterGoosen whoops! Been neck-deep in bash, which doesn't need semicolons. Try it now. – Stephen M. Harris Apr 16 '13 at 17:08
Great, that worked perfectly. If i put this code in a plugin and move the two sidebar templates into a plugin, how would i edit the code to make it work. one thing i forget to say is that i'm doing all this in a site specific plugin – Pieter Goosen Apr 16 '13 at 17:29
@PieterGoosen Well, the widgets are managed from the Dashboard, so all you need to do code-wise is register the sidebars and perhaps add your widget css. You can use a css file from your plugin like this: wp_enqueue_style( 'headfootwidgets', plugin_url(basename(__DIR__).'/headfootwidgets.css') ) – Stephen M. Harris Apr 16 '13 at 17:40
thanks. you were a great help – Pieter Goosen Apr 16 '13 at 18:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.