feat: 用户资料页添加本地头像上传功能
This commit is contained in:
@@ -147,6 +147,60 @@ add_action('template_include', function($template) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ── 本地头像:用户资料页上传 ─────────────────────
|
||||||
|
add_action('show_user_profile', 'kjweji_avatar_field');
|
||||||
|
add_action('edit_user_profile', 'kjweji_avatar_field');
|
||||||
|
function kjweji_avatar_field($user) {
|
||||||
|
$att_id = get_user_meta($user->ID, 'custom_avatar', true);
|
||||||
|
$preview = $att_id ? wp_get_attachment_image_url($att_id, 'medium') : '';
|
||||||
|
wp_nonce_field('kjweji_avatar_nonce', 'kjweji_avatar_nonce_field');
|
||||||
|
echo '<h3>自定义头像</h3>';
|
||||||
|
echo '<table class="form-table"><tr><th><label>上传本地头像</label></th><td>';
|
||||||
|
echo '<input type="hidden" id="custom_avatar" name="custom_avatar" value="' . esc_attr($att_id) . '" />';
|
||||||
|
echo '<div id="avatar_preview" style="margin-bottom:10px">';
|
||||||
|
if ($preview) echo '<img src="' . esc_url($preview) . '" style="max-width:120px;border-radius:8px;" />';
|
||||||
|
echo '</div>';
|
||||||
|
echo '<button type="button" class="button" id="upload_avatar_btn">选择图片</button>';
|
||||||
|
echo '<button type="button" class="button" id="remove_avatar_btn" style="margin-left:5px;' . ($att_id ? '' : 'display:none;') . '">移除</button>';
|
||||||
|
echo '</td></tr></table>';
|
||||||
|
echo '<script>
|
||||||
|
jQuery(function($) {
|
||||||
|
var frame;
|
||||||
|
$("#upload_avatar_btn").click(function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (frame) { frame.open(); return; }
|
||||||
|
frame = wp.media({ title: "选择头像", button: { text: "设为头像" }, multiple: false });
|
||||||
|
frame.on("select", function() {
|
||||||
|
var att = frame.state().get("selection").first().toJSON();
|
||||||
|
$("#custom_avatar").val(att.id);
|
||||||
|
$("#avatar_preview").html("<img src=\'" + att.url + "\' style=\'max-width:120px;border-radius:8px;\' />");
|
||||||
|
$("#remove_avatar_btn").show();
|
||||||
|
});
|
||||||
|
frame.open();
|
||||||
|
});
|
||||||
|
$("#remove_avatar_btn").click(function() {
|
||||||
|
$("#custom_avatar").val("");
|
||||||
|
$("#avatar_preview").html("");
|
||||||
|
$(this).hide();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>';
|
||||||
|
}
|
||||||
|
add_action('personal_options_update', 'kjweji_save_avatar');
|
||||||
|
add_action('edit_user_profile_update', 'kjweji_save_avatar');
|
||||||
|
function kjweji_save_avatar($user_id) {
|
||||||
|
if (!current_user_can('edit_user', $user_id)) return;
|
||||||
|
if (!isset($_POST['kjweji_avatar_nonce_field']) || !wp_verify_nonce($_POST['kjweji_avatar_nonce_field'], 'kjweji_avatar_nonce')) return;
|
||||||
|
if (isset($_POST['custom_avatar'])) {
|
||||||
|
$att_id = intval($_POST['custom_avatar']);
|
||||||
|
if ($att_id) {
|
||||||
|
update_user_meta($user_id, 'custom_avatar', $att_id);
|
||||||
|
} else {
|
||||||
|
delete_user_meta($user_id, 'custom_avatar');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 本地头像(团队证件照)
|
// 本地头像(团队证件照)
|
||||||
add_filter('get_avatar', function($avatar, $id_or_email, $size, $default, $alt) {
|
add_filter('get_avatar', function($avatar, $id_or_email, $size, $default, $alt) {
|
||||||
$uid = 0;
|
$uid = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user