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!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0