Same header and footer in admin across all sites in network

Hi WPMU!

I want to display the main site header and footer in the admin of all of our subsites, but I don't know how it can be done.
I have read a post on stack exchange: http://wordpress.stackexchange.com/questions/231790/same-header-footer-in-admin-across-all-network-sites-in-multisite and the user says he made it work, but I can't find out how to create it.

Can you help me?

    Mahlamusa

    Hello Mathias,

    I hope you are having a great day so far.

    I had a look at the thread that you linked to and have found a way to try and make a simple guide for you that will get you started. Basically, what you need to do is create a simple plugin that will grab the theme's header and footer and display it in your admin.

    To make it easier for you to get started, I have created a plugin for you that will get the header of the parent site and display it in admin page. Please note that this is just the bare bones of what you should do. You will additionally have to remove some elements of the admin if you do not want them showing on your admin after this change.

    Here is the plugins code.

    <?php
    /**
     * Plugin Name: Admin Header and Footer
     */
    
     add_filter( 'show_admin_bar', '__return_false' );
     add_action( 'wp_before_admin_bar_render', 'get_theme_header' ); //can also use wp_after_admin_bar_render
     add_action( 'admin_footer', 'get_theme_footer' );
     //add_filter( 'admin_footer_text', 'get_theme_footer'); //can also hook to admin_footer_text
    
    function get_theme_header($adminbar){
    	global $wp_admin_bar;
    
    	$wp_admin_bar->remove_menu('wp-logo'); //sample remove the wordpress logo
    
    	/*Get the contents of the parent site theme's header*/
    	ob_start();
    	$file = file_get_contents($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/twentytwelve/header.php");
    	$content = eval("?> $file");
    
    	echo $content;
    }
    
    function get_theme_footer () {
    	ob_start();
    	$file = file_get_contents($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/twentytwelve/footer.php");
    
    	$content = eval("?> $file");
    
    	echo $content;/**/
    	ob_end_flush();
    }

    You will need to create a folder inside of '/wp-content/plugins' like and inside that folder paste the code above then go to your wordpress network admin and "Network Activate" the plugin, it will be called 'Admin Header and Footer' if you pasted the code as is.

    Aleternatively, download the attached 'admin-footer-and-header.zip', install it via "My Sites > Network Admin > Plugins > Add New" and choose to upload the downloaded file. After installing network activate.

    Please note that in my example, I am using the "Twenty Twelve" theme that is why you will see the name 'twentytwelve' in the code, so you can replace that with the folder name of your theme.

    In the post you linked to, the author also mentions doing this for the front end where sub sites have the same header and footer. You can achieve that by the use of a child theme. Please download and install the theme 'sameheaderfooter.zip' attached, activate it in your sub sites and it should get the header and footer of the parent site and display it on the sub sites.

    Please note that the sample code provided is only to serve as a foundation for what you want to achieve. You should research and add to the code to have a full solution for your needs.

    I hope you find this useful. Please enjoy the rest of your day further.

    Cheers,
    Mahlamusa

        Mahlamusa

        Hi Mathias,

        I have included included two files, the one you should use for adding the footer and header of the main site to the sub sites admins is the plugin named 'admin-footer-and-header.zip'. Please download that and install it as a plugin on your network admin "My Sites > Network Admin > Plugins > Add New", upload and network activate the plugin and go to any sub site's admin and you will see the header and footer of the main site embedded in the admin page.

        Please note that I have used the 'twentytwelve' theme on the code and so you should use that to test or change the code to call your main site's theme instead of the one I used.

        After activating the plugin you may find the dashboard does it look as you want it to, in that case you will have to add some custom styles to change how it looks or hide certain admin elements. As an example I have added a line to remove the logo from the admin bar, you can do more than that in your final implementation.

        Please let me know if the plugin works or not but I have tested it on my development environment and it works as it should.

        Cheers,
        Mahlamusa

    Mahlamusa

    Hello Mathias,

    I am sorry it is not working as it should for your theme. I know the Bridge theme, it uses a lot of variables in the header, those variables may have not loaded their data when you call the 'get_theme_header' function. I suggest you try to initialize WordPress inside the function 'get_theme_header' which calls the admin header.

    Try to insert the following code at the top of the function 'get_theme_header', this should make WordPress actions and hooks available in the header. I have not tried to do this on WordPress admin but it works fine on front end.

    After the line 'ob_start();' add the following:

    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');

    Then try again to see what you get. If nothing works, you should try including the parent theme's 'functions.php' which contains most of the functions used by the variables to load data into the header.

    Please note that I am not in a position to give full working code but only to guide you and help you get started.

    I hope this helps.

    Cheers,
    Mahlamusa

    Mathias

    I tried changing the code to this:

    <?php
    /**
     * Plugin Name: Admin Header and Footer
     */
    
     add_filter( 'show_admin_bar', '__return_false' );
     add_action( 'wp_before_admin_bar_render', 'get_theme_header' ); //can also use wp_after_admin_bar_render
     add_action( 'admin_footer', 'get_theme_footer' );
     //add_filter( 'admin_footer_text', 'get_theme_footer'); //can also hook to admin_footer_text
    
    function get_theme_header($adminbar){
    	global $wp_admin_bar;
    
    	$wp_admin_bar->remove_menu('wp-logo'); //sample remove the wordpress logo
    
    	/*Get the contents of the parent site theme's header*/
    	ob_start();
    	define('WP_USE_THEMES', false);
    	require('./wp-blog-header.php');
    	$file = file_get_contents($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/bloggerspoint/header.php");
    	$content = eval("?> $file");
    
    	echo $content;
    }
    
    function get_theme_footer () {
    	ob_start();
    	$file = file_get_contents($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/bloggerspoint/footer.php");
    
    	$content = eval("?> $file");
    
    	echo $content;/**/
    	ob_end_flush();
    }

    Can you confirm that it is correct?
    It didn't work either for me, and the admin is still empty, as you can see on the attached screenshot. How can I include the functions.php file?

    I really appreciate you help on this!

    Mahlamusa

    Hello Mathias,

    Thank you for your question.

    Would you please send me a copy of your theme 'bloggerspoint' so I can have a look at it. Please upload it to dropbox or a similar service and share the link. I will have a look at your theme and see how it works.

    Please note that I will not go into much details but will just get the basics and give you suggestions.

    I will wait for your reply.

    Cheers,
    Mahlamusa

    Mahlamusa

    Hello Mathias,

    I am very sorry for the delay on my side.

    I have seen your theme, downloaded it and tried to play with it using the code I sent to you. The code as it is works fine on the main admin but on the sub site admin, some more coding has to be done to get it to work. I have tried to do it but unfortunately it is a lot of work to do and it is beyond the purpose of this support forum.

    If you can't achieve this on your own, then you should post a job on the Jobs and Pros page, please note that no WPMUDEV staff is allowed to work or take a job posted there, so you will be hiring independent professionals or businesses.

    To give you a guide of what needs to be done, you have to do the following:

    1. Copy the contents of the file '/themes/boggerspoint/theme-includes.ptp' and paste it inside the plugin possible above the function 'get_theme_header' or create a function.
    2. Then move the lines that begin with 'define(' from the code and paste it above this function.
    3. Rewrite the 'include_once(' lines and make them use the '$file' variable instead, so the line :
    include_once('framework/qode-framework.php');
    becomes
    $file .= file_get_contents($_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/bloggerspoint/framework/qode-framework.php');

    So finally you should have the file look like this:

    define('QODE_ROOT', get_template_directory_uri());
    define('QODE_ROOT_DIR', get_template_directory());
    define('QODE_VAR_PREFIX', 'qode_');
    define('QODE_FRAMEWORK_ROOT', get_template_directory_uri().'/framework');
    define('QODE_FRAMEWORK_ROOT_DIR', get_template_directory().'/framework');
    define('QODE_SHORTCODES_ROOT_DIR', get_template_directory().'/wp-content/themes/bloggerspoint/shortcodes/shortcode-elements');
    function theme_includes(){
      ob_start();
       $file .= file_get_contents(ABSPATH.'/wp-load.php');
        $file .= file_get_contents(ABSPATH.'/wp-blog-header.php');
    //all other lines
    }

    Then once you have done this, you can call this function in the 'get_theme_header' and 'get_theme_footer' functions as follows:

    function get_theme_header($adminbar){
    	global $wp_admin_bar;
    	$wp_admin_bar->remove_menu('wp-logo'); //sample remove the wordpress logo
    	ob_start();
            $file .= theme_includes();
            $file .= file_get_contents($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/bloggerspoint/header.php");
           $content = eval('$&gt'. $file);
           echo $content;
    }

    From here your code is almost ready but not complete. This is where the work begins, if you test the code at this point you should see an error about an unexpected '<' or '>' in the 'eval()' function, this is because the files contain a lot of '<?php' and '?>' tags in the body, in this case those may have to be removed or replaced with another method like concatenation instead of echoing. Or you may find an alternative method instead of using the eval() function.

    If you go with the eval() function, you may have to do these changes in a child theme for the theme-includes files.

    As you have seen, the initial code I sent to you works fine but for your specific theme you have to do all the extra work to get it to work.

    I hope you find this useful.

    Cheers,
    Mahlamusa