最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - Function added to hook "new_to_publish" not executing - custom plugin

programmeradmin1浏览0评论

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.

<?php
/**
* 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() {
  wp_mail('[email protected]','test','test');
}

?>

What am I doing wrong here?

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.

<?php
/**
* 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() {
  wp_mail('[email protected]','test','test');
}

?>

What am I doing wrong here?

Share Improve this question edited Dec 14, 2016 at 15:27 Ceds asked Dec 14, 2016 at 12:59 CedsCeds 151 silver badge4 bronze badges 2
  • Did you try with simple PHP mail() function? or use SMTP. – fmeaddons Commented Dec 14, 2016 at 13:09
  • Unfortunately that also appears to do nothing. – Ceds Commented Dec 14, 2016 at 15:18
Add a comment  | 

2 Answers 2

Reset to default 1

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

  function conditional_email( $old_status, $new_status) {
   wp_mail('[email protected]','test','test');
  }

You will see that in the example here

Hope it works after !

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( '[email protected]', 'test', 'test' );
}
发布评论

评论列表(0)

  1. 暂无评论