1

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!

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
    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! 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
    Jul 1, 2020 at 9:09
  • @TomJNowell thank you for your recommended, I will surely take a look in WP codex and PHP. Jul 1, 2020 at 11:58

1 Answer 1

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

3
  • Thank you so much for your kindly and supported. I've finally done it by changed the $in + 1 with $in - 2 and it's working fine for me. My lacking in PHP and WP Codex, so I don't understanding it well from start, it's clarify for me now. Jul 1, 2020 at 12:11
  • The codex is deprecated, take a look at developer.wordpress.org and the plugin/theme handbooks as well as the function reference
    – Tom J Nowell
    Jul 1, 2020 at 13:21
  • Already read it, thank you btw :). Jul 2, 2020 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.