Action hook "user_registration_after_register_user_action"

Home Forums User Registration Action hook "user_registration_after_register_user_action"

This topic contains 11 replies, has 9 voices, and was last updated by  dflynn742@gmail.com 2 weeks, 2 days ago.

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

    Anonymous

    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

    Anonymous

    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

    Anonymous

    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

    #48258

    Afflumad82
    Participant

    Student also need some registeration process which is one of the difficult thing to complete the writing work due to large number of registeration option.I have tried to check some of options which the browse around this site providing the help to students for completing the registeration options.

    #58650

    emmadavis
    Participant

    Are you looking for a reliable Chicago reference generator? Then Allessaywriter.com is the right place for you. We have designed the most advanced Chicago citation generator to make referencing 10X easier for you. Try out our tool and see the difference in your grades. Chicago referencing style encompasses a slew of rules and guidelines. Thus, take your time to learn the guidelines. Till then, use our generator for free.
    Our Chicago referencing generator is available 24*7 for the students. Enter the requirements as instructed on our website and get the citations done in a matter of seconds. This tool follows the latest edition of the Chicago style referencing tool. Visit http://www.allessaywriter.com right now.

    #58745

    joegracia
    Participant

    Is “Pay Someone To Do My Homework” your search to have online homework help? Hire our 24/7 live tutors and let them do your paid homework help. Click Now!

    There are many students out there who don’t get enough time to complete their homework or assignments and the reasons might be their personal issues, household works, part-time job, any accidental causes or already given academic projects or assignments. These factors make them look for online homework help and they search to pay someone to do my homework. To keep their concerns our expert writers provide the best homework or assignment writing services online. We are the best writing service provider for all academic papers. We provide them their homeworks as per their college requirements also within the deadlines.

    So, get in touch with MyAssignmenthelp.com today and grab the best writing help when you seek to pay for homework.

    #60028

    robert1833
    Participant

    Remove snags with your devices at Geek Squad Tech Support. You can approach the experts round the clock at your own convenience. Feel free to reach Geek Squad Tech Support for all issues regarding your gadgets.
    Geek squad renewal

    #60748

    robert1833
    Participant

    Thanks for the information.
    Read our post on how to fix network issue between mac and printer

    #61085

    Jacobliam
    Participant

    Domyhomework.us provides the best homework help online services to students for their academic projects.
    do my homework | pay someone to do homework

    #61101

    monalisa00
    Participant

    With <b>Geek Squad Phone Number</b>, get your queries answered and problems resolved 24×7. We offer an unrivaled level of help for your defective appliances.

    #62831

    It’s better to take care of the pest issue as soon as possible before it escalates into a serious issue. Professional Pest Control Lewisville TX services can help you to get rid of these creatures easily and safely.

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

You must be logged in to reply to this topic.