当使用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 管理面板的设置>讨论
部分。向下滚动底部,可以看到avator的默认选项很少。
<强>2。 Gravatar:要在用户个人资料中使用Gravator
图像,您应该在https://en.gravatar.com
注册。如果注册电子邮件地址与“Gravator”电子邮件匹配,Wordpress 会自动抓取图像。
为您提供的解决方案:
当您创建用户时,您可以将图像的 url 作为元字段添加到用户。请参阅以下示例:
$user_name = 'userid';
$user_email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f7a7c6a7d506a626e66634f6b60626e6661216c6062" rel="noreferrer noopener nofollow">[email protected]</a>';
$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
图像标记中echo
图像网址:
<img src="<?=$user_img_url; ?>" alt=""/>
希望对您有帮助。
关于php - 如何通过 json api 创建 WordPress 用户时指定个人资料照片 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35887118/