Created
March 5, 2018 22:32
-
-
Save tripflex/9b5c7b5135b18c9c943486d16fe98933 to your computer and use it in GitHub Desktop.
Populate current user role in field value with WP Job Manager Field Editor
This file contains 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 | |
// This assumes the META KEY for the field is "current_user_role" | |
add_filter( 'field_editor_auto_populate_current_user_role', 'smyles_auto_populate_set_cur_user_role' ); | |
function smyles_auto_populate_set_cur_user_role( $populate_value ){ | |
if ( is_user_logged_in() ) { | |
$user = wp_get_current_user(); | |
$role = ( array ) $user->roles; | |
return esc_attr($role[0]); // Return first role assigned to user account | |
} | |
return 'anonymous'; // Return 'anonymous' for users not logged in | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For this to work, you must create a custom field named "current_user_role" (or update the code to correct meta key), and you MUST enable Auto Populate feature (you don't need to configure the other settings, and it's recommend to NOT enable auto save)