Custom Post Type Alerts

时间:2014-03-25 作者:Brian

我设置了一个自定义的帖子类型,一切正常。用户可以提交审查,我看到它在管理区域中待定。我如何让WordPress在用户提交帖子时向我发送电子邮件通知?

add_action( \'init\', \'artwork_feature\');
function artwork_feature() {
register_post_type( \'artwork\',
    array(
        \'labels\' => array(
            \'name\' => __( \'Artwork\' ),
            \'singular_name\' => __( \'Artwork\' )
        ),
    \'public\' => true,
    \'exclude_from_search\'       => false,
    \'capability_type\' => \'artwork\',
    \'supports\' => array(\'custom-fields\', \'comments\'),
        \'capabilities\' => array(
            \'publish_posts\' => \'publish_artworks\',
            \'edit_posts\' => \'edit_artworks\',
            \'edit_others_posts\' => \'edit_others_artwork\',
            \'delete_posts\' => \'delete_artworks\',
            \'delete_others_posts\' => \'delete_others_artwork\',
            \'read_private_posts\' => \'read_private_artwork\',
            \'edit_post\' => \'edit_artwork\',
            \'delete_post\' => \'delete_artwork\',
            \'read_post\' => \'read_artwork\',
        ),
    \'map_meta_cap\' => true,
    \'has_archive\' => true,
    \'supports\' => array(\'title\', \'editor\', \'thumbnail\')

    )
);
}

1 个回复
SO网友:Katrina

WordPress有一个save_post 钩子,这是在创建或更新帖子或页面时触发的操作。

在函数中添加如下内容。php:

function my_project_updated_send_email( $post_id ) {

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

    $post_title = get_the_title( $post_id );
    $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_title . ": " . $post_url;

    // Send email to admin.
    wp_mail( \'admin@example.com\', $subject, $message );
}
add_action( \'save_post\', \'my_project_updated_send_email\' );
需要注意的一件事是,默认情况下,您的localhost不会向外部源发送电子邮件,除非您已经对其进行了配置。不过,有几个插件允许您通过SMTP发送,这样您就可以轻松地从localhost进行测试

上述示例摘自save_post 但是,除了上述内容之外,您还需要添加过滤器,以便仅针对您的自定义帖子类型而不是每个帖子发送该页面,也可能只是针对一个新的创建而不是所有更新(通过指定post status 您需要的警报)-由您决定。

祝你一切顺利,凯特

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register