Drop-down user role

By: Eric Anderson | Asked: 12/10/2024
ForumsCategory: General questionsDrop-down user role
Eric Anderson asked 2 days ago

Hi

I decided to use formidable to avoid writing codes, yet still need to write a few. Anyway, I created a snippet to set the user role during registration based on the drop-down, but nothing happens, still the role selected in form settings applies.

Do I need to copy the short code from PHP to the page where my form is?

1 Answers
Rob LeVineRob LeVine answered 2 days ago
Post a link to the code you're using.
Eric Anderson replied 1 day ago

I followed this KB - https://formidableforms.com/knowledgebase/user-registration/#kb-set-the-user-role

And created the below snippet

add_filter('frmreg_new_role', 'frmreg_new_role', 10, 2);
function frmreg_new_role($role, $atts){
if ( $atts['form']->id == 46 && isset( $atts['entry'] ) ) {
$field_val = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => 604, 'entry_id' => $atts['entry']->id, 'show' => 'value' ) );
if ( $field_val == 'Author' ) {
$role = 'author';
} else if ( $field_val == 'editor' ) {
$role = 'editor';
}
}
return $role;
}

Rob LeVineRob LeVine replied 1 day ago

I was wondering why you referred to a shortcode. You can't so this via a shortcode and, no offense, I'm not sure you understand how shortcode work. To solve your issue, just use frm_after_create_entry - https://formidableforms.com/knowledgebase/frm_after_create_entry/#sts=Change%20user%20role%20after%20entry%20submission

Eric Anderson replied 1 day ago

Hello again

I create a new snippet using your code and changed the $form_id to 46 and $new_role to my drop-down id still nothing happened, I then changed $new_role to editor still nothing happened!

Is there anything else besides a form and a PHP snippet for implementing this code?

Rob LeVineRob LeVine replied 1 day ago

That code does exactly that you want it to do so you'll need to debug it. By putting in debug output, you'll be able to make sure that the function is being called, that all the values are what you think they are and that wp_update_user returns the user id you're updating.

https://www.wpbeginner.com/wp-tutorials/how-to-easily-enable-wordpress-debug-mode-to-fix-site-errors/

Eric Anderson replied 9 hours ago

Got it working! The PHP location was set to nothing. Thanks a ton for your help - it made all the difference!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right