• I have created two separate roles for my site, buyer and seller, with buyer as default role.

    I was trying to make a specific register form for a specific role (seller). Email activation was used on registration.

    I put this line on custom register-seller.php form, inside the <form> tag.
    <input type="hidden" id="role<?php $template->the_instance(); ?>" name="role" value="seller" />

    Then I add this on functions.php

    function tml_user_register( $user_id ) {
    	if ( !empty( $_POST['first_name'] ) )
    		update_user_meta( $user_id, 'first_name', $_POST['first_name'] );
    	if ( !empty( $_POST['last_name'] ) )
    		update_user_meta( $user_id, 'last_name', $_POST['last_name'] );
    	if (isset($_POST['role'])){
           $userdata = array();
           $userdata['ID'] = $user_id;
           $userdata['role'] = $_POST['role'];
    
           wp_update_user($userdata);
       }
    }
    add_action( 'user_register', 'tml_user_register' );
    
    function set_role_on_registration( $user_id ) {
        if( isset($_POST['role']) )
            {
                $role = 'seller';
            }
        if ( in_array( $role, array( 'seller', 'buyer' ) ) )
            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( 'seller', 'buyer' ) ) )
                return;
            $user = new WP_User( $user_id );
            $user->set_role( $pending_role );
            unset( $user );
        }
    }
    add_action( 'tml_new_user_activated', 'set_role_on_activation' );

    So right now, even if a user register through seller’s register form, he always ends up as buyer after activation. Any help would be appreciated!

    https://ww.wp.xz.cn/plugins/theme-my-login/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Register Form for Specific Role’ is closed to new replies.