首先,添加一个自定义操作挂钩,每当用户更新时就会触发该挂钩。
例如,在WordPress中更新自定义元字段时,一些操作挂钩,如:updated\\u{$meta\\u type}\\u meta、updated\\u posmeta等。
因此,如果您使用的是ACF插件,则可以挂接到“ACF/save\\u post”操作挂钩。操作挂钩允许您在更新自定义字段之前或之后执行某些操作。有关使用acf/save\\u帖子的更多信息,click here.
接下来,您可以使用wp\\u mail()函数向用户发送电子邮件。
根据您提供的代码,我进行了一些重构:
add_action(‘acf/save_post’, ‘my_save_post’);
function my_save_post( $post_id ) {
// create some logic here to check if you are editing a user
// Keep an eye on this pagenow check to see if it\'s correct
// Use wp_die( var_dump( $pagenow ); to debug
global $pagenow;
if ($pagenow == ‘user-edit.php’) {
//get the value of the field
$value = get_field(‘login_allowed’,$post_id);
// Remove this, just here for debugging
// wp_die( var_dump( $value ) );
// check if the checkbox is filled
if ( $value == \'unchecked\' ) {
return false;
}
// Company information
$email = “removed”;
$name = “removed”;
// Debug: I see $useremail variable, is it set somewhere?
//get user\'s email
$user = get_user_by(\'email\', $useremail);
if ($user) {
$details[\'email\'] = sanitize_email( $user->user_email );
// email data
$to = $useremail;
$subject = \'The subject\';
$body = \'The email body content\';
$headers = ‘From:‘ . $name . ‘ ’ . “\\r\\n”;
// send email
wp_mail($to, $subject, $body, $headers );
}
}
}