0

What I have

I am using the "The Events Calendar" plugin on my site to coordinate a team of employees. I want to be able to send an automated email containing the body of an event post (post-type is not $post, but $tribe_events instead) each time it's updated, but only to the employees concerned by that post.

To get the email addresses, I have set up a meta box in the $tribe_events post-type editor as a plugin based off something I found online for adding social media buttons and it works fine:

add_action( 'add_meta_boxes', 'add_custom_box' );

function add_custom_box( $tribe_events ) {
    add_meta_box(
        'animateurs', // ID, should be a string.
        'Animateurs', // Meta Box Title.
        'social_services', // Your call back function, this is where your form field will go.
        'tribe_events', // The post type you want this to show up on, can be post, page, or custom post type.
        'side', // The placement of your meta box, can be normal or side.
        'core' // The priority in which this will be displayed.
    );
}

function social_services( $tribe_events )
{
// Get post meta value using the key from our save function in the second paramater.
$custom = get_post_meta($tribe_events->ID, '_social_services', true);

?>
    <input type="checkbox" id="anim_patrick" name="social_services[]" value="patrick@example.com" <?php echo (in_array('patrick@example.com', $custom)) ? 'checked="checked"' : ''; ?>>
    <label for="anim_patrick"></label>Patrick<br>

    <input type="checkbox" id="anim_jeff" name="social_services[]" value="jeff@example.com" <?php echo (in_array('jeff@example.com', $custom)) ? 'checked="checked"' : ''; ?>>
    <label for="anim_jeff"></label>Jeff<br>
<?php
}     

function save_extra_fields(){
global $post;

if(isset( $_POST['social_services'] ))
{
    $custom = $_POST['social_services'];
    $old_meta = get_post_meta($post->ID, '_social_services', true);
    // Update post meta
    if(!empty($old_meta)){
        update_post_meta($post->ID, '_social_services', $custom);
    } else {
        add_post_meta($post->ID, '_social_services', $custom, true);
    }
  }
// update_post_meta($post->ID, "producers", $_POST["producers"]);
}
add_action( 'save_post', 'save_extra_fields' );

The result

What the above code adds to the editor

This successfully saves the email addresses of the employees concerned by the event post.

I can use this code to show the array of those email addresses (on a page - not what I want to do. This is purely to show how to get the email addresses from the array):

<?php $meta = get_post_meta($post->ID, '_social_services', true); $anims = implode (", ", $meta); echo $anims; ?>

What I need

Now I'm just stuck on the last part, which is to send an email containing the new updated post content to the email addresses contained in $anims every time a $tribe_events post-type is updated.

How can I achieve that?

PS: I'm a nube at PHP. This is my first attempt at something like this.

  • If you found the solution then mark your own answer as accepted to signal the question is solved. – Webloper Jul 26 '15 at 17:27
1

You are almost there. You just need to use wp_mail to send emails containing in your array.

Here is how you can achieve that using save_post action hook.

/**
 * To send email on updating your event post
 *
 * @param int $post_id The post ID.
 * @param post $post The post object.
 * @param bool $update Whether this is an existing post being updated or not.
 */
function send_email_on_event_update( $post_id, $post, $update ) {

    $post_type = 'tribe_events';  // your post type

    // If this isn't a 'tribe_events' post, No need to send emails
    if ( $post_type != $post->post_type ) {
        return;
    }

    // - Now to Send emails

    // first get array of your emails
    $all_emails = get_post_meta($post_id , '_social_services', true); // this is suppose to be variable containing array of emails


    $post_title = get_the_title( $post_id );
    $post_url = get_permalink( $post_id );
    $subject = $post_title;

    $message = "This event has been updated:\n\n";
    $message .= $post_title . ": " . $post_url;
    $message .= "keep adding what ever you want to send in this email...";


    // Send email to $all_emails.
    wp_mail( $all_emails, $subject, $message );

}
// have kept the priority to 11 to make sure we get the content for email after every thing is saved
add_action( 'save_post', 'send_email_on_event_update', 11, 3 ); 

Haven't test this code yet. But I'm sure this would work for you.

| improve this answer | |
  • Hi. Thanks for your help but it doesn't seem to be working for me. – xtojump Jul 26 '15 at 14:13
  • Are you sure $all_emails is an array of email ids? – Mohammad Mursaleen Jul 26 '15 at 14:18
  • When I try putting <?php $meta = get_post_meta($post->ID, '_social_services', true); $anims = implode (", ", $meta); echo $anims; ?> into a template (like footer.php) it shows the email addresses that correspond to the boxes I have ticked for that post in this format : patrick@example.com, jeff@example.com – xtojump Jul 26 '15 at 14:26
  • OK just updated code. Actually your $meta is the array containing all email ids which you are converting into string using implode. – Mohammad Mursaleen Jul 26 '15 at 15:00
  • It still doesn't seem to be working. Is there anything I should check that might be affecting it? I've put your code just after mine in the same plugin. – xtojump Jul 26 '15 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.