I have this apply filter in my plugin.
$pre_html = apply_filters( 'events_views_html', null, $view_slug, $query, $context );
I want to change $view_slug
value dynamically from child theme using add_filter
because I do not want to modify plugin files. But this code is not working. It is displaying value of $view_slug
instead of displaying complete page content.
function add_extra_val( $view_slug, $query, $context ){
if (is_singular('tribe_events')) {
$view_slug = 'single-event';
}
return $view_slug;
}
add_filter('events_views_html', 'add_extra_val', 10, 3);
It is a basic question but I have limited knowledge of WordPress filters and hooks. Some guidelines regarding this will be appreciated.
I have this apply filter in my plugin.
$pre_html = apply_filters( 'events_views_html', null, $view_slug, $query, $context );
I want to change $view_slug
value dynamically from child theme using add_filter
because I do not want to modify plugin files. But this code is not working. It is displaying value of $view_slug
instead of displaying complete page content.
function add_extra_val( $view_slug, $query, $context ){
if (is_singular('tribe_events')) {
$view_slug = 'single-event';
}
return $view_slug;
}
add_filter('events_views_html', 'add_extra_val', 10, 3);
It is a basic question but I have limited knowledge of WordPress filters and hooks. Some guidelines regarding this will be appreciated.
Share Improve this question edited Oct 28, 2020 at 12:21 wplearner asked Oct 23, 2020 at 17:54 wplearnerwplearner 4892 gold badges9 silver badges27 bronze badges2 Answers
Reset to default 1Filters only allow you to directly modify the first value passed to them. In your case, that's null
. If you shifted view_slug
up a place, it would be available to filters using your events_view_html
tag.
add_filter( 'events_view_html', 'my_callback', 10, 3); // passing 3 args to the callback, assuming you need them for something.
function my_callback( $view_slug, $query, $context ) {
// Do whatever you need to do to $view_slug
// $query and $context can be read but not changed
return $view_slug;
}
Below is an example to modified the value using the add_filter hook:
// Accepting two arguments (three possible).
function example_callback( $view_slug, $query ) {
...
return $maybe_modified_value;
}
add_filter( 'events_views_html', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.