简体   繁体   中英

Override function in custom plugin

I'm using a plugin with this action:

add_filter( 'pre_comment_approved', array( $this, 'pre_save_review' ), 10, 2 );

I've written a plugin to do some tweaking to my site here and there. Mostly REST API customizations.

I'd like to override, subvert, ignore, remove, that filter. Right now I've got it "working" by just return ing on the first line of the pre_save_review function.

I tried:

remove_action( 'pre_comment_approved', 'pre_save_review');

... but I wonder if there's some namespacing issue. I don't know a lot about PHP so I don't know how to refer to classes in other files/plugins, which I imagine is the issue.

Thanks!

You can remove all filters with a wordpress function "remove_all_filters"

remove_all_filters('pre_comment_approved');

This also has a second parameter to pass the priority number, since this one is added with priority 10 it would be this:

remove_all_filters('pre_comment_approved', 10);

Reference remove_all_filters

You'll want to use the remove_filter function for this.

See the Example in the documentation here: https://codex.wordpress.org/Function_Reference/remove_filter

If a filter has been added from within a class, for example by a plugin, removing it will require accessing the class variable.

global $my_class;
remove_filter( 'the_content', array($my_class, 'class_filter_function') );

So maybe give the following a try;

global $this;
remove_filter( 'pre_comment_approved', array( $this, 'pre_save_review' ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM