• Resolved Tjarls.com

    (@tjarlscom)


    I am currently writing about WordPress and wanted to explain how the get_header() function worked. But then I hit a dead end.

    The get_header function looks like this:

    function get_header( $name = null ) {
    	do_action( 'get_header', $name );
    
    	$templates = array();
    	if ( isset($name) )
    		$templates[] = "header-{$name}.php";
    
    	$templates[] = "header.php";
    
    	if ('' == locate_template($templates, true))
    		load_template( get_theme_root() . '/default/header.php');
    }

    I figured out that $templates is an array in which the filename “header.php” ( and header-($name).php – if $name is set) is stored.

    After that $template is added to the function locate_template() which searches for the given file, and returns a string containing the file path if one is found or an empty string if no file is found.

    If the string is empty the default header is loaded through load_template().

    Now to the dead end. I cant seem to figure out where the template file is being loaded. It’s like there is an “else” missing in the “if” sentence. eg.

    if ('' == locate_template($templates, true))
    		load_template( get_theme_root() . '/default/header.php');
    else
    $template = locate_template($templates, true);
    load_template($template);

    The only other option i could think of would be that the template was loaded in do_action('get_header', $name);
    but i havn’t been able to find anything of the like in the source codes. So if anyone knows anything about this, please help(the sooner the better).

    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • popper

    (@julialasarte)

    Hey there!

    the function locate_template searchs for the file(s) in the $templates Array, if it finds the file, the function already loads it, so there’s no need to call load_template for the template file.

    see here: https://codex.www.ads-software.com/Function_Reference/locate_template
    and here: https://core.trac.www.ads-software.com/browser/trunk/wp-includes/theme.php#L1045

    hope that helps!

    Thread Starter Tjarls.com

    (@tjarlscom)

    Thank you very much for the response ??

    This does make some sense now, but this leads me to a new question.

    When the function locate_template() is called $load is defined as being true

    if ('' == locate_template($templates, true))
    		load_template( get_theme_root() . '/default/header.php');

    but in locate_template() it seams that load_template() is only being called if the string in $located is not empty and equal to $load

    if ($load && '' != $located)
    		load_template($located);

    but the fact that $located is not empty should mean that it is being returned as ‘true’ right? and since $load is defined as true when the function is first called i don’t see how it can load the template.

    Did i read this wrong? or am i missing something? Really hope you can help me.

    again, thanks in advance.

    popper

    (@julialasarte)

    You’re welcome!

    About your question, in this piece of code:

    if ($load && '' != $located)
    		load_template($located);

    what actually happens is this:

    if (($load)&&(''!=$located))
        load_template($located);

    meaning if ($load is TRUE) AND ($located is not an empty string), it loads the template.

    Hope that helps!

    Thread Starter Tjarls.com

    (@tjarlscom)

    Ah I see. You’ve really been a great help, thank you.

    Now, I do have a final question.
    It’s about the get_header action.

    When ′do_action(get_header)′ is called, what actually happens? or does anything really happen?

    ′do_action( ‘get_header’, $name );′

    So far the only thing I’ve been able to conclude is that the get_header action is used to run functions before the header template is loaded.
    Does it have any other uses or is it just a means to add functions before the header is loaded?

    popper

    (@julialasarte)

    What do_action actually does, is create a hook for attaching actions via add_action. The actions can be hooked to a specific function by plugis.

    you could do, for example, something like this:

    add_action(‘get_header’, ‘some_plugin_function’);

    If you’re interested, you could read about hooks here

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How does get_header() get the header.php template? – Detailed PHP’ is closed to new replies.