在元值更新时生成电子邮件

时间:2017-04-19 作者:Akshat

我为用户实现了一些元字段,如手机号码、地址。它可以由用户和管理员更新,我不想触发一封电子邮件给用户和管理员,说明更新的字段值,如果任何值被更新,无论谁更新它。

2 个回复
SO网友:Jeffrey von Grumbkow

您可以使用profile_update hook 在保存后执行操作

function wpse_264096_send_email( $user_id, $old_user_data ) {

    // Get the user object for $user_id
    $user = get_user_by( \'ID\', $user_id );
    $user_email = $user->data->email;

    /** You can get any meta fields by this, just change \'meta_key\' to the key desired
      * True means its a single value, it will output as a string.
      * If you set to false you get an array with the meta_value, handy if there are multiple values.
      */
    $user_meta = get_user_meta( $user_id, \'meta_key\', true );

    // Send e-mail to the user
    $message = "Your information has changed. Your new <META KEY> is now <$user_meta>";
    wp_mail( $user_email, \'Your information has been updated\', $message );

    // Send e-mail to the site admin
    $message = "A users information has changed. The new <META KEY> is now <$user_meta>";
    wp_mail( get_option( \'admin_email\' ), \'Your information has been updated\', $message );

}
add_action( \'profile_update\', \'wpse_264096_send_email\', 10, 2 );

SO网友:Ismail

WordPress在更新某个元时触发一个自定义挂钩,用户元将计数。看见updated_{$meta_type}_meta. 下面是一个示例用例:

add_action(\'updated_user_meta\', function( $meta_id, $user_id, $meta_key, $_meta_value ) {
    switch ( $meta_key ) {
        case \'mobile_number\':
        case \'address\':

            $user = new \\WP_User($user_id);

            // user
            wp_mail($user->user_email, \'Profile updated\', \'Profile updated.\');

            // admin
            wp_mail(get_bloginfo(\'admin_email\'), \'Profile updated\', "Profile updated for user {$user->display_name}.");
            break;
    } 
}, 0, 4);

相关推荐

Wp-login.php输入密码什么都不会发生

我有一个prettypeople网站。无论何时我尝试以管理员身份登录,无论是在/wp admin还是/wp登录并输入正确的凭据,都不会发生任何事情!页面刷新,如果我检查日志,我找不到任何东西!我能做什么?链接更改自prettypeople.nl/wp-login 收件人:https://prettypeople.nl/wp-login.php?redirect_to=https%3A%2F%2Fprettypeople.nl%2Fprettypeople%2F%2Fwp-admin%2F&reau