Forums

[Plugin: Theme My Login] add_action user_register does not work (18 posts)

  1. arbitrage
    Member
    Posted 1 year ago #

    Hi, I run the Theme My Login plugin on WordPress installation. Now, I try to update the user role on registration, but I cannot call my funtion via add_action('user_register', 'register_role') in functions.php:

    `add_action('user_register', 'register_role');

    function register_role($user_id) {

    update_user_meta( $user_id, 'wp_capabilities', 'myrole' );

    }

    During registration via the register-form.php at ?page_id=117action=register the function is ignored.

    What am I doing wrong? My intension is to set self-defined user roles on registration instead using the default role.

    Already tried the "WP Roles at Registration" plugin which funtions neither.

    Thanks for your help.

  2. Jeff Farthing
    Member
    Posted 1 year ago #

    I would do it this way:

    function register_role( $user_id ) {
    	// Instantiate a user object
    	$user = new WP_User( $user_id );
    
    	// Set your role
    	$user->set_role( 'myrole' );
    
    	// Destroy user object
    	unset( $user );
    }
    add_action( 'user_register', 'register_role' );
  3. arbitrage
    Member
    Posted 1 year ago #

    Thanks for the post. It is still not working. In my opinion it's not about the function, its about the failed calling of the function during registration.

    It seems that add_action( 'user_register', 'set_role_on_registration' ); does not work! I've tried to output an echo or alert in the function set_role_on_registration, but nothing happens.

    My current approach in functions.php:

    //create custom user role at registration
    ## example link: http://example.com/wp-login.php?action=register&role=my_role1
    //
    
    function show_role_field(){
    	?>
        <input id="role" type="hidden" tabindex="20" size="25" value="<?php if (isset($_GET['role'])){echo $_GET['role'];} ?>"  name="role"/>
    	<?php
    }
    add_action('register_form','show_role_field');
    
    function set_role_on_registration( $user_id ) {
    
    	$new_role = sanitize_text_field( $_POST['role'] );
    
    	//only allow if user role is one of defined roles
    
    	if (
    		($new_role == "my_role1") or
    		($new_role == "my_role2") )
    	{
    		// Instantiate a user object
    		$user = new WP_User( $user_id );
    
    		// Set your role
    		$user->set_role( $new_role );
    
    		// Destroy user object
    		unset( $user );	}
    }
    add_action( 'user_register', 'set_role_on_registration' );
    
    //end create user role at registration
  4. Jeff Farthing
    Member
    Posted 1 year ago #

    That should work. Just out of curiosity, try hooking set_role_on_registration() to tml_new_user_registered.

  5. arbitrage
    Member
    Posted 1 year ago #

    Thanks. But, doesn't work either. Could it be because of the user moderation by email verifcation? Because the role is "Pending" after registration and after email confirmation the role is set to the default role.

  6. arbitrage
    Member
    Posted 1 year ago #

    Yes. Seems that this was the reason. Due to the user moderation option "email verifcation", registering a individual role on registration does not work.

    Line 271 in user-moderation.php:

    $user_object = new WP_User( $user->ID );
    $user_object->set_role( get_option( 'default_role' ) );
    unset( $user_object );
  7. Jeff Farthing
    Member
    Posted 1 year ago #

    Well, of course. The role must be set to "Pending" until the user has activated it. What you can do is temporarily store the selected role via user meta using the tml_new_user_registered hook. Then hook set_role_upon_registration() to tml_new_user_activated.

    function set_role_on_registration( $user_id ) {
    	$role = sanitize_text_field( $_POST['role'] );
    
    	if ( in_array( $role, array( 'my_role1', 'my_role2' ) ) )
    		add_user_meta( $user_id, 'pending_role', $role );
    
    }
    add_action( 'tml_new_user_registered', 'set_role_on_registration' );
    
    function set_role_on_activation( $user_id ) {
    	if ( $pending_role = get_user_meta( $user_id, 'pending_role', true ) ) {
    		if ( !in_array( $pending_role, array( 'my_role1', 'my_role2' ) ) )
    			return;
    
    		$user = new WP_User( $user_id );
    
    		$user->set_role( $pending_role );
    
    		unset( $user );
    	}
    }
    add_action( 'tml_new_user_activated', 'set_role_on_activation' );
  8. arbitrage
    Member
    Posted 1 year ago #

    Thank you very much for the revised solution. I have now more detailed view on the hooks of WordPress and Theme My Login plugin.

    But anyhow I cannot receive the $_POST['role'] in function set_role_on_registration.

    function show_role_field(){
        ?>
        <input id="role" type="hidden" tabindex="20" size="25" value="my_role1" name="role"/>
        <?php
    }
    add_action('register_form','show_role_field');
    
    function set_role_on_registration( $user_id ) {
    
    	$role = 'my_role1'; // Works fine
            $role = sanitize_text_field( $_POST['role'] ); // Does not work
    
    	if ( in_array( $role, array( 'my_role1', 'my_role2' ) ) )
    		add_user_meta( $user_id, 'pending_role', $role );
    }
    add_action( 'tml_new_user_registered', 'set_role_on_registration' );
    
    function set_role_on_activation( $user_id ) {
    	if ( $pending_role = get_user_meta( $user_id, 'pending_role', true ) ) {
    
    		if ( !in_array( $pending_role, array( 'my_role1', 'my_role2' ) ) )
    			return;
    
    		$user = new WP_User( $user_id );
    
    		$user->set_role( $pending_role );
    
    		unset( $user );
    	}
    }
    add_action( 'tml_new_user_activated', 'set_role_on_activation' );;
    
    //end set user role at registration

    Do you any hint for me what I am doing wrong? Thanks in advance.

  9. Jeff Farthing
    Member
    Posted 1 year ago #

    That should work. Be sure that the hidden field is actually there.

  10. arbitrage
    Member
    Posted 1 year ago #

    Guess so, either. However, it does not. I checked if the hidden field actually exists and put it into the register form. But still nothing. I attached to action tag of the register form &role=myrole to get the value via GET. But still nothing!

    In the register form and before the submit button I put:

    <input type="hidden" name="role" value="bdonator" />

    and removed add_action('register_form','show_role_field');.

    In the function set_role_on_registration I put:

    $role = sanitize_text_field( $_POST['role'] );
    // $role = sanitize_text_field( $_GET['role'] );
  11. Jeff Farthing
    Member
    Posted 1 year ago #

    I hope you have changed the values in the "whitelist" roles array:

    if ( in_array( $role, array( 'my_role1', 'my_role2' ) ) )

    Of course, "my_role1" would work, as it's in the array. You must change those array values to allowed roles.

  12. arbitrage
    Member
    Posted 1 year ago #

    Thanks for the quick reaction. I did.

    Since it functions when I just put

    $role = 'my_role1';

    I am sure that the POST is the problem.

  13. Jeff Farthing
    Member
    Posted 1 year ago #

    Again, your response seems like you missed the point. Of course, giving the default values I wrote, $role = 'my_role1' would work, because it's in that array.

  14. arbitrage
    Member
    Posted 1 year ago #

    Sorry, my last response did confuse you. Here is the actual code with which I got the problem.

    In the regsiter form:

    <input type="hidden" name="role" value="bdonator" />

    In functions.php:

    function set_role_on_registration( $user_id ) {
    
    	//$role = sanitize_text_field( $_POST['role'] );
    	$role = 'bdonator';
    
    	if ( in_array( $role, array( 'bdonator', 'bprofessional' ) ) )
    		add_user_meta( $user_id, 'pending_role', $role );
    }
    add_action( 'tml_new_user_registered', 'set_role_on_registration' );
    
    function set_role_on_activation( $user_id ) {
    	if ( $pending_role = get_user_meta( $user_id, 'pending_role', true ) ) {
    
    		if ( !in_array( $pending_role, array( 'bdonator', 'bprofessional' ) ) )
    			return;
    
    		$user = new WP_User( $user_id );
    
    		$user->set_role( $pending_role );
    
    		unset( $user );
    
    	}
    }
    add_action( 'tml_new_user_activated', 'set_role_on_activation' );

    Thanks in advance for your patience :-)

  15. Jeff Farthing
    Member
    Posted 1 year ago #

    Can you verify if the user meta value ("pending_role") is being created?

  16. arbitrage
    Member
    Posted 1 year ago #

    I could not find where it is buffered. But when I directly set the value of role instead of $_POST['role'] the role is correctly defined after email confirmation of the user.

    //$role = sanitize_text_field( $_POST['role'] );
    $role = 'bdonator';
  17. arbitrage
    Member
    Posted 1 year ago #

    Maybe there is one more thing, you could help me. Do have any idea how to implement role based User Notification mails e.g. "New User" and "Account Activated" to the Theme My Login plugin?

  18. arbitrage
    Member
    Posted 1 year ago #

    Hi Jeff, many thanks for the support. I found the origin for the problem of passing the value from the POST variable role. It was a mistake in the form validation check.

Topic Closed

This topic has been closed to new replies.

About this Topic