Adding widgets to header and footer from plugin

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:

Read More
<? 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

Related posts

Leave a Reply

1 comment

  1. 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' );
    }