Reuse variable in hook callback

If you want to use a variable from a another context in a function, create a class: class CustomLog { private $debug_msg = “”; public function __construct( $msg ) { $this->debug_msg = $msg; } public function log() { error_log( $this->debug_msg, 0 ); } } add_action( ‘init’, [ new CustomLog( “Hi there!” ), ‘log’ ], 20 … Read more

Using auth_redirect returns cannot modify header information

It works fine on my localhost as well. The reason it probably doesn’t work on your server is that it’s not using output buffering. Hooking into wp_head means that the page has already started printing to the client’s screen. Meaning auth_redirect‘s use of wp_redirect will fail: the headers have already been sent and you see … Read more

Detect type of post status transition

If not, how do I tie multiple hooks to the same action? Do I just list them like so: add_action(‘new_to_publish’, ‘save_new_post’, 10, 1); add_action(‘future_to_publish’, ‘save_new_post’, 10, 1); add_action(‘draft_to_publish’, ‘save_new_post’, 10, 1); This is exactly the way to go. Just hook the same callback into each of the status-transition hooks on which you want the callback … Read more

Why is `add_meta_box` not working?

I can see 2 problems with your code. You seem to want that meta box to appear on product pages, but you actually add it to the post type and not the product post type. The other problem is the hook you attach your function to. Try this: add_action( ‘add_meta_boxes_{post_type}’, ‘abc_load_post’ ); Substitute {post_type} with … Read more

add_action on inherit post status

I’m running into this same issue in some code I’m developing. In your case though tree is a better hook as you only want to hook posts that are moving to ‘publish’ You need to hook your function into all of the possible {status}_to_publish hooks like this: add_action(‘new_to_publish’, ‘my_function’); add_action(‘draft_to_publish’, ‘my_function’); add_action(‘auto-draft_to_publish’, ‘my_function’); add_action(‘pending_to_publish’, ‘my_function’); … Read more

How to hook into the quick edit action?

There are two flaws in the code that I can see. The first one is a bug in this code if($post->post_type != ‘product’) If no post exists, you get the following error Notice: Trying to get property of non-object… This can be fixed by first checking if a post isset if(isset($post) && $post->post_type != ‘product’) … Read more

Is it possible to create an action hook using do_action() within add_action()?

Creating an answer based upon responses via comments on original question of: Is it possible to create an action hook using do_action() within add_action()? Yes it is possible to create an action hook using do_action() on a call to add_action(). For clarification, the following code does NOT work: add_action(‘init’, do_action(‘my-hook-name’)); As stated by @IvanHanák in … Read more