使用USER_REGISTER操作获取unctions.php中的用户ID

时间:2013-02-07 作者:Mario

我在函数的user\\u register filter中获取ID时遇到问题。php。。。

这是我得到的

add_action( \'user_register\', \'sendanotheremail\' );
function sendanotheremail($user_id) {
        $user = new WP_User($user_id);
        $user_login = stripslashes($user->user_login);
        $user_email = stripslashes($user->user_email);
        $getid      = $user->ID;
        global $wpdb;
        $getunion = $wpdb->get_results("SELECT value from wp_bp_xprofile_data where user_id = $getid AND field_id = 4", "ARRAY_N");
        $message  = sprintf(__(\'New user registration on %s:\'), get_option(\'blogname\')) . "\\r\\n\\r\\n";
        $message .= sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n\\r\\n";
        $message .= sprintf(__(\'E-mail: %s\'), $user_email) . "\\r\\n";
        $message .= "<br /> Please go here to approve the new user <a href=\\"http://xxx.com/wp-admin/admin.php?page=bp_registration_options_member_requests\\">Approve/Deny</a>";
        if(!($getunion)) {
                $message .= "nothing there";
        }
        else {
                $message .= "yes";
        }
$headers = \'From: xxx <[email protected]>\';
add_filter(\'wp_mail_content_type\',create_function(\'\', \'return "text/html";\'));
wp_mail(\'[email protected]\', \'New User Registration to xxx website\', $message, $headers);
}
我正在测试getunion,如果它是空的,我将返回“nothing there”。

So all of the other variables are getting me email with right data, but $getunion is empty...

当我在这里手动传递ID时

$getunion = $wpdb->get_results("SELECT value from wp_bp_xprofile_data where user_id = $get_id AND field_id = 4", "ARRAY_N");
它起作用了。。。那我怎么才能拿到身份证呢?

2 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

你在付出$wpdb->get_results 第二个参数的字符串。它应该是一个常数--ARRAY_N-- 意味着您需要删除引号

$getunion = $wpdb->get_results(
  "SELECT value from wp_bp_xprofile_data where user_id = $getid AND field_id = 4", 
  ARRAY_N
);
既然你声称它在二审中有效,我假设$wpdb 补偿,但我还没有证实。我想我应该提一下。

我认为你的问题在于:

    $user = new WP_User($user_id);
    $user_login = stripslashes($user->user_login);
    $user_email = stripslashes($user->user_email);
    $getid      = $user->ID;
您的用户ID作为$user_id. 我不知道你为什么要创建另一个用户。我认为,用以下内容替换这四行可以解决问题。

    $user = get_user_by(\'id\',$user_id);
    $user_login = stripslashes($user->user_login);
    $user_email = stripslashes($user->user_email);
    // $getid      = $user->ID; // you already have the user ID -> $user_id; use that below or change this line to
    $getid = $user_id; // if you must

SO网友:montrealist

从文档:

此挂钩允许您访问新用户的数据immediately after 它们将添加到数据库中。用户id作为参数传递给挂钩函数。

您已经将新创建的用户ID作为$user_id. 你所做的就是打电话new WP_User() 有一个已经存在的ID,所以可能每次都会失败。

结束

相关推荐

Accessing widget information

我正在尝试访问保存在wp_options 表我打印出来了:add_filter(\'the_content\', function(){ $res = get_option(\'widget_my_widget\'); print_r($res); }); 它输出了这个:Array ( [2] => Array ( [title] => asian kung-fu generation [description] => awesome b