Opened 9 years ago
Last modified 7 months ago
#40508 accepted enhancement
Add new do_action after load template files
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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 )
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)
Change History (22)
#1
@
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
@
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
@
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
@
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' );
This ticket was mentioned in Slack in #core by rajanit2000. View the logs.
8 years ago
#7
@
7 years ago
- Keywords has-patch needs-testing added; needs-patch reporter-feedback removed
- Version set to 5.0
#11
@
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
@
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 -->
#14
@
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
- ✅ Hooks work as expected.
Additional Notes
- The
after_get_headerandafter_get_footerworks as expected.
Supplemental Artifacts
#15
@
7 months ago
- Keywords needs-testing removed
Great @sainathpoojary, pretty straightforward.
I put this on my list.


updated general-template.php