1

I am trying to write a function that emails the site admin when a user profile reaches 100%.

I found this piece of code that sends an admin email when a post is published:

function send_admin_email($post_id){
    $to = '[email protected]';
    $subject = 'mail subject here';
    $message = "your message here ex: new post published at: ".get_permalink($post_id);
    wp_mail($to, $subject, $message );
}

And I have this value from the profile plugin I'm using:

$user_progress['completion_percentage'] == 100

Instead of sending an email when a post is published, I need to send an email when a user's profile reaches 100%.

Unfortunately, it's over my head as to how to achieve what I want to do. Can someone point me in the right direction?

1
  • The problem you're going to have to overcome is WHERE/WHEN to execute a check for the profile completion value and if the check shows that the value is 100% you then send the email. Since you're using a third party plugin for the profiles, you're going to have to check their documentation and see if there's a logical place to add your mail function. For example, the profile plugin has a save methodology and that's probably where it updates the 'completion_percentage' value, so you'd look to hook into that process, check the value after it's increased and then send the email. Jun 28, 2023 at 14:34

1 Answer 1

0

Based on your question, you could simply call the WordPress function

wp_mail( $to, $subject, $message, $headers );

when your var $user_progress['completion_percentage'] equal 100 %.

<?php 
if( $user_progress['completion_percentage'] === 100 ) {
  // Complete your variables with original code
  $to = "[email protected]";
  $subject = __("New user registration", "yourdomain");
  $headers = array('Content-Type: text/html; charset=UTF-8');
  $message = "A new user have completed his/her profile" . $user['id'];
  
  // Send the email
  wp_mail( $to, $subject, $message, $headers );

}
?>

Source : https://developer.wordpress.org/reference/functions/wp_mail/

2
  • This will be heavily dependent on where it's placed though - you have to tie this functionality into the process that checks and updates the $user_progress function. Can OP please show the code they wrote for the user as I'm assuming they have some custom fields/user meta data that they're recording. Jun 28, 2023 at 14:28
  • Actually, I read it again, there's a third-party profile plugin, so OP will have to check with the plugin's devs to see if there's a place to hook into the plugin's process where this code can be executed - otherwise, it'll have to be a CRON that checks for newly completed profiles and I would strongly advise against that. Jun 28, 2023 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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