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

customization - Custom Post Type Alerts

programmeradmin4浏览0评论

I setup a custom post type and everything works right. The user can submit for review and I see it's pending in the admin area. How can I have WordPress send me an Email Notification whenever a user submits a post?

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')

    )
);
}

I setup a custom post type and everything works right. The user can submit for review and I see it's pending in the admin area. How can I have WordPress send me an Email Notification whenever a user submits a post?

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')

    )
);
}
Share Improve this question edited Mar 27, 2014 at 17:04 Nicolai Grossherr 18.9k8 gold badges64 silver badges109 bronze badges asked Mar 25, 2014 at 20:33 BrianBrian 1501 silver badge8 bronze badges 9
  • 2 Is this something you've custom built? It is a plugin? By comments do you mean like Post Comments you would make on a blog? When you create a Post Type you should be able to set the option 'supports' => array('comments') View Codex – Howdy_McGee Commented Mar 25, 2014 at 20:52
  • I set it up in functions.php and I do have it set to supports => comments but I don't get any notifications. Yes I am talking about post comments which does work as expected. I added my code. – Brian Commented Mar 25, 2014 at 21:32
  • Is this not possible? – Brian Commented Mar 27, 2014 at 13:33
  • I've tested this with my Post Type and I get the Red (1) whenever somebody posts a comment. If you want email notifications, checkout Settings->Discussion – Howdy_McGee Commented Mar 27, 2014 at 13:50
  • 1 I was able to find what I wanted with: Post Status Notifier Lite – Brian Commented Mar 27, 2014 at 17:30
 |  Show 4 more comments

1 Answer 1

Reset to default 1

WordPress has a save_post hook, which is an action triggered whenever a post or page is created or updated.

Add something like this below to your functions.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( '[email protected]', $subject, $message );
}
add_action( 'save_post', 'my_project_updated_send_email' );

One thing to note is that your localhost by default will not send out an email to an external source - unless you have configured it. There is however a couple of plugins that will allow you to send via SMTP, that way you can easily test from localhost.

The example above is taken from the save_post codex page, however in addition to the above, you would want to add filters to only send it out for your custom post type and not every post, as well as maybe just for a fresh create rather than all updates (by specifying which post status you want alerts for) - up to you to decide.

All the best,
Kat

发布评论

评论列表(0)

  1. 暂无评论