首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >wordpress - wp只为保存的新帖子插入post钩子

wordpress - wp只为保存的新帖子插入post钩子
EN

Stack Overflow用户
提问于 2016-06-26 22:31:54
回答 3查看 4.7K关注 0票数 2

我正在使用wp insert post作为一个钩子发送电子邮件,当一个新的帖子已经提交到wordpress,我已经参考了这个链接,并尝试了下面的代码。我能够完美地收到电子邮件,问题是我收到的邮件,即使是垃圾邮件,这是不必要的。是否有任何方法只在创建新帖子时触发邮件,而不用于任何其他操作。

代码语言:javascript
复制
function my_project_updated_send_email( $post_id, $post, $update ) {

// If this is a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
    return;

$post_url = get_permalink( $post_id );
$subject = 'A post has been updated';

$message = "A post has been updated on your website:\n\n";
$message .= $post->post_title . ": " . $post_url;

// Send email to admin.
wp_mail( 'admin@example.com', $subject, $message );
}
add_action( 'wp_insert_post', 'my_project_updated_send_email', 10, 3 );
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-26 22:42:02

我刚发完这个问题就想出来了,但在这里更新了我的解决方案,这样对某个人是有用的。

解决方案是,我使用如下所示的相同的post_status检查提交的帖子的wp insert post,从而添加了一个检查点。

代码语言:javascript
复制
$post_status = get_post($post_id)->post_status;

if($post_status == 'pending'){
//send the post pending email
}elseif($post_status == 'publish'){
// send the post published email
}elseif($post_status == 'trash'){
// send the post trashed email
}
票数 1
EN

Stack Overflow用户

发布于 2016-06-26 23:02:14

您可以使用后状态转换。下面是draft -> publish的例子。

代码语言:javascript
复制
add_action('draft_to_publish', 'draft_to_publish_actions');
function draft_to_publish_actions($object)
{
//do stuff    
}
票数 1
EN

Stack Overflow用户

发布于 2022-09-04 15:15:25

代码语言:javascript
复制
function wcl_insert_car($post_ID, $post, $update) {
    if (get_post_type() != 'car') {
        return;
    }
    $value = get_post_meta($post_ID, 'car_init', true);
    if (empty($value)) {
        // One time code on init Post
        update_post_meta($post_ID, 'car_init', true);
    }
}
add_action('wp_insert_post', 'wcl_insert_car', 10, 3);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38043811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档