• Resolved oKikos

    (@okikos)


    hi I wrote the below plugin to send an email once posts are published, but nothing happens – where is my mistake? thank you!

    <?php
    function send_email(){
    $to = “[email protected]”;
    $subject = single_post_title();
    $body = single_post_title();
    the_excerpt();
    function new_excerpt_more($more) {
    global $post;
    return ‘ID) . ‘”>Read the Rest…‘;
    }
    add_filter(‘excerpt_more’, ‘new_excerpt_more’);
    }
    add_action(‘publish_post’,’send_email()’);
    ?>

    [mod: please read and apply the forum guidelines for posting code]

Viewing 7 replies - 1 through 7 (of 7 total)
  • remove the ()
    add_action(‘publish_post’,’send_email’);

    Thread Starter oKikos

    (@okikos)

    I did as you said, nothing happened, i didn’t get the email

    first off, you never call wp_mail so it’s not actually sending mail, secondly, even if it were, you have no headers defined, and lastly,
    in that function what is “single_post_title()”? i.e. how does it know what id it’s supposed to be getting the post for? publish_post changes the data, therefore the $post variable is empty until it’s re-read, so only $post_ID is available.

    function send_email($post_ID) {
      $FromEmail = "address@mail_is_from.com";
      $to = "[email protected]";
      $mypost = get_post($post_ID);
      $subject = $mypost->post_title;
      $body = $mypost->post_content;
      $headers = "MIME-Version: 1.0" . "\r\n";
      $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
      $headers .= "From: \"$FromEmail\" <$FromEmail>\n";
      @wp_mail($to, $Subject, $body, $headers);
    }

    additionally, did you know that the publish_post hook gets executed anytime a post is saved and has a status of published?.. i.e. when it’s first converted from draft to published, if you edit it, and then save it, etc…

    Thread Starter oKikos

    (@okikos)

    Dear luckdragon, firstly thank you very much for your help, but unfortunately it did not do anything. I replaced my code with yours (putting 2 different emails of mine in from and to), then published the post and did not get any email. Also tried updating the post, publishing a new one.
    I have just <?php to begin with then the plugin info, then your code and ?> to finish with.

    If I am again making a mistake I apprecaite the help.

    Thank you!

    Thread Starter oKikos

    (@okikos)

    oh it now works after i added back the add_action(‘publish_post’,’send_email’);
    but no subject in the email

    probably because you are defining it as $subject, but passing it as $Subject

    Thread Starter oKikos

    (@okikos)

    Brilliant! Thank you for helping a silly newbie (=me) luckdragon! ??
    Have a wonderful day!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘I wrote this simple plugin but its not working – help’ is closed to new replies.