Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created March 5, 2018 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/9b5c7b5135b18c9c943486d16fe98933 to your computer and use it in GitHub Desktop.
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
<?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
}
@tripflex
Copy link
Author

tripflex commented Mar 5, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment