是否可以通过编程方式更改WordPress中的用户头像?我问,因为我现在在WordPress多站点中显示用户头像时遇到问题:头像没有显示。
答案 0 :(得分:1)
最有可能在某个地方调用get_avatar
过滤器并执行某些操作。我建议您搜索get_avatar
的插件和主题,并查看类似的内容:add_filter ('get_avatar', .....
否则,您可以使用以下代码编写自己的行为。
<?php // in a plugin file or in a theme functions.php
function SO13911452_override_avatar ($avatar_html, $id_or_email, $size, $default, $alt) {
// check all values
return $avatar_html
}
add_filter ('get_avatar', 'SO13911452_override_avatar', 10, 5);
答案 1 :(得分:0)
我必须做三件事才能以编程方式将用户头像插入我的WordPress,从一个托管在远程URL的头像开始。
假设您的用户的头像为public class QuestionsAdapter extends RecyclerView.Adapter<QuestionsAdapter.QuestionsViewHolder> {
List<QuestionModel> Questions;
public Context context;
public QuestionsAdapter(Context context, List<QuestionModel> questions) {
this.Questions = questions;
this.context = context;
}
@Override
public QuestionsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.questions_card_view, parent, false);
return new QuestionsViewHolder(v);
}
@Override
public void onBindViewHolder(QuestionsViewHolder holder, final int position) {
holder.txtQuestionText.setText(Questions.get(position).QuestionText);
}
@Override
public int getItemCount() {
return Questions.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public class QuestionsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public RadioButton rbLike;
public RadioButton rbdisLike;
public RadioButton rbnoComment;
public TextView txtQuestionText;
public QuestionsViewHolder(View itemView) {
super(itemView);
rbLike = (RadioButton) itemView.findViewById(R.id.like);
rbdisLike = (RadioButton) itemView.findViewById(R.id.Dislike);
txtQuestionText = (TextView) itemView.findViewById(R.id.txtCardQuestion);
rbnoComment = (RadioButton) itemView.findViewById(R.id.NoComment);
rbnoComment.setOnClickListener(this);
rbdisLike.setOnClickListener(this);
rbLike.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.like:
rbLike.setChecked(true);
rbnoComment.setChecked(false);
rbdisLike.setChecked(false);
break;
case R.id.Dislike:
rbdisLike.setChecked(true);
rbnoComment.setChecked(false);
rbLike.setChecked(false);
break;
case R.id.NoComment:
rbnoComment.setChecked(true);
rbLike.setChecked(false);
rbdisLike.setChecked(false);
//do something
break;
default:
}
}
}
我使用WooCommerce的$avatar_url = 'http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a';
中的upload_product_image()
将头像带入我的本地服务器。
然后,使用此support post中的部分代码创建附件。 然后将附件与用户关联。
这仅适用于WP用户头像插件。
class-wc-api-products.php
来自WooCommerce's class-wc-api-products.php
function se13911452_set_avatar_url($avatar_url, $user_id) {
global $wpdb;
$file = upload_product_image($avatar_url);
$wp_filetype = wp_check_filetype($file['file']);
$attachment = array(
'guid' => $file['url'],
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($file['file'])),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file['file']);
$attach_data = wp_generate_attachment_metadata($attach_id, $file['file']);
wp_update_attachment_metadata($attach_id, $attach_data);
update_user_meta($user_id, $wpdb->get_blog_prefix() . 'user_avatar', $attach_id);
}
答案 2 :(得分:0)
这将起作用:
add_filter('get_avatar_data', 'ht1_change_avatar', 100, 2);
function ht1_change_avatar($args, $id_or_email) {
if($id_or_email == 1) {
$args['url'] = 'https://uinames.com/api/photos/female/1.jpg';
}
if($id_or_email == 2) {
$args['url'] = 'https://uinames.com/api/photos/male/19.jpg';
}
return $args;
} // end of function
如果您具有用户的头像目录位置元数据,则将其用于所有用户:
add_filter('get_avatar_data', 'ht1_change_avatar', 100, 2);
function ht1_change_avatar($args, $id_or_email) {
$avatar_url = get_user_meta($id_or_email, 'avatar', true);
$args['url'] = $avatar_url;
return $args;
} // end of function
希望您明白这一点。