Make WordPress Core

Opened 5 years ago

Last modified 3 years ago

#40508 new enhancement

Add new do_action after load template files — at Version 5

Reported by: rajanit2000 Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 5.0
Component: Themes Keywords: has-patch needs-testing
Focuses: template Cc:

Description (last modified by desrosj)

Could not perform action after get_header loads.

get_header hook only perform before template part loads. So we can add new do_action after template files loads

do_action( 'get_header_after', $name );

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|null $name Name of the specific header file to use. null for the default header.
	do_action( 'get_header', $name );

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

	$templates[] = 'header.php';

	locate_template( $templates, true );

	do_action( 'get_header_after', $name );

Change History (7)

5 years ago

updated general-template.php

#1 @truongwp
5 years ago

Please attach the diff file if you have any patches, not edited file.
See https://make.wordpress.org/core/handbook/tutorials/trac/submitting-a-patch/

#2 @johnbillion
5 years ago

  • Component changed from General to Themes
  • Focuses template added
  • Keywords needs-patch reporter-feedback added
  • Type changed from defect (bug) to enhancement
  • Version trunk deleted

Hi @rajanit2000. Can you give an example of what this hook would be used for, please?

#3 @rajanit2000
5 years ago

Hi @johnbillion

Now i am working on one Cache plugin its under development. I need to add some lines after header.php file. How can i do that without this custom hook?

At this time i don't have samples.

#4 @desrosj
5 years ago

  • Description modified (diff)

This would be very helpful.

I have a child theme that uses a framework. Currently, the only way to include the additional markup that I need would be to override the file in the header.php child theme.

Ideally, I would like to add a function to an action hook to include this additional markup to prevent overriding the entire template file.

function my_additional_markup( $template_slug ) {
    <div>my additional stuff</div>
    // Could also include a template part
    if ( 'additional-header-type' === $slug ) {
        get_template_part( 'additional_template', 'slug' );
add_action( 'after_get_header', 'my_additional_markup' );

#5 @desrosj
5 years ago

  • Description modified (diff)

5 years ago

Add after_get_header and after_get_footer hooks.

Note: See TracTickets for help on using tickets.