Last active
September 11, 2021 09:50
-
-
Save vfontjr/cfa1f3d0da0d373e8ee022cf792f835e to your computer and use it in GitHub Desktop.
Source code for https://formidable-masterminds.com/change-wordpress-user-role-when-editing-field-in-a-view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('frm_after_update_entry', 'update_user_role', 10, 2); | |
function update_user_role($entry_id, $form_id){ | |
if ( $form_id == 4 ) { | |
$userid = $_POST['item_meta'][15];// ID of the userID field | |
$role = $_POST['item_meta'][19];// ID of the role field | |
if ( $userid && $role ) { | |
$user = get_userdata( $userid ); | |
if ( $user && ! $user->has_cap('administrator') ) { | |
$user->set_role( $role ); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('frm_after_update_field', 'frm_trigger_entry_update'); | |
function frm_trigger_entry_update($atts){ | |
$userid_field= '15'; | |
$entry = FrmEntry::getOne( $atts['entry_id'], true ); | |
$userid = FrmEntryMeta::get_meta_value( $entry, $userid_field ); | |
$role = $atts['value']; | |
if ( $userid && $role ) { | |
update_wordpress_user_role( $userid, $role ); | |
} | |
$form = FrmForm::getOne( $entry->form_id ); | |
FrmFormActionsController::trigger_actions('update', $form, $entry, 'email'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('frm_after_update_entry', 'update_user_role', 10, 2); | |
function update_user_role($entry_id, $form_id){ | |
if ( $form_id == 4 ) { | |
$userid = $_POST['item_meta'][15];// ID of the userID field | |
$role = $_POST['item_meta'][19];// ID of the role field | |
if ( $userid && $role ) { | |
update_wordpress_user_role( $userid, $role ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function update_wordpress_user_role( $userid, $role ) { | |
$user = get_userdata( $userid ); | |
if ( $user && ! $user->has_cap('administrator') ) { | |
$user->set_role( $role ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment