当我发布一个新的 post 时,wordpress 钩子是什么(而不是当我更新已发布的钩子时)?
What is the wordpress hook when I publish a new post, (not when I update a published one)?
我想在新 post 首次发布时(而不是编辑时)随时发送电子邮件
我试过:
add_action('publish_post', 'postPublished');
但是 postPublished 当我更新已经发布的 post 时也会调用。
我只想在第一次发布 post 时将其称为 。
此致
我想你要找的钩子是 draft_to_publish
如果您只想在新 post 上发送电子邮件,我会考虑在 post 发布时进行定位。您甚至可以查看 transition_post_status
挂钩,如果 post 已被编辑,则只需条件化,这样的事情可能会起作用:
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 );
您应该阅读 Post Status Transitions WordPress Codex 以完全理解问题。
例如,涵盖大多数情况的可能解决方案是:
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 );
我想在新 post 首次发布时(而不是编辑时)随时发送电子邮件
我试过:
add_action('publish_post', 'postPublished');
但是 postPublished 当我更新已经发布的 post 时也会调用。 我只想在第一次发布 post 时将其称为 。
此致
我想你要找的钩子是 draft_to_publish
如果您只想在新 post 上发送电子邮件,我会考虑在 post 发布时进行定位。您甚至可以查看 transition_post_status
挂钩,如果 post 已被编辑,则只需条件化,这样的事情可能会起作用:
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 );
您应该阅读 Post Status Transitions WordPress Codex 以完全理解问题。
例如,涵盖大多数情况的可能解决方案是:
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 );