我正在尝试创建一个功能,当用户更新仅特定配置文件字段(例如ID为8,9,10的字段)时,它将向管理员发送通知电子邮件。我不在乎通过电子邮件发送更新后的值。我只想收到通知,其中一个特定字段已更新。
我做了这样的事情
add_action('xprofile_profile_field_data_updated', 'checkField', 10, 2);
function checkField($field_id, $value) {
// Getting current logged in user's info
$current_user = wp_get_current_user();
// Checking the hidden value and if it has any value
if($field_id == 8 && $value != '')
// Sending email
wp_mail( 'YOUR_EMAIL_ADDRESS ', 'Phone no is changed', $current_user->user_login . ' has changed his phone number ');
if($field_id == 9 && $value != '')
// Sending email
wp_mail( 'YOUR_EMAIL_ADDRESS ', 'Address is changed', $current_user->user_login . ' has changed his address ');
if($field_id == 10 && $value != '')
// Sending email
wp_mail( 'YOUR_EMAIL_ADDRESS ', 'Email is changed', $current_user->user_login . ' has changed his email ');
}
但是即使更新其他任何个人资料字段,它也会向我发送电子邮件。我只是想收到电子邮件仅在其中一个配置文件字段已更新时。我不明白xprofile_profile_field_data_updated的工作方式,或者我是否需要其他钩子。
将提供任何帮助。
所以,我开始工作了。如果有人在搜索类似内容,则为:
function buddypress_profile_update( $user_id, $posted_field_ids, $errors,$old_values, $new_values ) {
$admin_email = "[email protected]";
if ($old_values[4]['value'] !== $new_values[4]['value']) {
$message = sprintf( __( 'There was a change in the profile of the member: <strong>%1$s</strong>', 'buddypress' ), bp_core_get_user_displayname( $user_id ) ) . "\r\n\r\n";
$message .= sprintf( __( '<p>The field <strong>Phone</strong> was <br /><hr><br /> %s <br /><hr><br />and now it is:<br /><hr><br /> <strong> %s </strong><hr></p>' ), $old_values[4]['value'], $new_values[4]['value'] ). "\r\n\r\n";
wp_mail( $admin_email, sprintf( __( 'Change in members Profile' ), get_option('blogname') ), $message );
}
if ($old_values[5]['value'] !== $new_values[5]['value']) {
enter code here
wp_mail(.......
}
add_action( 'xprofile_updated_profile', 'buddypress_profile_update', 10, 5 );
如果您具有复选框作为字段,则可以为此使用json_encode
来提取数据。
例如
$message .= sprintf( __( '<p>The field <strong>Checkbox</strong> was <br /><hr><br /> %s <br /><hr><br />and now it is:<br /><hr><br /> <strong> %s </strong><hr></p>' ), json_encode($old_values[5]['value']), json_encode($new_values[5]['value']) ). "\r\n\r\n";
不要忘记将字段ID放在[ID]中的每个ID中>