I was creating a custom plugin which was to send certain emails based on the content/category of a post, but whilst trying to do that, I ran into some problems just getting a basic email sent out. Am I hooking on to the wrong function here? When I publish a post, nothing happens.

* Plugin Name: Conditional Emailing
* Description: Sends emails based on categories.
* Version: 1.0
* Author: Ceds

add_action( 'new_to_publish', 'conditional_email', 10, 0);

function conditional_email() {


What am I doing wrong here?

  • Did you try with simple PHP mail() function? or use SMTP. – fmeaddons Dec 14 '16 at 13:09
  • Unfortunately that also appears to do nothing. – Ceds Dec 14 '16 at 15:18

You need to add parameters $old_status and $new_status to the function.

  function conditional_email( $old_status, $new_status) {

You will see that in the example here

Hope it works after !

|improve this answer|||||
  • Thank you! I ended up using the post transition hook as opposed to the new to publish hook, and with your additions it works perfectly now. – Ceds Dec 14 '16 at 19:04
  • Great, you can see a plugin I've done to follow new post and correction made by author : Brozzme multiple admin emails. – Benoti Dec 14 '16 at 19:08

The problem with your code is that you passed zero parameters, but the documentation clearly specifies that:

It is necessary to specify the number of arguments do_action() should pass to the callback function.

Also another nice thing to know: this hook does not run if you click on new post, type in the title and immediately hit the publish button. It only runs if you wait for WordPress to generate the URL for the post! It is because of a DOING_AUTOSAVE condition in the core function.

So here is the corrected code for you:

add_action( 'new_to_publish', 'conditional_email', 10, 1);

 * Send emails based on categories when a post is published
 * @param \WP_Post $post
function conditional_email( $post ) {
  wp_mail( 'my@email.com', 'test', 'test' );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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