当使用Wordpress api创建用户(http://codex.wordpress.org/Function_Reference/wp_create_user)时,有没有办法指定个人资料照片/头像?
我不希望显示标准的gravatar。我想指定一个照片网址。
以下是示例代码:
browserHistory.push('/some/path');
答案 0 :(得分:3)
Wordpress没有为用户添加自定义图像的功能。有两种显示配置文件图像的方法:
<强> 1。阿凡达:要更改avator,您应该转到wordpress管理面板的Settings > Discussion
部分。在底部向下滚动,您可以看到avator的默认选项很少。
<强> 2。 Gravatar:要在用户个人资料中使用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 );
所以,现在你应该echo
html
图片代码中的<img src="<?=$user_img_url; ?>" alt=""/>
图片网址:
.navbar-nav li{
float:left;
display: inline-block;
}
<div class="navbar-custom-menu">
<ul class="nav navbar-nav">
<!-- Messages: style can be found in dropdown.less-->
<li class="paddingtopright">
<select class="select" name="time" id="time" onload="javascript:changeDiv('Yearly');" onchange="changeDiv(this.options[this.selectedIndex].value);" style="padding-top:1px;padding-bottom:3px;">
<option value="Yearly" selected="selected">Yearly</option>
<option value="Monthly">Monthly</option>
</select>
</li>
<div id="Yearly">
<li class="paddingtopright">
<select class="select" name="itemList" id="itemList" style="padding-top:1px;padding-bottom:3px;">
<option selected>All items</option>
<option value="123">123 </option>
<option value="343">343</option>
<option value="127">127</option>
</select>
</li>
<li class="paddingtopright">
<select class="select" name="logList" id="logList" style="padding-top:1px;padding-bottom:3px;">
<option selected>All Logs</option>
<option value="ErrorLog">ErrorLog</option>
<option value="Warnings">Warnings</option>
</select>
</li>
</div>
<div id="Monthly">
<li class="paddingtoright">
<input list="month" name="month" id="month" placeholder="Enter Month here">
<datalist id="month">
<option id="Jan" value="Jan">
<option id="May" value="May">
<option id="Aug" value="Aug">
</datalist>
</li>
</div>
<li class="paddingtopright">
<button id="mybutton">OK</button>
</li>
</ul>
</div>
<script>
function changeDiv(divId)
{
if(divId=='Yearly')
{
document.getElementById(divId).style.display="block";
document.getElementById('Monthly').style.display="none";
}else if(divId=='Monthly')
{
document.getElementById(divId).style.display="block";
document.getElementById('Yearly').style.display="none";
}
}
$('#time').change();
$(':checked').trigger('click');
</script>
希望它会对你有帮助。
答案 1 :(得分:0)
前两个答案对我不起作用。取而代之的是,我只获得了成功:
>0
这意味着您需要先将图像上传到媒体库-然后在上述代码段中将该图像的ID用作std::any_of
值。
此外,不确定这是否与主题相关,但是只有在向用户数据中添加$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'.
(例如,something @ example.org)后,我才能显示图片。
还请注意上面代码段中的第二个注释,因为它非常重要! :-)
答案 2 :(得分:-1)
您可以使用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.');
}