Support » Plugin: New User Approve » Get the user ID in the new_user_approve_notification_message_default filter

  • Resolved railmedia

    (@railmedia)


    Dear Josh,

    Thank you for a great plugin.

    I am trying to do the following: when a user registers on my website, the admin gets an email which can be customized using the new_user_approve_notification_message_default filter. Inside I am trying to display a link directly to the edit user screen in WP Admin (so that when the admin clicks the link in the email he will be taken directly in the user edit screen rather than on the user list screen) and for that I need the user ID. home_url().’/wp-admin/user-edit.php?user_id=’;

    I have tried to add a new email tag, but with no success.

    Here is the code I am using:

    add_action( 'nua_email_tags', 'add_email_tags_user_id');
    function add_email_tags_user_id( $email_tags ) {
    	$email_tags[] = array(
    		'tag'         => 'user_id',
    		'description' => __( 'The user ID.', 'mytheme' ),
    		'function'    => 'email_tag_user_id',
    		'context'     => array( 'email' ),
    	);
    
    	return $email_tags;
    }
    function email_tag_user_id( $attributes ) {
    	return $attributes['user_id'];
    }
    
    add_filter( 'new_user_approve_notification_message_default', 'user_registration_message', 10, 2 );
    
    function user_registration_message( $message ) {
    	$message = __( '{username} ({user_email}) has requested a username at {sitename}', 'new-user-approve' ) . "<br/>";
    	$message .= "{site_url}<br/>";
    	$message .= __( 'To approve or deny this user access to {sitename} go to', 'new-user-approve' ) . "<br/>";
    	//$message .= "{admin_approve_url}\n\n";
    	$user_id = $wpdb->get_var("SELECT ID FROM wp_users WHERE user_email='{username}'");
    	$message .= home_url().'/wp-admin/user-edit.php?user_id={user_id}';
    	return $message;
    }

    What am I doing wrong? Is it even possible to pull the ID in my case?

    Please advise.

    I look forward to hearing from you.

    Best regards,
    Adrian

    https://wordpress.org/plugins/new-user-approve/

Viewing 13 replies - 1 through 13 (of 13 total)
  • Anyone with this problem?

    Plugin Author Josh Harrison

    (@picklewagon)

    Replace the email_tag_user_id function with this one. It should give you what you want.

    function email_tag_user_id( $attributes ) {
    	$user = $attributes['user'];
    	return $user->ID;
    }

    Also, in the user_registration_message function, you do not need to add the database call to get the user ID.

    Let me know how it goes for you.

    Hi Josh,

    Thank you very much for your reply. Unfortunately this doesn’t solve my problem. It’s the same result, no user id.

    Regards,
    Adrian

    Any luck with finding another possible solution?

    Thanks

    I know this is a little old, but looking at your code it would seem to me that your SQL is looking for the wrong thing:

    Where you have

    $user_id = $wpdb->get_var("SELECT ID FROM wp_users WHERE user_email='{username}'");

    shouldn’t that be:
    $user_id = $wpdb->get_var("SELECT ID FROM wp_users WHERE user_email='{user_email}'");

    replacing the {username} with {user_email}

    Plugin Author Josh Harrison

    (@picklewagon)

    Good find.

    Hi Josh,

    Sadly, my correction of the SQL wasn’t the the issue, just a red herring.
    I too am struggling to get data out and pass it through on the email too. I have been in touch with @railmedia to see if he ever managed it – but he didn’t.

    Can you shed any light on how we might achieve this? I want to pass things like first name, lastname along with the username and email.

    Your help is most appreciated.
    Regards
    David

    Plugin Author Josh Harrison

    (@picklewagon)

    Send me all of the code you have and let me take a look. Where are you putting the code?

    Hi Josh,

    Thanks for taking a look. The code is below – but I’ve hacked about with it so much in my attempts to get it to work, I don’t know if I’m coming or going now.
    At this point I was just trying to get one additional field ‘first_name’ working before I added others.

    I have it in its own plugin:

    add_action( 'nua_email_tags', 'add_email_tags_first_name');
    
    function add_email_tags_first_name( $email_tags ) {
    
    	$email_tags[] = array(
    		'tag'         => 'firstname',
    		'description' => __( 'The users first name.', 'new-user-approve' ),
    		'function'    => 'email_tag_first_name',
    		'context'     => array( 'email' ),
    	);
    
    	return $email_tags;
    }
    
    function nua_email_tags_attributes ( $content, $attributes ){
    
    	// Replace all tags
    	$content = pw_new_user_approve()->email_tags->do_tags( $content, $attributes );
    
    	// Return content
    	return $content;
    
    }
    
    function email_tag_first_name( $attributes ) {
    	$user = $attributes['user'];
    	return $user->first_name;
    }
    
    add_filter( 'new_user_approve_notification_message_default', 'user_registration_message', 10, 2 );
    
    function user_registration_message( $message ) {
    	$message = __( '{firstname} {username} ({user_email}) has requested a username at {sitename}', 'new-user-approve' ) . "\n\n";
    	$message .= "{site_url}\n\n";
    	$message .= __( 'To approve or deny this user access to {sitename} go to', 'new-user-approve' ) . "\n\n";
    	$message .= "{admin_approve_url}\n\n";
    	return $message;
    }
    Plugin Author Josh Harrison

    (@picklewagon)

    Is the first name attribute the only email tag not working?

    In order to get the first name to return correctly and replace the email tag, you need to look in the right place for the first name attribute. It is in the usermeta table. Try using this function in place of what you have above:

    function email_tag_first_name( $attributes ) {
    	$user = $attributes['user'];
    	return get_user_meta( $user->ID, 'first_name', true );
    }

    Let me know how it goes.

    Hi Josh,

    Like I said I have been messing about with it so much…:)

    Anyway, the suggestion you had (above) had no effect. But…I did discover what the problem is!

    There is no [user] key in the $attributes array. The keys in the array are [context] [user_login] [user_email] [admin_url]

    So to fix it all I had to do was extract the user ID from the username and use that to do a lookup on the first name, last name etc.

    function email_tag_name( $attributes ) {
    	$user = get_user_by( 'login', $attributes['user_login'] );
    	$name = $user->first_name . " " . $user->last_name;
     return $name;

    Thank you for all your help. I’ll get back in touch with @railmedia and let him know.
    David

    Thanks David. The solution works.

    However, for me the original need was to pull the user id to reflect a direct link to user edit in the admin notification.

    Here’s the final code and how it looks on my end:

    In functions.php I added the below code:

    1. Add the new email tag for userid:

    add_filter( 'nua_email_tags', 'add_email_tags_user_id');
    function add_email_tags_user_id( $email_tags ) {
    	$email_tags[] = array(
    		'tag' => 'userid',
    		'Description' => __('Generates the user ID', 'new-user-approve'),
    		'function' => 'email_tag_user_id',
    		'context' => array('email')
    	);
    
    	return $email_tags;
    }

    2. The function David came up with to pull the user ID from the database:

    function email_tag_user_id( $attributes ) {
    	$user = get_user_by( 'login', $attributes['user_login'] );
    	$userID = $user->ID;
    	return $userID;
    }

    3. The function that sends the message to the admin when a new user registers on the website:

    add_filter( 'new_user_approve_notification_message_default', 'user_registration_message', 10, 2 );
    
    function user_registration_message( $message ) {
    	$message = __( '{username} ({user_email}) has requested a username at {sitename}', 'new-user-approve' ) . "<br/>";
    	$message .= "{site_url}<br/>";
    	$message .= __( 'To approve or deny this user access to {sitename} go to', 'new-user-approve' ) . "<br/>";
    	$message .= home_url().'/wp-admin/users.php?user_id={userid}';
    	return $message;
    }

    Hope this helps anybody in trouble.

    Regards,
    Adrian

    [SOLVED] I think we can close this topic now.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Get the user ID in the new_user_approve_notification_message_default filter’ is closed to new replies.