admin管理员组

文章数量:1435262

What's the best way to send a email from Wordpress after a certain action.

I'm working on a site where users can submit ideas and then other users can comment on them.

I would like to send emails to a number of different addresses (moderators) when a post is made and when a comment is made.

I have this code to add the posts with an attempt to send an email that doesn't work.

<section class="submitForm">

    <h1>Submit idea</h1>

    <?php

    if(isset($_POST['new_post']) == '1'){

        $post_title = $_POST['post_title'];
        $post_content = $_POST['content'];

        $new_post = array(
            'ID' => '',
            'post-author' => $user->ID,
            'post_title' => $post_title,
            'post_content' => $post_content,
            'post_status' => 'publish'
            );

        $post_id = wp_insert_post($new_post);
        $post = get_post($post_id);
        wp_redirect($post->guid);
        exit;

        // send email notifications


        wp_mail( '[email protected]', 'New Idea', $post_content);
    }

    echo '<form method="post" action="" >';
    echo '<label for="idea_title">Idea Title</label>';
    echo '<input type="text" name="post_title" class="title">';

    $settings = array(
        'quicktags' => false,
        'textarea_name' => 'content',
        'media_buttons' => true,
        'textarea_rows' => 12,
        'tinymce' => array(
            'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
            'bullist,blockquote,|,justifyleft,justifycenter' .
            ',justifyright,justifyfull,|,link,unlink,|' .
            ',spellchecker'
            )
        );
    wp_editor( '', 'content', $settings );

    echo '<input type="hidden" name="new_post" value="1">';
    echo '<input type="submit" class="submit" value="Submit Idea">';
    ?>

</section>

What's the best way to send a email from Wordpress after a certain action.

I'm working on a site where users can submit ideas and then other users can comment on them.

I would like to send emails to a number of different addresses (moderators) when a post is made and when a comment is made.

I have this code to add the posts with an attempt to send an email that doesn't work.

<section class="submitForm">

    <h1>Submit idea</h1>

    <?php

    if(isset($_POST['new_post']) == '1'){

        $post_title = $_POST['post_title'];
        $post_content = $_POST['content'];

        $new_post = array(
            'ID' => '',
            'post-author' => $user->ID,
            'post_title' => $post_title,
            'post_content' => $post_content,
            'post_status' => 'publish'
            );

        $post_id = wp_insert_post($new_post);
        $post = get_post($post_id);
        wp_redirect($post->guid);
        exit;

        // send email notifications


        wp_mail( '[email protected]', 'New Idea', $post_content);
    }

    echo '<form method="post" action="" >';
    echo '<label for="idea_title">Idea Title</label>';
    echo '<input type="text" name="post_title" class="title">';

    $settings = array(
        'quicktags' => false,
        'textarea_name' => 'content',
        'media_buttons' => true,
        'textarea_rows' => 12,
        'tinymce' => array(
            'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
            'bullist,blockquote,|,justifyleft,justifycenter' .
            ',justifyright,justifyfull,|,link,unlink,|' .
            ',spellchecker'
            )
        );
    wp_editor( '', 'content', $settings );

    echo '<input type="hidden" name="new_post" value="1">';
    echo '<input type="submit" class="submit" value="Submit Idea">';
    ?>

</section>
Share Improve this question asked Jun 24, 2014 at 8:51 user51590user51590 331 gold badge1 silver badge5 bronze badges 1
  • Have you tested if wp_mail works on your site, outside a form? – Wyck Commented Jun 24, 2014 at 16:05
Add a comment  | 

2 Answers 2

Reset to default 0

Try to add some code to the functions.php like:

add_action('publish_post','send_email');

function send_email() { // write some code with wp_mail function. }

I think you can use the $post to get the post info.

For comments I guess you can use:

add_action('comment_post', 'send_email');

Let me know if you need some more help!

Your wp_mail() function has a wp_redirect() and an exit right in front of it. These things stop execution of the current script.

Move your mail function above the redirect.

本文标签: wp mailSend email from Wordpress