0

I have a custom post type «teams» with a set of different custom fields (ACF). I'm not a big coder, but hope to get some guidance in how to create a function that sends an email including ACF every time one of the teams (CPT) is updated by a WP-user.

I've found plugins that accomplish this, but unfortunately I need a bit more complex solution. The problem is the email should ONLY include custom fields that actually has changed. So I need to loop through and check each custom field for any change, and then only include the fields that has a new value in the final email.

I guess I'm asking if this is a very difficult task, or could it be done in a rather simple way? All sources would be very appreciated!

2
  • You need to expand on your question. How CPT is linked to users? – Fresz Feb 17 at 22:43
  • The link between users and the CPT is as simple as it can get. Each team has it's own WP-account (contributor role). All users with contributor role are given access to publish and make changes to the CPT «teams». There are no restrictions, so in theory the administrator (user) of a specific team can make changes to ALL of the other teams. I understand that this is not a recommended solution, but there is no great danger of users abusing the system. The only thing I need is a function that sends an notification email to a custom address each time one of the teams is updated with new info. – Michael Feb 18 at 19:11
0

From your explanation, it seems that we need wp_mail() function and post_updated hook.

wp_mail() reference: https://developer.wordpress.org/reference/functions/wp_mail/ post_updated action hook: https://developer.wordpress.org/reference/hooks/post_updated/

<CPT_name> - replace this with your custom post type key.

(Untested code - it should give you a good idea of what needs to be done)

add_action('post_updated', `send_custom_update`, 10, 3);
function send_custom_update($post_ID, $post_after, $post_before) {

  if(get_post_type($post_ID) == '<CTP_name>') {

     $to = 'sendto@example.com';
     $subject = get_the_title($post_ID) . ' has been updated!';
     $body = 'The email body content';
     $headers = array('Content-Type: text/html; charset=UTF-8');
 
     wp_mail( $to, $subject, $body, $headers );

  }
}

If you look at the documentation of this action hook you will see that there are two versions of the post available - before and after edit. You might use them in the $body - but that is up to you.

Let me know if this helps.

0

Thank you so much, Fresz! That helped me a lot :)

I found another hook from Advanced Custom Fields (acf/save_posts), and now have a working (I think) solution!

Reference: https://www.advancedcustomfields.com/resources/acf-save_post/

I've just tested by checking the old and new value of one of the custom fields, and the function seems to do the trick. Next question; how can this be done a lot cleaner? I have 8 custom fields, and checking all of them in the way I do in the code below seems very amateurish. Is there a better way to set up this «check old versus new value» for all the 8 fields?

add_action('acf/save_post', 'teams_update_notification', 5);
function teams_update_notification( $post_ID ) {

// Check that it is the correct post type
if(get_post_type($post_ID) == 'teams') {

// Get post name
$post_title = get_the_title( $post_id );

// New and old field value
$team_name = get_field('team_name', $post->ID);
$team_name_new =  '';
    if (isset($_POST['acf']['field_12345'])) {
        $team_name_new = $_POST['acf']['field_12345'];
    }
if ($team_name_new != $team_name) {
$team_name_updated = $_POST['acf']['field_12345'];
}

// SET UP MAIL
$subject = $post_title . ' has been updated';
$headers = array('Content-Type: text/html; charset=UTF-8');
$message .= $post_title . 'has been updated' . "\r\n";
$message .= $team_name_updated;

// Send email to admin.
wp_mail( 'mail@domain.com', $subject, $message, $headers );

}

}

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.