Remove an action from an external Class

A simple way to achieve this (but without the Class approach) is by filtering the output of wp_head action hook using the output buffering. In your theme’s header.php, wrap the wp_head() call with ob_start($cb) and ob_end_flush(); functions like: ob_start(‘ad_filter_wp_head_output’); wp_head(); ob_end_flush(); Now in theme functions.php file, declare your output callback function (ad_filter_wp_head_output in this case): … Read more

How to do_action and get a return value?

The cool thing is a filter is the same as an action, only it returns a value, so just set it up as a filter instead: add_filter( ‘myplugin_clean_logs’, array( ‘MyPlugin_Logs’, ‘clean_logs’ ) ); Then something like: $affected_rows=””; $affected_rows = apply_filters( ‘myplugin_clean_logs’, $affected_rows ); should pass $affected_rows to clean_logs() (and whatever other functions you may have … Read more

How to know what priority to use with add_action()?

range of priority values? range of priority values? Generally, speaking, you can’t know a priori what priority something is hooked with. The priority needed depends on how other callbacks where hooked in. Often that is the default value of 10 but it could be anywhere between PHP_INT_MIN (negative integers are still integers) and PHP_INT_MAX and … Read more

Remove parent theme action in child

For removing an action hook you should use the same action name, callback name and the priority that was used to add a action in parent theme. And register it on init add_action( ‘init’, ‘remove_my_action’); function remove_my_action() { remove_action( ‘woocommerce_before_shop_loop’,’storefront_sorting_wrapper’,9 ); } Read about remove_action

Why does save_post action fire when creating a new post?

When you click ‘New Post’, you’re simply loading the page wp-admin/post-new.php. In doing so, WordPress will always create a new post (an ‘Auto Draft’) to ensure all other features (such as media uploads) and plugins work as normal, even before you actually save a draft or publish the post. And this, in turn, triggers save_post. … Read more

add_action reference a class

No, you cannot ‘initialise’ or instantiate the class through a hook, not directly. Some additional code is always required ( and it is not a desirable thing to be able to do that, as you’re opening a can of worms for yourself. Here is a better way of doing it: class MyClass { function __construct() … Read more

remove_action or remove_filter with external classes?

The best thing to do here is to use a static class. The following code should be instructional: class MyClass { function __construct() { add_action( ‘wp_footer’, array( $this, ‘my_action’ ) ); } function my_action() { print ‘<h1>’ . __class__ . ‘ – ‘ . __function__ . ‘</h1>’; } } new MyClass(); class MyStaticClass { public … Read more