dongnuoyi8833 2016-03-09 09:04
浏览 121
已采纳

如何通过json api创建Wordpress用户时指定配置文件照片网址

When using the Wordpress api to create a user (http://codex.wordpress.org/Function_Reference/wp_create_user) is there a way to specify a profile photo / avatar?

I don't want the standard gravatar to be displayed. I want to specify a photo url.

Here is the example code:

$user_id = username_exists( $user_name );
if ( !$user_id and email_exists($user_email) == false ) {
    $random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
    $user_id = wp_create_user( $user_name, $random_password, $user_email );
} else {
    $random_password = __('User already exists.  Password inherited.');
}
  • 写回答

3条回答 默认 最新

  • douchen9569 2016-03-14 13:14
    关注

    Wordpress has no feature to add custom image for the user. There are two way to display profile image:

    1. Avatar: to change the avator, you should go Settings > Discussion section of wordpress admin panel. Scroll down at bottom, you can see there are few default options of avator.

    2. Gravatar: to use Gravator image in user profile, you should sign up at https://en.gravatar.com. Wordpress automatically grabs the image if sign up email address matches with 'Gravator' email.

    Solution for you:

    When you create user, you can add the image's url as meta field to the user. See the following example:

    $user_name = 'userid';
    $user_email = 'user_email@domain.com';
    $user_id = username_exists( $user_name ); // check if user exist
    
    if ( !$user_id and email_exists($user_email) == false ) { // if user does not exist
    
       $random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );  // generate random password
    
       $user_id = wp_create_user( $user_name, $random_password, $user_email );  // creating new user
    
       $img_url = 'http://orig15.deviantart.net/9faa/f/2011/006/e/9/there__s_something_about_molly_by_avator-d36l074.jpg'; // this is a sample image url but you should add the image url from json api 
    
       add_user_meta( $user_id, '_user_img_url', $img_url); // save the image url as meta field where meta key is _user_img_url
    
      } else {
    
          $random_password = __('User already exists.  Password inherited.');
    
      }
    

    Now the question is, how you will retrieve image url:

    // assume that the user id is 6
    $user_id = 6; 
    // met key what we used to create user
    $key = '_user_img_url';
    // If true return value of meta data field
    $single = true;
    // get the image url
    $user_img_url = get_user_meta( $user_id, $key, $single );
    

    So, now you should echo the image url in html image tag:

    <img src="<?=$user_img_url; ?>" alt=""/>
    

    Hopefully it will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型