Open joshdarby opened 4 years ago
if ( ! empty( $context ) ) {
$context = apply_filters( 'largo_render_template_context', $context, $slug, $name );
// we're about to modify $wp_query in hard-to-predict ways, so take a backup
$_preserve = $wp_query;
// get_template_part() uses locate_template() uses load_template(),
// which extracts these variables into the context
// @link https://developer.wordpress.org/reference/functions/load_template/
$wp_query->query_vars = array_merge( $wp_query->query_vars, $context );
}
get_template_part( $slug, $name );
if ( ! empty( $context ) ) {
// restore the backup
$wp_query = $_preserve;
}
I'm not sure about that one, though, because the filter can change $context
to be an empty()
value. Are there any empty values of $context
that would result in $wp_query->query_vars
being modified by the array_merge
?
This one doesn't reuse $context
:
if ( ! empty( $context ) ) {
$filtered_context = apply_filters( 'largo_render_template_context', $context, $slug, $name );
// we're about to modify $wp_query in hard-to-predict ways, so take a backup
$_preserve = $wp_query;
// get_template_part() uses locate_template() uses load_template(),
// which extracts these variables into the context
// @link https://developer.wordpress.org/reference/functions/load_template/
$wp_query->query_vars = array_merge( $wp_query->query_vars, $filtered_context );
}
get_template_part( $slug, $name );
if ( ! empty( $context ) ) {
// restore the backup
$wp_query = $_preserve;
}
Lesson learned: When mucking with $wp_query
, reset it.
In WP 5.5, we can get rid of largo_render_template: https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/
https://github.com/INN/largo/blob/512da701664b329f2f92244bbe54880a6e146431/inc/helpers.php#L244-L254
largo_render_template
is using query vars from previous queries sometimes. Discovered because in the City Limits podcasts widget when$context
haspodcast => true
, it doesn't get removed from the query when the query runs for the next widget below.Updating
to
works, but not sure of any implications of doing that.