Tagged: Member Types Pro, Xprofile Fields
Hi there,
I’ve created a xprofile single member type field using the instructions found in the plugin’s documentation section. I’ve added it to the user-new.php registration form as a dropdown option with the assumption that an admin or editor (I’ve modified the editor role for create user capabilities) can set the member type when creating a new user. The user is being created but the member type is not being set. Can someone have a look at the following code and let me know where I’m going wrong?
// add fields to new user form add_action( 'user_new_form', 'mktbn_user_new_form' ); function mktbn_user_new_form() { ?> <table class="form-table"> <tbody> <tr class="form-field"> <th scope="row"><label for="Member type">Community Member Type</label></th> <td> <select name="MemberType"> <option value="type1">Member Type 1</option> <option value="type2">Member Type 2</option> <option value="type3">Member Type 3</option> </select> </td> </tr> </tbody> </table> <?php } // process the extra fields for new user form add_action( 'user_register', 'mktbn_user_register', 10, 1 ); function mktbn_user_register( $user_id ) { mikakoo_log( 'user_register: ' . $user_id ); { update_user_meta($user_id, 'member type', $_POST['MemberType']); } }
Hi,
Xprofile fields are not supported on Dashboard->Add User screen.In this case, you can change the following line
update_user_meta($user_id, 'member type', $_POST['MemberType']);
with this
if ( ! empty( $_POST['MemberType'] ) ) { bp_set_member_type( $user_id, $_POST['MemberType'] ); }
That will make it work.
Regards
BrajeshHi Jeff,
Please share the complete code for function mktbn_user_register()Most probably there is some syntaxt error causing it.
Regards
BrajeshThank you sir!
// add fields to new user form add_action( 'user_new_form', 'mktbn_user_new_form' ); function mktbn_user_new_form() { ?> <table class="form-table"> <tbody> <tr class="form-field"> <th scope="row"><label for="Member type">Community Member Type</label></th> <td> <select name="MemberType"> <option value="type1">Member Type 1</option> <option value="type2">Member Type 2</option> <option value="type3">Member Type 3</option> </select> </td> </tr> </tbody> </table> <?php } // process the extra fields for new user form add_action( 'user_register', 'mktbn_user_register', 10, 1 ); function mktbn_user_register( $user_id ) { mikakoo_log( 'user_register: ' . $user_id ); if ( ! empty( $_POST['MemberType'] ) ) { bp_set_member_type( $user_id, $_POST['MemberType'] ); } }
HI Jeff,
Thank you. Your code is correct. If it is giving 500 error, It means one of the used function does not exist.I have slightly modified the function, Please change the function with this one and then let me know if it works or not?
function mktbn_user_register( $user_id ) { if ( function_exists( 'mikakoo_log' ) ) { mikakoo_log( 'user_register: ' . $user_id ); } if ( ! empty( $_POST['MemberType'] ) && function_exists( 'bp_set_member_type' ) ) { bp_set_member_type( $user_id, $_POST['MemberType'] ); } }
Regards
BrajeshSure, Here is the functiona updated again.
function mktbn_user_register( $user_id ) { if ( function_exists( 'mikakoo_log' ) ) { mikakoo_log( 'user_register: ' . $user_id ); } if ( ! empty( $_POST['MemberType'] ) && function_exists( 'bp_set_member_type' ) ) { bp_set_member_type( $user_id, $_POST['MemberType'] ); } $user = get_user_by( 'id', $user_id ); // if member type submitted and valid user and the person adding user is super admin // then set role too. if ( $user && ! empty( $_POST['MemberType'] ) && is_super_admin() ) { $user->set_role($_POST['MemberType'] ); } }
Regards
BrajeshHi Jeff,
Yes, that is correct.I added the check as we do not want users to misuse it. The user_register action fires even when a user registers from front end and if we do not put a check on who is creating the user, The code has a potential to be misused.
Regards
Brajesh
You must be logged in to reply to this topic.