如何在通过 json API 创建 Wordpress 用户时指定个人资料照片 URL


How to specify a profile photo url when creating a Wordpress user via the json api

使用 Wordpress api 创建用户 (http://codex.wordpress.org/Function_Reference/wp_create_user) 时,有没有办法指定个人资料照片/头像?

我不希望显示标准头像。我想指定一个照片网址。

下面是示例代码:

$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.');
}

Wordpress没有为用户添加自定义图像的功能。有两种显示个人资料图像的方法:

1.头像:要更改飞行员,您应该转到WordPress管理面板Settings > Discussion部分。向下滚动底部,您可以看到 avator 的默认选项很少。

2.头像:要在用户个人资料中使用Gravator图像,您应该在https://en.gravatar.com注册。如果注册电子邮件地址与"Gravator"电子邮件匹配,WordPress会自动抓取图像。

为您提供的解决方案:

创建用户时,您可以将图像的 url 作为元字段添加到用户。请参阅以下示例:

$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.');
  }

现在的问题是,您将如何检索图像网址:

// 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 );

因此,现在您应该html图像标记中echo图像网址:

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

希望它能帮助你。

前两个答案对我不起作用。相反,我只取得了成功:

$gallery_image_id = 1234; // Replace this with ID of your uploaded image
add_user_meta( $user_id, 'wp_user_avatar', $gallery_image_id); // This should be correct if your WordPress tables were set up with the standard 'wp_' prefix. If you set them up with, say, a 'wp_mysite_' prefix then you'll need to change 'wp_user_avatar' to 'wp_mysite_user_avatar'.

这意味着您需要先将图像上传到媒体库,然后在上面的代码片段中使用该图像的 ID 作为$gallery_image_id值。

另外,不确定这是否取决于主题,但是只有在我向用户数据添加user_email(例如,something@example.org)后,我才能显示图像。

另请注意上面的代码片段中的第二条评论,因为它非常重要! :-)

您可以使用

wp_insert_attachent手动为用户设置头像。您应该使用以下步骤:

1) 将图像下载到wp_upload_dir,因为wp_insert_attachent $filename的第二个参数是:

文件在服务器上的位置。使用绝对路径而不是 URI 的文件。文件必须位于上传目录中

2)使用wp_insert_attachent创建附件

3)使用update_user_meta将头像附加到用户

所以代码

可以是这样的(我很久以前没有为我们写任何wordpress,所以也许这段代码不能立即工作,所以可能需要一些改进):

// url for user avatar
$imageUrl = '...';
$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 );
    $upload_dir = wp_upload_dir();
    // Download file by url and save it
    // to our file system into $upload_dir
    $file = $upload_dir.DIRECTORY_SEPARATOR.$user_id.
        'avatar_'.basename($imageUrl);
    copy($imageUrl, $file);
    // Create attachment
    $wp_filetype = wp_check_filetype(basename($file), null);
    $attachment = array(
        'guid' => $upload_dir['url'] .
            DIRECTORY_SEPARATOR . basename($file),
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace('/'.[^.]+$/', '', basename($file)),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment($attachment, $file);
    $attach_data = wp_generate_attachment_metadata($attach_id, $file);
    wp_update_attachment_metadata($attach_id, $attach_data);
    // Attach avatar to user
    delete_metadata('post', null, '_wp_attachment_wp_user_avatar',
        $user_id, true);
    update_user_meta($user_id, '_wp_attachment_wp_user_avatar', $attach_id);
    update_user_meta($user_id,
        $wpdb->get_blog_prefix($blog_id) . 'user_avatar', $attach_id);
} else {
    $random_password = __('User already exists.  Password inherited.');
}