• Resolved nwp_developer

    (@nwp_developer)


    How can I dynamically pass $name argument in get_header($name) from functions.php?

    I want to dynamically change the $name argument instead of manually changing it to get_header(foo) to load header-foo.php.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    There is no usable filter for get_header(), so you can’t alter what happens from functions.php alone. You’d have to alter the calls from your theme templates so they can be manipulated by other code. One way would be to add your own filter to any theme templates where you want to control the header template.
    get_header( apply_filters('my_header_filter', '') );

    Thread Starter nwp_developer

    (@nwp_developer)

    Thanks!
    To clarify for anyone else:
    apply_filters allows you to create your own custom filters.

    In apply_filters(‘my_header_filter’, ”):
    my_header_filter is your custom filter and the ” is the value you’re filtering.

    So you can use <?php get_header( apply_filters(‘my_header_filter’, ”) ); ?> to dynamically change your header in your index or frontpage file.

    Then in functions.php you can change the value of the header
    function change_header($header){
    $header=’second’;
    return $header;
    }

    add_filter(‘my_header_filter’, ‘change_header’);

    To filter get_header() to load get_header(‘second’) to load header-second.php.

    Now the question is how to get this into $wp_customize.

    Thread Starter nwp_developer

    (@nwp_developer)

    Great! So you can create a Theme customization for the header using $wp_customize in functions.php, instantiate the WP_Customize_Control with type select to select your setting id for the header and pass the setting id of the header to your filter.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pass $name to get_header()’ is closed to new replies.