Support Forum
Support › MB Frontend Submission › Outputting a different user meta photo from field group
Hi,
I am very stuck. In my custom fields frontend form a user can select a different user to add to their submission. This selection is in a group field, as there is another question attached to it. I then show this extra user on the same single post template.
I am using elementor group as it is a value within a group. But if I just select the User field, it does not populate my meta group with their photo. So I understand I need a shortcode. But this is where I am stuck. How do I even approach a shortcode to pull a selected users photo. I tried to apply the documentation on this, but maybe I have the wrong documentation:
add_shortcode( 'co_author_loop_photo', function() {
$user = get_usermeta( 'academic_user_coauthor', get_queried_object_id() );
$co_author_photos = get_user_meta( $user, $custom_avatar, false );
foreach ( $co_author_photos as $co_author_photo ) {
$image = RWMB_Image_Field::file_info( $co_author_photo, array( 'size' => 'thumbnail' ) );
echo '<img src="' . $image['url'] . '">';
$output = $image;
}
});
Do you have any resources on how to pull a selected users information? And does it make any difference that I am using a group field?
Many thanks,
Yasmine
Hi,
1. The extension MB Frontend Submission support creating a post on the frontend. So the second line in your code is not correct to get the post meta
$user = get_usermeta( 'academic_user_coauthor', get_queried_object_id() );
You need to use the function get_post_meta() instead of get_usermeta() (it is also deprecated).
2. If the field academic_user_coauthor
is a subfield in a group, you need to get the group value first and get the subfield value with the returned array. Please read more on this documentation https://docs.metabox.io/extensions/meta-box-group/#sub-field-values
Oh ok. So in essence there are 3 stages:
1 get the group
2 get the subgroup
3. Get the user
Im a bit confused by the documentation, as it says to use get_userdata
- not for the first part, which makes complete sense to get the post's information first, but for the third part when I need to find the selected user, do I not use get_usermeta
?
add_shortcode( 'co_author_loop_photo', function() {
$user = get_post_meta( 'academic_user_coauthor', get_queried_object_id() );
$coauthor = rwmb_meta( 'academicadd_co_author' );
$coauthor = $coauthors ?? [];
foreach ( $coauthors as $coauthor ) {
$coauthor_details = $coauthor['academic_user_coauthor'] ?? '';
$co_author_photos = get_userdata( $coauthor_details, $custom_avatar, false );
foreach ( $co_author_photos as $co_author_photo ) {
$image = RWMB_Image_Field::file_info( $co_author_photo, array( 'size' => 'thumbnail' ) );
echo '<img src="' . $image['url'] . '">';
return $image;
}
});
Please ignore (or delete) my top message, I sent too soon and it doesn't make sense. Here is my real question:
It makes sense to get_post_meta first (rather than user). And as a sequence do I -
1 get the group
2 get the subgroup
3. Get the user
When I use a user
field within a group, is that returned value the user's ID? If so that makes more sense how to proceed.
And then if I want to get the users profile picture, is this section in the documentation correct? Would I get the user's avatar in the same way I would get an advanced image?
// Sub-field image_advanced.
$image_ids = $group['images'] ?? [];
if ( $image_ids ) {
$output .= '<div class="my-images">';
foreach ( $image_ids as $image_id ) {
$image = RWMB_Image_Field::file_info( $image_id, ['size' => 'my-image-size'] );
$output .= '<img src="' . $image['url'] . '" />';
}
$output .= '</div>';
}
My questions only continue as I am definitely still doing something very wrong.
Hi,
When I use a user field within a group, is that returned value the user's ID?
Yes, you will get the user ID when getting the subfield value in a group field. Please read more on this documentation https://docs.metabox.io/extensions/meta-box-group/#sub-field-values
After having the user ID, you can get the field value associated with the user (user meta) by following the documentation https://docs.metabox.io/extensions/mb-user-meta/#getting-field-value
Hope that makes sense.
Hi Long,
I am still a bit confused by this. I think it is because I am trying to get an array value of a field in a group. So three levels. I tried to pair the two tutorials. And came out with this, which does not work. Any idea what I am doing wrong?
add_shortcode( 'co_author_loop_photo', function() {
$user_group = rwmb_meta( 'academicadd_co_author' );
$user_ids = $user_group['academic_user_coauthor'] ?? : [];
foreach ( $user_ids as $user_id) {
$coauthor_photo = rwmb_meta( 'custom_avatar', [ 'object_type' => 'user' ], $user_id );
return $coauthor_photo;
}});
Hi Yasmine,
Can you please share the code that creates the custom field (group add_co_author) on your site?
Hi Long,
Thank you for the support!
I have managed to pull the name and photo to the meta box group now (had some help).
However, it now does not show the data on the single post type. It is just blank, even though it populates on the meta box group skin side. I did it using shortcodes.
//Just the name of co-author
add_shortcode("get_co_user_name", function (){
global $post;
$post_type = get_post_type($post->ID);
if(is_singular() && "research" === $post_type)
{
$field_id = rwmb_get_value( "academicadd_co_author", $post->ID, false );
foreach($field_id as $user)
{$user_name = get_userdata(intval($user['academicacademic_user_coauthor']))->display_name;
return $user_name;
}}});
//Avatar of co-author
add_shortcode("get_co_user_avatar", function (){
global $post;
$post_type = get_post_type($post->ID);
if(is_singular() && "research" === $post_type)
{
$field_id = rwmb_get_value( "academicadd_co_author", $post->ID, false );
foreach($field_id as $user)
{$avatar_url = get_avatar_url($user['academicacademic_user_coauthor']);
$main_html_markup = "<img src='$avatar_url'>";
};
return $main_html_markup;
}});
Hi Yasmine,
The order of arguments passed to the helper function rwmb_get_value()
is not correct. Please follow the format
rwmb_get_value( $field_id, $args, $object_id )
It should be
rwmb_get_value( 'academicadd_co_author', '', $post->ID )
Read more on the documentation https://docs.metabox.io/functions/rwmb-get-value/
Hi Long,
I just gave this a try - but same issue. It pulls the correct information and successfully populates the MB group skin. But then does not work when I add that group to a section on my single post template.
Many thanks,
Yasmine
Hi,
Can you please add a specific post ID to the helper function and recheck the field value?
rwmb_get_value( 'academicadd_co_author', '', 123 )
You can also use this code to print out any variable
echo '<pre>';
print_r( $field_id);
echo '</pre>';