Support Forum
Support › MB User Meta › Outputting user value in arrayResolved
Hi 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!
Hi,
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.
Thank 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" }
Hi,
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/
Thanks Long!
Actually - 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!
Got 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.