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

Php 如何在通过json api创建Wordpress用户时指定配置文件照片url,php,wordpress,Php,Wordpress,当使用Wordpress api创建用户()时,是否有方法指定个人资料照片/化身 我不想显示标准的gravatar。我想指定一个照片url 下面是示例代码: $user_id = username_exists( $user_name ); if ( !$user_id and email_exists($user_email) == false ) { $random_password = wp_generate_password( $length=12, $include_stand

当使用Wordpress api创建用户()时,是否有方法指定个人资料照片/化身

我不想显示标准的gravatar。我想指定一个照片url

下面是示例代码:

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

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

1) 将图像下载到
wp\u upload\u dir
,因为
wp\u insert\u attachent
$filename
的第二个参数是:

文件在服务器上的位置。使用绝对路径,而不是URI 文件的一部分。该文件必须位于uploads目录中

2) 使用
wp\u insert\u attachent创建附件

3) 使用
update\u user\u meta将化身附加到用户

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


Wordpress没有为用户添加自定义图像的功能。有两种方式显示配置文件图像:

1。头像:要更改头像,您应该进入wordpress管理面板的
Settings>Discussion
部分。在底部向下滚动,你们可以看到avator的默认选项很少

2。Gravator:要在用户配置文件中使用
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.');

  }
现在的问题是,如何检索图像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 );
因此,现在您应该在
html
image标记中
echo
图像url:

<img src="<?=$user_img_url; ?>" alt=""/>
“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\u image\u ID

此外,不确定这是否与主题有关,但我只在添加了
用户电子邮件后才获得要显示的图像(例如。,something@example.org)对用户数据进行修改


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

我认为您必须为用户图像创建自己的自定义设置,或者使用第三方插件。
$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'.