PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 26, 2022

[FIXED] How to get image url from wordpress meta_key field?

 August 26, 2022     html, metadata, php, variables, wordpress     No comments   

Issue

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" >

Solution

Use wp_get_attachment_image_url:

function wdoc_filter_get_avatar_url( $url, $id_or_email, $args ) {
    $current_user_id = 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 );


Answered By - amiad
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing