I would like to use create a function in my custom plugin to tell WP to use a different header

I am well aware of the use of child themes for this. I know about get_header($name). I already understand the use of conditional tags.

I want my plugin to remove the function:

function get_header( $name = null ) {
do_action( 'get_header', $name );
$templates = array();
if ( isset($name) )
templates[] = "header-{$name}.php";
$templates[] = 'header.php';
// Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( ABSPATH . WPINC . '/theme-compat/header.php');
}

and the replace get_header() with:

function get_header( $name = null ) {
do_action( 'get_header', $name );
$templates = array();
if ( isset($name) )
templates[] = "header-{$name}.php";
$templates[] = 'header.php';
// Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( MY PLUGIN PATH/header.php');
}

php.net told me to use runkit_function_remove() but the website I am working on says this is an undefined function.

Any help would be much appreciated.

Leon

1 Answer
1

I am not sure what you want to do is doable (seems impossible to me but everything seems impossible until someone does it). I thought of a very ugly workaround.

add_filter( 'template_include', function( $template ) {
    $temp = get_temp_dir() . basename( $template );
    file_put_contents( $temp, str_replace( 'get_header(', 'get_header2(', file_get_contents( $template ) ) );
    return $temp;
});

function get_header2( $name = null ) {
    do_action( 'get_header', $name );

    $templates = array();
    if ( isset($name) )
        $templates[] = "header-{$name}.php";

    $templates[] = 'header.php';

    // Backward compat code will be removed in a future release
    if ('' == locate_template($templates, true))
        load_template( MY PLUGIN PATH/header.php');
}

This replaces get_header with get_header2 in all templates before loading.

Leave a Comment