Make WordPress Core

Opened 9 years ago

Last modified 7 months ago

#40508 accepted enhancement

Add new do_action after load template files

Reported by: rajanit2000's profile rajanit2000 Owned by: joedolson's profile joedolson
Milestone: Future Release Priority: normal
Severity: normal Version: 5.0
Component: Themes Keywords: has-patch has-test-info needs-dev-note
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 9 years ago.
updated general-template.php
40508.diff (1.1 KB) - added by desrosj 9 years ago.
Add after_get_header and after_get_footer hooks.
40508.1.patch (913 bytes) - added by rajanit2000 7 years ago.
Add hooks with phpdoc
Screenshot from 2018-11-26 14-12-32.png (61.2 KB) - added by rajanit2000 7 years ago.
Already have get_header hook

Download all attachments as: .zip

Change History (22)

@rajanit2000
9 years ago

updated general-template.php

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

  • Description modified (diff)

@desrosj
9 years ago

Add after_get_header and after_get_footer hooks.

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


8 years ago

@rajanit2000
7 years ago

Add hooks with phpdoc

#7 @rajanit2000
7 years ago

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

#8 @truongwp
7 years ago

For the footer, I think we need before_get_footer hook

@rajanit2000
7 years ago

Already have get_header hook

#9 @truongwp
7 years ago

Ah Sorry. I didn't check it

#10 @matveb
7 years ago

  • Milestone changed from Awaiting Review to Future Release

#11 @SirLouen
8 months ago

  • Keywords needs-refresh needs-test-info added; needs-testing removed

After [48209] a little refresh work is needed @rajanit2000 @desrosj

Also some test info will be appreciated.

This ticket was mentioned in PR #8939 on WordPress/wordpress-develop by @sainathpoojary.


7 months ago
#12

  • Keywords needs-refresh removed

Trac ticket: #40508

#13 @sainathpoojary
7 months ago

  • Keywords has-test-info needs-testing added; needs-test-info removed

Testing Instructions

Plugin Code:

<?php
/**
 * Plugin Name: Test Header Footer Actions
 * Description: Tests the after_get_header and get_footer_after actions
 * Version: 1.0
 */

add_action('after_get_header', 'test_after_header', 10, 2);
function test_after_header($name, $args) {
    echo '<!-- Testing after_get_header action -->';
    echo "<!-- Header name: " . esc_html($name) . " -->";
}

add_action('after_get_footer', 'test_footer_after', 10, 2);
function test_footer_after($name, $args) {
    echo '<!-- Testing after_get_footer action -->';
    echo "<!-- Footer name: " . esc_html($name) . " -->";
}

Steps to Test:

  • Activate the Twenty Seventeen theme.
  • Install and activate the above test plugin.
  • Visit any frontend page with header and footer (e.g., homepage or a single post).
  • View the page source (Right-click → "View Page Source").
  • Confirm the following output exists:
    • <!-- Testing after_get_header action -->
    • <!-- Header name: header -->
    • <!-- Testing after_get_footer action -->
    • <!-- Footer name: footer -->
Last edited 7 months ago by sainathpoojary (previous) (diff)

#14 @ankitkumarshah
7 months ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/8939

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.27.4
  • Database: mysqli (Server: 8.0.41 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 137.0.0.0

Actual Results

  1. ✅ Hooks work as expected.

Additional Notes

  • The after_get_header and after_get_footer works as expected.

Supplemental Artifacts

https://i.postimg.cc/P51zNnJc/image.png
https://i.postimg.cc/xCB3cfvw/image.png

#15 @SirLouen
7 months ago

  • Keywords needs-testing removed

Great @sainathpoojary, pretty straightforward.
I put this on my list.

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


7 months ago

#17 @SirLouen
7 months ago

  • Keywords needs-dev-note added

#18 @joedolson
7 months ago

  • Owner set to joedolson
  • Status changed from new to accepted
Note: See TracTickets for help on using tickets.