Make WordPress Core

Opened 8 years ago

Last modified 6 years ago

#40508 new enhancement

Add new do_action after load template files

Reported by: rajanit2000's profile 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 );
}

Attachments (4)

general-template.php (122.2 KB) - added by rajanit2000 8 years ago.
updated general-template.php
40508.diff (1.1 KB) - added by desrosj 7 years ago.
Add after_get_header and after_get_footer hooks.
40508.1.patch (913 bytes) - added by rajanit2000 6 years ago.
Add hooks with phpdoc
Screenshot from 2018-11-26 14-12-32.png (61.2 KB) - added by rajanit2000 6 years ago.
Already have get_header hook

Download all attachments as: .zip

Change History (14)

@rajanit2000
8 years ago

updated general-template.php

#1 @truongwp
8 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
8 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
8 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
7 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>
    <?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
7 years ago

  • Description modified (diff)

@desrosj
7 years ago

Add after_get_header and after_get_footer hooks.

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


7 years ago

@rajanit2000
6 years ago

Add hooks with phpdoc

#7 @rajanit2000
6 years ago

  • Keywords has-patch needs-testing added; needs-patch reporter-feedback removed
  • Version set to 5.0

#8 @truongwp
6 years ago

For the footer, I think we need before_get_footer hook

@rajanit2000
6 years ago

Already have get_header hook

#9 @truongwp
6 years ago

Ah Sorry. I didn't check it

#10 @matveb
6 years ago

  • Milestone changed from Awaiting Review to Future Release
Note: See TracTickets for help on using tickets.