WordPress.org

Ready to get started?Download WordPress

Forums

making author notification condtional (2 posts)

  1. lwrkrol
    Member
    Posted 1 year ago #

    I use this piece of code to send notifications to the author of a post when it gets published. The problem is that everytime something changes, the e-mail is fired. I would like this code to be executed only the first time it gets published. Since I lack a serious amount of programming experience, i would be grateful to any kind of help

    function authorNotification ($post_id) {
    global $wpdb;
    $post = get_post($post_id);
    $author = get_userdata($post->post_author);
    $message = "
    Hallo ".$author->display_name.",

    De door u geregistreerde winkel, ".$post->post_title." is gepubliceerd op Scanjevoordeel.nl, u kunt nu uw aanbiedingen toevoegen.

    Met vriendelijke groet,

    Scanjevoordeel.nl";
    wp_mail($author->user_email, "Uw winkel op Scanjevoordeel.nl is gepubliceerd", $message);}
    add_action('publish_winkel', 'authorNotification');

    Thanks
    Luit

  2. bcworkz
    Member
    Posted 1 year ago #

    Change the action on last line from 'publish_winkel' to what would be in your language equivalent to English 'draft_to_publish'. This works because all new posts are instantly auto-saved to draft.

    The main difference is the whole post object is passed as a parameter, so you don't need to get_post(), so your first four lines become these three:

    function authorNotification ($post) {
    global $wpdb;
    $author = get_userdata($post->post_author);

Topic Closed

This topic has been closed to new replies.

About this Topic