最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Update variable value via add_filter

programmeradmin2浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

Filters 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.
发布评论

评论列表(0)

  1. 暂无评论