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

functions - Override the filter from plugin in child theme

programmeradmin1浏览0评论

I have a filter below from plugin file:

$review_num_fetch = apply_filters('tourmaster_review_num_fetch', 5);

I want change the number from 5 to 3, I’ve tried change it like below:

// reviews number
function tourmaster_review_num_fetch_custom () {
    $review_num_fetch = apply_filters('tourmaster_review_num_fetch_custom', 3);
}

remove_filter(‘tourmaster_review_num_fetch’, 10);
add_filter('tourmaster_review_num_fetch', 'tourmaster_review_num_fetch_custom', 10);

But it’s wrong. I’m not sure why, I hope get some explain about it, TIA!

I have a filter below from plugin file:

$review_num_fetch = apply_filters('tourmaster_review_num_fetch', 5);

I want change the number from 5 to 3, I’ve tried change it like below:

// reviews number
function tourmaster_review_num_fetch_custom () {
    $review_num_fetch = apply_filters('tourmaster_review_num_fetch_custom', 3);
}

remove_filter(‘tourmaster_review_num_fetch’, 10);
add_filter('tourmaster_review_num_fetch', 'tourmaster_review_num_fetch_custom', 10);

But it’s wrong. I’m not sure why, I hope get some explain about it, TIA!

Share Improve this question asked Jul 1, 2020 at 8:52 Loc_rabbirtLoc_rabbirt 1138 bronze badges 4
  • Where is the code located? functions.php? I see you used backticks iinstead of quotes iin your remove_filter call, and you've replaced it with another filter that calls another filter, but you don't seem to have implemented the filters properly, there's basic stuff missing – Tom J Nowell Commented Jul 1, 2020 at 9:02
  • The original code located from plugin file and my code located in functions.php, sorry for my knowledge, I’m not sure about the implemented the filters, my knowledge about wp codex and php so bad, could you help me know how to implemented it correct? TIA! – Loc_rabbirt Commented Jul 1, 2020 at 9:07
  • I strongly recommend reading about the basics of PHP, particularly functions and function arguments. It will save you a lot of time, and make problems like this trivial to solve. Otherwise you won't understand the answers you get – Tom J Nowell Commented Jul 1, 2020 at 9:09
  • @TomJNowell thank you for your recommended, I will surely take a look in WP codex and PHP. – Loc_rabbirt Commented Jul 1, 2020 at 11:58
Add a comment  | 

1 Answer 1

Reset to default 3

That's not how filters work. Much like a filter in real life, something goes in, it gets changed, and the result goes out. Filters always take in a parameter and return something. They're an opportunity to change something

So if we take this:

$review_num_fetch = apply_filters('tourmaster_review_num_fetch', 5);

This means the plugin uses 5, but it has given other code an opportunity to change this to another value. It does this by passing it to the tourmaster_review_num_fetch filter then using the result. So hook into that filter and return a different value to change it.

When that line runs, WP looks at all the filters registered, passes the value, and then replaces it with what the filter returned.

This is what a filter that adds 1 to the value looks like:

function addone( $in ) {
    $out = $in + 1;
    return $out;
}

And this is how you would add it:

add_filter( 'tourmaster_review_num_fetch', 'addone' );

With that you should have all the knowledge needed for implementing your solution

发布评论

评论列表(0)

  1. 暂无评论