【问题标题】:Adding user meta values to Wordpress admin emails将用户元值添加到 Wordpress 管理员电子邮件
【发布时间】:2016-10-04 03:18:27
【问题描述】:

当新用户在我们的网站上注册时,他们需要填写公司信息。此信息存储在 _usermeta 表中,meta_key 为“公司”。

我要做的就是将此信息包含在 Wordpress 发送给站点管理员的通知电子邮件中。我在处理 pluggables.php(默认电子邮件代码所在的位置)时遇到了一些运气,但我无法在电子邮件中发送任何元值。

这是我当前的代码:

function wp_new_user_notification($user_id, $plaintext_pass = '') {
    $user = get_userdata( $user_id );
    $user_meta = get_user_meta( $user_id );
    $company = $user_meta['company'][0];

// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

$message  = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
$message .= sprintf(__('Name: %s'), $user->display_name) . "\r\n\r\n";
$message .= sprintf(__('E-mail: %s'), $user->user_email) . "\r\n\r\n";
$message .= sprintf(__('Company: %s'), $company) . "\r\n";

@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

代码输出:

在您的网站 mywebsite 上注册新用户

姓名:名姓

电子邮件:email@example.com

公司:

我已包含 get_user_meta() 和 get_metadata() 但值始终为空。

非常感谢任何帮助。

【问题讨论】:

  • 您是否检查过company 在数据库中是否存在一个sp。用户与否?可能是在用户注册时,company 元没有被插入到数据库中。
  • 我检查了这个...对于每个 id,一个 meta_key 生成为“company”,并根据他们的提交添加一个 meta_value,例如“麦当劳”
  • 好的,现在在wp_mail 之前使用die(); 并打印get_user_meta( $user_id ) 并检查它是否返回company
  • 你可能没有正确拉取数据,直接拉取公司密钥$company = get_user_meta( $user_id, 'company', true);
  • 感谢您的建议...直接提取公司密钥具有相同的结果,并且 print get_user_meta( $user_id ) 返回了公司的空值:/

标签: php wordpress email


【解决方案1】:

我发现了这个问题。我用来创建新用户(个人资料新闻)的插件正在发布新用户,触发 wp_new_user_notification,然后将自定义值添加到元表中。我将元表函数移到 wp_new_user_notification 上方,并且数据正在按预期传输。如果其他人遇到这个问题,这里是如何解决它:

在 wp-includes/pluggable.php 中,以下内容按预期工作:

$company = get_user_meta( $user_id, 'company', true ); 
$message .= sprintf(__('Company: %s'), $company) . "\r\n";

至于配置文件,导航到 wp-content/plugins/profilepress/classes/class-registration-form-auth.php 并输入:

// register custom profile field
if ( ! is_wp_error($user_id)) {
.
//truncated
.
            do_action('pp_after_custom_field_update', $key, $value, $user_id, 'registration');
        }
    }

以上:

    if (is_int($user_id) && 'enable' == $new_user_notification) {
        wp_new_user_notification($user_id, null, 'admin');
    }

希望这可以帮助遇到类似问题的其他人。特别感谢@RaunakGupta 为我指明了方向,并感谢他们的代码。

【讨论】:

    猜你喜欢
    • 2019-01-27
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多