Outputting user value in array
Support › MB User Meta › Outputting user value in arrayResolved
- This topic has 6 replies, 2 voices, and was last updated 2 years, 8 months ago by
Yasmine.
-
AuthorPosts
-
August 2, 2022 at 4:05 AM #37306
Yasmine
ParticipantHi Long,
I'm trying to output a user meta value in a shortcode, but its not going well. I only get a string with my value in.add_shortcode ('user_academic_title', function(){ $user = get_current_user_id(); $user_academic_title = rwmb_get_value( 'academic_title',['object_type' => 'user'],$user); return var_dump($user_academic_title); });
Can you see where I'm going wrong? I can't get my value out from the string. I have tried to read the documentation on arrays and shortcodes and I still can't make it work!
August 2, 2022 at 11:20 AM #37312Long Nguyen
ModeratorHi,
Can you please share the code that creates the user meta fields? And some screenshots of the value input/output. I will help you to check the issue.
August 2, 2022 at 1:52 PM #37314Yasmine
ParticipantThank you Long, I appreciate it. There are two fields
Academic position:
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' ); function your_prefix_function_name( $meta_boxes ) { $prefix = ''; $meta_boxes[] = [ 'title' => __( 'Profile: Academic Profile', 'your-text-domain' ), 'id' => 'academic_profile', 'type' => 'user', 'fields' => [ [ [ 'name' => __( 'Academic Position', 'your-text-domain' ), 'id' => $prefix . 'academic_position', 'type' => 'select', 'options' => [ 'Distinguished Professor' => __( 'Distinguished Professor', 'your-text-domain' ), 'Honorary Professor' => __( 'Honorary Professor', 'your-text-domain' ), 'Professor' => __( 'Professor', 'your-text-domain' ), 'Associate Professor' => __( 'Associate Professor', 'your-text-domain' ), 'Assistant Professor' => __( 'Assistant Professor', 'your-text-domain' ), 'Senior Lecturer' => __( 'Senior Lecturer', 'your-text-domain' ), 'Lecturer' => __( 'Lecturer', 'your-text-domain' ), 'Associate Lecturer' => __( 'Associate Lecturer', 'your-text-domain' ), 'Junior Lecturer' => __( 'Junior Lecturer', 'your-text-domain' ), 'Postdoctoral Researcher' => __( 'Postdoctoral Researcher', 'your-text-domain' ), 'PhD Researcher' => __( 'PhD Researcher', 'your-text-domain' ), 'Reader' => __( 'Reader', 'your-text-domain' ), 'Senior Research Fellow' => __( 'Senior Research Fellow', 'your-text-domain' ), 'Research Fellow' => __( 'Research Fellow', 'your-text-domain' ), 'Research Assistant' => __( 'Research Assistant', 'your-text-domain' ), 'Research Consultant' => __( 'Research Consultant', 'your-text-domain' ), 'Researcher' => __( 'Researcher', 'your-text-domain' ), 'Journalist' => __( 'Journalist', 'your-text-domain' ), 'Writer' => __( 'Writer', 'your-text-domain' ), ], 'admin_columns' => 'after title', ], ], ]; return $meta_boxes; }
And academic_title
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' ); function your_prefix_function_name( $meta_boxes ) { $prefix = ''; $meta_boxes[] = [ 'title' => __( 'Profile: About You', 'your-text-domain' ), 'id' => 'basic_profile', 'type' => 'user', 'fields' => [ [ 'name' => __( 'Academic Title', 'your-text-domain' ), 'id' => $prefix . 'academic_title', 'type' => 'select_advanced', 'options' => [ 'Dr' => __( 'Dr', 'your-text-domain' ), 'Professor' => __( 'Professor', 'your-text-domain' ), ], 'columns' => 2, ],
And hard to share screenshot (although still can), but here is a copy and paste:
array(1) { [0]=> string(2) "Dr" }
NICE Test
array(1) { [0]=> string(18) "Associate Lecturer" }August 3, 2022 at 6:21 AM #37322Long Nguyen
ModeratorHi,
Returning array so you need to use a loop to get the value of each element or point to a specific element to get its value.
$user_academic_title = rwmb_get_value( 'academic_title', ['object_type' => 'user'], $user); return var_dump( $user_academic_title[0] );
Read more on the documentation https://docs.metabox.io/fields/select-advanced/
August 6, 2022 at 4:11 PM #37381Yasmine
ParticipantThanks Long!
August 6, 2022 at 4:19 PM #37382Yasmine
ParticipantActually - it is still not working.
On an admin account it outputs: string(1) "P"
And for a non-admin role it outputs: string(9) "Professor"
Can't seem to just get the "professor" part!
August 7, 2022 at 4:19 PM #37393Yasmine
ParticipantGot it working using
sprintf
- but it turned out shouldn't have been displaying as an array in first place (a third party plugin was modifying it). So all sorted. Thanks again. -
AuthorPosts
- You must be logged in to reply to this topic.