Make WordPress Core

Opened 10 years ago

Last modified 5 years ago

#28094 new enhancement

Add action hook after getting template part

Reported by: andrezrv's profile andrezrv Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9
Component: Themes Keywords: has-patch
Focuses: template Cc:


I think it should be nice to have the possibility to execute actions after getting a template part, not only before doing it, as it is now. I find this can be particularly useful to deal with output buffering, allowing to do things like this:

add_action( 'get_template_part_content', 'catch_template_part_output' );
 * Start output buffering for template part.
function catch_template_part_output( $slug, $name = null ) {
	if ( '' == $name ) {

add_action( 'after_get_template_part_content', 'release_template_part_output' );
 * End output buffering for template part and print modified output.
function release_template_part_output( $slug, $name = null ) {
	if ( '' == $name ) {
		$output = ob_get_contents();
		$output = '[Something before the original output ...]' . $output;
		$output .= '[Something after the original output ...]';
		echo $output;

I'm thinking this particular method could give an extra option to theme developers who need to modify a very simple piece of output without having to create a new template or new functions to be embedded into an existing template. I know maybe some people won't consider this to be a recommended practice (I'm actually a little divided here), but you'd still have the possibility to trigger any other functionality you want on such hook.

Attachments (2)

after-get-template-part-action.diff (352 bytes) - added by andrezrv 10 years ago.
28094.diff (1.6 KB) - added by iamfriendly 9 years ago.
Added a filter at the top of get_template_part() to bail if we wish to, add an action after locate_template

Download all attachments as: .zip

Change History (7)

#1 @andrezrv
10 years ago

  • Keywords has-patch 2nd-opinion added

#2 @ocean90
10 years ago

  • Type changed from feature request to enhancement

Related: #18561 for another use case

#4 @wonderboymusic
9 years ago

  • Keywords needs-docs needs-refresh added; 2nd-opinion removed
  • Milestone changed from Awaiting Review to Future Release

This should also have a filter at the top to bail from the function if you've determined that you can serve from the cache. Filters and actions need doc blocks

9 years ago

Added a filter at the top of get_template_part() to bail if we wish to, add an action after locate_template

#5 @iamfriendly
9 years ago

  • Keywords needs-docs needs-refresh removed

Building on the work of @andrezrv I've added docs to the action and also, as suggested by @wonderboymusic added a filter at the top of the function to allow us to bail should we wish. Docs added to both the action and filter.

Note: See TracTickets for help on using tickets.