WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 6 months ago

#40508 new enhancement

Add new do_action after load template files

Reported by: rajanit2000 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Themes Keywords: needs-patch reporter-feedback
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 );
}

Attachments (2)

general-template.php (122.2 KB) - added by rajanit2000 9 months ago.
updated general-template.php
40508.diff (1.1 KB) - added by desrosj 8 months ago.
Add after_get_header and after_get_footer hooks.

Download all attachments as: .zip

Change History (8)

@rajanit2000
9 months ago

updated general-template.php

#1 @truongwp
9 months 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
9 months 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
9 months 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
8 months 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>
    <?php
    // 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
8 months ago

  • Description modified (diff)

@desrosj
8 months ago

Add after_get_header and after_get_footer hooks.

This ticket was mentioned in Slack in #core by rajanit2000. View the logs.


6 months ago

Note: See TracTickets for help on using tickets.