【发布时间】:2017-04-06 07:13:16
【问题描述】:
我想在首次发布新帖子时发送电子邮件(而不是在编辑时)
我试过了:
add_action('publish_post', 'postPublished');
但是 postPublished 在我更新已发布的帖子时也会调用。 我只想仅在第一次发布帖子时将其命名为。
问候
【问题讨论】:
-
你可以查看帖子的ID
我想在首次发布新帖子时发送电子邮件(而不是在编辑时)
我试过了:
add_action('publish_post', 'postPublished');
但是 postPublished 在我更新已发布的帖子时也会调用。 我只想仅在第一次发布帖子时将其命名为。
问候
【问题讨论】:
我想你要找的钩子是draft_to_publish
如果您只想在新帖子上发送电子邮件,我会考虑在帖子发布时进行定位。您甚至可以查看 transition_post_status 钩子,并在帖子已被编辑时设置条件,这样的事情可能会起作用:
function so_post_40744782( $new_status, $old_status, $post ) {
if ( $new_status == 'publish' && $old_status != 'publish' ) {
$author = "foobar";
$message = "We wanted to notify you a new post has been published.";
wp_mail($author, "New Post Published", $message);
}
}
add_action('transition_post_status', 'so_post_40744782', 10, 3 );
【讨论】:
您应该阅读 WordPress Codex 中的 Post Status Transitions 以完全理解问题。
例如,涵盖大多数情况的可能解决方案是:
add_action('draft_to_publish', 'postPublished');
add_action('future_to_publish', 'postPublished');
add_action('private_to_publish', 'postPublished');
【讨论】:
function your_callback( $post_id, $post ) {
if (strpos($_SERVER['HTTP_REFERER'], '&action=edit') !== false) {
//edit previous post
}
else{
//just add a new post
}
}
add_action( 'publish_{your_post_type}', 'your_callback', 10, 3 );
【讨论】: