Home > Blockchain >  How to get image url from wordpress meta_key field?
How to get image url from wordpress meta_key field?

Time:08-18

I have a filter that should change the user avatar across the site. The filter works, only I'm not getting the meta_key url. In this case image represents the meta_key where the avatar for each user is stored.

The filter

function wdoc_filter_get_avatar_url( $url, $id_or_email, $args ) {
    global $current_user;
    $url = get_user_meta( $current_user->ID, 'image' , true );
    return $url;
}

add_filter( 'get_avatar_url', 'wdoc_filter_get_avatar_url', 10, 3 );

What I am getting in html:

The number 49787 in the link would be the value of the meta_key, and it is correct. But I don't want the meta value, but its image link. How can I get it?

<img alt="" src="http://49787" srcset="http://49787 2x" >

CodePudding user response:

Use wp_get_attachment_image_url:

function wdoc_filter_get_avatar_url( $url, $id_or_email, $args ) {
    $current_user = get_current_user_id();
    $file_id = get_user_meta( $current_user->ID, 'image' , true );
    return wp_get_attachment_image_url($file_id);
}

add_filter( 'get_avatar_url', 'wdoc_filter_get_avatar_url', 10, 3 );
  • Related