【发布时间】:2018-04-14 19:54:22
【问题描述】:
我正在尝试创建一个功能,当用户更新她/他的个人资料时,管理员会收到邮件通知。不是存储在 wp_users 中的数据,我想知道存储在 wp_usermeta 中的更改。实际上,使用 Ultimate Member 创建的元密钥非常多。
电子邮件应该只包含更改后的值,最好也显示旧值。
因为我正在使用UltimateMember 插件。 根据this 网站,我需要这个才能开始:
function action_um_after_user_account_updated( $get_current_user_id ) {
// make action magic happen here...
};
add_action( 'um_after_user_account_updated', 'action_um_after_user_account_updated', 10, 1 );
经过大量搜索并且主要基于this,我想出了这个:
function action_um_after_user_account_updated( $get_current_user_id, $prev_value) {
$key = 'name';
$user = get_user_meta( $user_id, $key, $single);
$single = true;
if($prev_value->$key != $user->$key) {
$admin_email = "admin@site.com";
$message .= sprintf( __( 'New Name is: %s' ), $user ). "\r\n\r\n";
$message .= sprintf( __( 'Old name was: %s' ), $prev_value ). "\r\n\r\n";
wp_mail( $admin_email, sprintf( __( '[DB] Name changed' ) ),$message );
}
};
// add the action
add_action( 'um_after_user_account_updated', 'action_um_after_user_account_updated', 10, 1 );
好吧,它根本不起作用。我不知道我是否有 php 代码问题,或者代码是否已过期,但我无法让它工作。
据我所知,我还包括了我需要使用 wp_mail 的 pluggable.php。 (include ABSPATH . WPINC . '/pluggable.php';) 在我的主题 (smartpress) 的头文件中。
- Wordpress 版本:4.8.2
- 终极会员版本:1.3.88
- PHP 版本:5.6
更新:
我现在做了一个插件,它有点工作。我收到一封邮件,并从提供的 meta_keys 中获取值。现在,我不想在邮件中显示每一个 meta_value,只显示那些改变的。有什么方法可以在配置文件更新之前存储以前的值并与之进行比较或其他什么?
这是我当前的代码:
function profile_update_name() {
$user_id = get_current_user_id();
$single = true;
$user_fnm = get_user_meta( $user_id, 'firstnamemother', $single);
$user_lnm = get_user_meta( $user_id, 'nachnamemother', $single);
$admin_email = "admin@site.com";
$message .= sprintf( __( $user_fnm .' '. $user_lnm . ' has updated the profile.')). "\r\n\r\n";
$message .= sprintf( __( 'New Name is: %s' ), $user_fnm .' '. $user_lnm ). "\r\n\r\n";
$message .= sprintf( __( 'Old name was: %s' ), $user_lnm ). "\r\n\r\n";
wp_mail( $admin_email, sprintf( __( '[DB] Name changed' ) ),$message );
};
// add the action
add_action( 'um_user_after_updating_profile', 'profile_update_name', 1, 10 );
【问题讨论】: