Support Forum
Hello,
I am using a custom field for the profile picture. I have used the following to successfully autofill the WP avatar with my custom field.
add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 );
function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
$user = false;
if ( is_numeric( $id_or_email ) ) {
$id = (int) $id_or_email;
$user = get_user_by( 'id' , $id );
} elseif ( is_object( $id_or_email ) ) {
if ( ! empty( $id_or_email->user_id ) ) {
$id = (int) $id_or_email->user_id;
$user = get_user_by( 'id' , $id );
}
} else {
$user = get_user_by( 'email', $id_or_email );
}
if ( $user && is_object( $user ) ) {
$value = rwmb_meta( 'profile_picture', array( 'object_type' => 'user' ), $user->data->ID );
if ( $value ) {
$avatar = "<img alt='" . $alt . "' height='" . $size . "' width='" . $size . "' />";
}
}
return $avatar;
}
My avatar changes in WP admin - but does not populate my dynamic field in elementor. I believe the above code only replaces the image HTML on the user profile page rather than saving in the database (this is what elelemntor told me). Could you help me figure out how to make this work properly? What is missing to make it work?
I think I have the same issue for first name, last name, biography and email too..
Hi,
You can refer to this post https://support.metabox.io/topic/metabox-user-avatar-not-working-with-elementor-author-widget/#post-24660
to show the avatar in Elementor builder. Just change the field id mbua_avatar
to your field ID profile_picture
.
Perfect - it works.
However I now see this error message "
Warning: Illegal string offset 'full_url' in ...functions.php on line 117
--> should I not be saving in my child theme php but somewhere else?
I thought I had worked out the error message, and thought it was because I had not used the prefix (making my id 'academicprofile_picture') - while that got rid of the error message, it also stopped it from working!
Would you mind deleting my error code once you've seen it?
Thank you!
Ok, your site info was deleted. Let me know if you have any questions.
Ok. But this is a problem as the error message does not disappear. Do you have any modifications to the code where the site info is not deleted? Or do you know another reason why this might have happened when running the above code??
Hi,
Can you please share the code that creates the custom fields and the code that filter the avatar URL? I will help you to check the issue.
Thank you Long. Custom fields code:
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Profile', 'your-text-domain' ),
'id' => 'academic_profile',
'tab_style' => 'box',
'type' => 'user',
'tabs' => [
'basic_info_tab' => [
'label' => 'Basic Info',
'icon' => 'dashicons-admin-users',
],
'academic_profile' => [
'label' => 'Academic Profile',
'icon' => 'dashicons-welcome-learn-more',
],
'social_sharing' => [
'label' => 'Social Sharing',
'icon' => 'dashicons-share1',
],
],
'fields' => [
[
'name' => __( 'Academic Title', 'your-text-domain' ),
'id' => $prefix . 'title',
'type' => 'select_advanced',
'options' => [
'Dr' => __( 'Dr', 'your-text-domain' ),
'Professor' => __( 'Professor', 'your-text-domain' ),
],
'columns' => 3,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'First Name', 'your-text-domain' ),
'id' => $prefix . 'first_name',
'type' => 'text',
'columns' => 4,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Last Name', 'your-text-domain' ),
'id' => $prefix . 'last_name',
'type' => 'text',
'columns' => 5,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Pronouns', 'your-text-domain' ),
'id' => $prefix . 'user_pronouns',
'type' => 'text',
'datalist' => [
'id' => '6218edf305a2b',
'options' => [
'He/His
',
'She/Hers
',
'They/Them',
],
],
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Nationality', 'your-text-domain' ),
'id' => $prefix . 'user_nationality',
'type' => 'text',
'datalist' => [
'id' => '6218edf305a41',
'options' => [
',//just took these options out as very long list
],
],
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Biography', 'your-text-domain' ),
'id' => $prefix . 'description',
'type' => 'text',
'desc' => __( 'One sentence - keep it short and snappy', 'your-text-domain' ),
'limit' => 280,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'Profile Picture', 'your-text-domain' ),
'id' => $prefix . 'profile_picture',
'type' => 'single_image',
'force_delete' => true,
'tab' => 'basic_info_tab',
],
[
'name' => __( 'University', 'your-text-domain' ),
'id' => $prefix . 'university',
'type' => 'post',
'post_type' => ['university'],
'field_type' => 'select_advanced',
'tab' => 'academic_profile',
],
[
'name' => __( 'Faculty', 'your-text-domain' ),
'id' => $prefix . 'faculty',
'type' => 'select_advanced',
'options' => [
//removed these
],
'columns' => 6,
'tab' => 'academic_profile',
],
[
'name' => __( 'Academic Position', 'your-text-domain' ),
'id' => $prefix . 'academic_position',
'type' => 'select_advanced',
'options' => [
//removed options
],
'columns' => 6,
'tab' => 'academic_profile',
],
[
'name' => __( 'University Profile Page', 'your-text-domain' ),
'id' => $prefix . 'university_profile',
'type' => 'url',
'tab' => 'social_sharing',
],
[
'name' => __( 'Linkedin Link', 'your-text-domain' ),
'id' => $prefix . 'linkedin',
'type' => 'url',
'tab' => 'social_sharing',
],
[
'name' => __( 'Twitter Handle', 'your-text-domain' ),
'id' => $prefix . 'twitter',
'type' => 'text',
'prepend' => '@',
'tab' => 'social_sharing',
],
[
'name' => __( 'Orchid Profile', 'your-text-domain' ),
'id' => $prefix . 'orchid',
'type' => 'url',
'tab' => 'social_sharing',
],
[
'name' => __( 'Email', 'your-text-domain' ),
'id' => $prefix . 'user_email',
'type' => 'email',
'tab' => 'social_sharing',
],
[
'name' => __( 'Email Sharing', 'your-text-domain' ),
'id' => $prefix . 'email_sharing',
'type' => 'switch',
'label_description' => __( 'Allow others to contact you', 'your-text-domain' ),
'on_label' => 'On',
'off_label' => 'Off',
'std' => true,
'tab' => 'social_sharing',
],
],
];
return $meta_boxes;
}
And the code for the filter:
add_filter( 'get_avatar_url', 'mbua_get_avatar_url', 10, 3 );
function mbua_get_avatar_url( $url, $id_or_email, $args ) {
if ( is_numeric( $id_or_email ) ) {
$user_id = $id_or_email;
} elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) ) {
$user_id = $user->ID;
} elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) {
$user_id = (int) $id_or_email->user_id;
}
if ( empty( $user_id ) ) {
return $url;
}
$custom_avatar = rwmb_meta( 'profile_picture', [ 'object_type' => 'user' ], $user_id );
if ( ! $custom_avatar ) {
return $url;
}
$url = $custom_avatar['full_url'];
return $url;
Hi,
Your code works as well on my local site without an error message.
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Profile', 'your-text-domain' ),
'id' => 'academic_profile',
'tab_style' => 'box',
'type' => 'user',
'tabs' => [
'basic_info_tab' => [
'label' => 'Basic Info',
'icon' => 'dashicons-admin-users',
],
],
'fields' => [
[
'name' => __( 'Profile Picture', 'your-text-domain' ),
'id' => $prefix . 'profile_picture',
'type' => 'single_image',
'force_delete' => true,
'tab' => 'basic_info_tab',
],
],
];
return $meta_boxes;
}
add_filter( 'get_avatar_url', 'mbua_get_avatar_url', 10, 3 );
function mbua_get_avatar_url( $url, $id_or_email, $args ) {
if ( is_numeric( $id_or_email ) ) {
$user_id = $id_or_email;
} elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) ) {
$user_id = $user->ID;
} elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) {
$user_id = (int) $id_or_email->user_id;
}
if ( empty( $user_id ) ) {
return $url;
}
$custom_avatar = rwmb_meta( 'profile_picture', [ 'object_type' => 'user' ], $user_id );
if ( ! $custom_avatar ) {
return $url;
}
$url = $custom_avatar['full_url'];
return $url;
}
Hi Long - but its not working on my site. Have others had this problem when trying to create a custom field for profile for elementor templates?? I think the incompatibility is with elementor? I have the meta box - elementor plugin installed.
What do you suggest I do to fix it?
I have also noticed that if all the values are empty (there are no profile pictures saved in that custom field then the error does not occur - its only when adding values into the custom field that it shows!
Rather than duplicating the value, can't I do what I did for post_content etc and make my custom field "profile_picture" the default WP field?
I think the is the request for 'full_url' --> is there something else I can use here?
Or do you have some alternative code? I found some a long time ago on the internet that worked perfectly without this error, but I mistakenly overwrote the code, and now can't find it again online
Hi,
It's ok. I've added a condition to check if the helper function returns an array to access the key full_url
. Can you please re-check the staging site?
if( is_array( $custom_avatar ) ) {
$url = $custom_avatar['full_url'];
}