Opened 12 years ago
Last modified 7 years ago
#28094 new enhancement
Add action hook after getting template part
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 3.9 |
| Component: | Themes | Keywords: | has-patch |
| Focuses: | template | Cc: |
Description
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:
<?php
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 ) {
ob_start();
}
}
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 ...]';
ob_clean();
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)
Change History (7)
#4
@
10 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
Related: #18561 for another use case