WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having trouble of getting the ID in user_register filter in functions.php...

Here is what I got

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 <xxx@xxx.com>';
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
wp_mail('xxx@xxx.com', 'New User Registration to xxx website', $message, $headers);

I am testing the getunion if its empty I am returning "nothing there".

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

When I pass manually the ID here

$getunion = $wpdb->get_results("SELECT value from wp_bp_xprofile_data where user_id = $get_id AND field_id = 4", "ARRAY_N");

it works... So how can I get the ID?

share|improve this question
up vote 0 down vote accepted

You are giving $wpdb->get_results a string for its second parameter. It should be a constant-- ARRAY_N-- meaning you need to remove the quotes

$getunion = $wpdb->get_results(
  "SELECT value from wp_bp_xprofile_data where user_id = $getid AND field_id = 4", 

Since you claim it works in the second instance, I am assuming $wpdb compensates, but I haven't verified that. Thought I'd mention it.

I think your problem is here:

    $user = new WP_User($user_id);
    $user_login = stripslashes($user->user_login);
    $user_email = stripslashes($user->user_email);
    $getid      = $user->ID;

You have your user ID coming into the function as $user_id. I don't know why you are creating another user. I believe that replacing those four lines with the following with sort things out.

    $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
share|improve this answer

From the doc:

This hook allows you to access data for a new user immediately after they are added to the database. User id is passed to hooked functions as an argument.

You already have the newly-created user ID passed into the function as $user_id. What you're doing is you're calling new WP_User() with an already existing ID, so that's probably going to fail every time.

share|improve this answer
new WP_User($user_id) creates a new object instance of the WP_User class. It should be passed an existing ID... – Johannes Pille Jul 10 '13 at 16:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.