Action hook "user_registration_after_register_user_action"

Home Forums User Registration Action hook "user_registration_after_register_user_action"

This topic contains 3 replies, has 2 voices, and was last updated by  rivan 1 year, 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #3122

    rivan
    Participant

    Hi there !

    I am trying to intercept the registration form that the user fill in order to use the forms information differently (such as change the user’s role, color theme, …). Here is the code I have written to do that but unfortunately during its execution it doesn’t change anything and even create an error message:

    Unexpected end of JSON input [}

    This error doesn’t interupt the users creation.

    
    add_action( "user_registration_after_register_user_action", "test_user_registration" );
    function test_user_registration( $a, $b, $user_id ) {
        $roles = $_POST['role_checkbox'];
        if( count($roles) == 2 ){
            $new_role = 'role3';
            update_user_meta( $user_id, 'role', $new_role );
        }
        elseif ( in_array('cond0', $roles) ) {
            $new_role = 'role1';
            update_user_meta( $user_id, 'role', $new_role );
        }
        elseif (in_array('cond1', $roles) ) {
            $new_role = 'role2';
            update_user_meta( $user_id, 'role', $new_role );
        }
    }
    

    Thank you in advance for your help !

    #3123

    sanjeev3
    Member

    Hello Rivan,

    Can you please show us the values from $_POST['role_checkbox']? Also make sure that $_POST['role_checkbox'] is set and not empty. Any PHP errors or warnings here will result in this error as ajax is being used. Hope you have debug log on too.

    In your config.php
    define( 'WP_DEBUG', true);

    Regards,
    WPEverest Support

    #3124

    rivan
    Participant

    Indeed the

    $_POST[‘role_checkbox’]

    don’t give me the values of the checkbox that I made using the UserRegistration form creator, in other terms my array

    $roles

    is empty … So how can I get the values of a custom checkbox which Field Name is role_checkbox ?

    #3126

    rivan
    Participant

    Okay I finally find out how to do a such thing, here is the solution for changing the users role for example.
    The $user_id parameter which is passed in the hook is made of following things:

    
    [07-Aug-2018 16:28:36 UTC] This is the content of user_id element: array(4) {
      ["user_login"]=>
      object(stdClass)#12761 (5) {
        ["value"]=>
        string(5) "test3"
        ["field_type"]=>
        string(4) "text"
        ["label"]=>
        string(8) "Username"
        ["field_name"]=>
        string(10) "user_login"
        ["extra_params"]=>
        array(2) {
          ["field_key"]=>
          string(10) "user_login"
          ["label"]=>
          string(8) "Username"
        }
      }
      ["user_email"]=>
      object(stdClass)#12762 (5) {
        ["value"]=>
        string(14) "test3@test.com"
        ["field_type"]=>
        string(5) "email"
        ["label"]=>
        string(10) "User Email"
        ["field_name"]=>
        string(10) "user_email"
        ["extra_params"]=>
        array(2) {
          ["field_key"]=>
          string(10) "user_email"
          ["label"]=>
          string(10) "User Email"
        }
      }
      ["user_pass"]=>
      object(stdClass)#12763 (5) {
        ["value"]=>
        string(4) "test"
        ["field_type"]=>
        string(8) "password"
        ["label"]=>
        string(13) "User Password"
        ["field_name"]=>
        string(9) "user_pass"
        ["extra_params"]=>
        array(2) {
          ["field_key"]=>
          string(9) "user_pass"
          ["label"]=>
          string(13) "User Password"
        }
      }
      ["role_checkbox"]=>
      object(stdClass)#12765 (5) {
        ["value"]=>
        string(45) "cond1,cond2,cond3,cond4"
        ["field_type"]=>
        string(8) "checkbox"
        ["label"]=>
        string(12) "Role chooser"
        ["field_name"]=>
        string(13) "role_checkbox"
        ["extra_params"]=>
        array(2) {
          ["field_key"]=>
          string(8) "checkbox"
          ["label"]=>
          string(12) "Role chooser"
        }
      }
    }
    

    Where role_checkbox is the fields name that I gave to the custom Checkbox in the UserRegistration form creator !
    So to use it one can do it like that:

    
    add_action( "user_registration_after_register_user_action", "user_registration_role_checkbox" );
    function user_registration_role_checkbox( $user_id ) {
    
        $role = $user_id["role_checkbox"]->value;
    
        if( strpos($role, "cond1")!==FALSE && strpos($role, "cond2")!==FALSE ){
            $new_role = 'role3';
            $user = new WP_User( $user_id["user_login"]->value );
            $user->set_role($new_role);
        }
        elseif ( strpos($role, "cond1")!==FALSE ) {
            $new_role = 'role1';
            $user = new WP_User( $user_id["user_login"]->value  );
            $user->set_role($new_role);
        }
        elseif ( strpos($role, "cond2")!==FALSE ) {
            $new_role = 'role2';
            $user = new WP_User( $user_id["user_login"]->value );
            $user->set_role($new_role);
        }
    }
    

    Just to explain a little bit: here cond1, cond2, … are the choices that has the user in the checkbox. When he validates the form all the conditions that were checked will be listed (separated by a comma) in the role_checkbox field of the $user_id array. Then in the code I have chosen for example that if cond1 and cond2 were checked => role3 is assigned to the user, if only cond1 => role1 and if only cond2 => role2.

    That’s it !

    Hope that it can help some other programmers 😉

    Rivan

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.