• How can I replace get_header function or header.php via plugin?

    I installed a child theme and now I’d like to add an html code into header.php

    I used this:

    function myheader( $template ) {
        if ( locate_template( 'header.php' ) != '' ) {
            return untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'header.php';
        }
    	return $template;
    }
    add_filter( 'template_include', 'myheader', 11 );

    It replaces the header and shows only the header and not the full template (the content, sidebar, footer…)

Viewing 1 replies (of 1 total)
  • The next solution don’t like me but is an options:

    Go to ‘wp-includes/general-template.php’ file and add:

    $templates = apply_filters(‘replace_header’, $templates);
    In the get_header function the final result function is:

    function get_header( $name = null ) {
        /**
         * Fires before the header template file is loaded.
         *
         * The hook allows a specific header template file to be used in place of the
         * default header template file. If your file is called header-new.php,
         * you would specify the filename in the hook as get_header( 'new' ).
         *
         * @since 2.1.0
         * @since 2.8.0 $name parameter added.
         *
         * @param string $name Name of the specific header file to use.
         */
        do_action( 'get_header', $name );
    
        $templates = array();
        $name = (string) $name;
        if ( '' !== $name ) {
            $templates[] = "header-{$name}.php";
        }
    
        $templates[] = 'header.php';
    
        $templates = apply_filters('replace_header', $templates); //This is the new line.
    
        locate_template( $templates, true );
    }

    And before that you can:

    function prefix_new_header() {
        return 'header-newtmpl.php';
    }
    add_filter( 'get_header', 'prefix_new_header' );

    I never modify the source code, I only put here one solution to the problem.

    Original Source: https://stackoverflow.com/questions/39661295/get-header-action-not-working/39662513#39662513

    Jose Carlos Ramos Carmenates

Viewing 1 replies (of 1 total)
  • The topic ‘Replace get_header() or header.php via plugin’ is closed to new replies.