diff --git a/functions.php b/functions.php index bcb74ab..dc390fd 100644 --- a/functions.php +++ b/functions.php @@ -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 '

自定义头像

'; + echo '
'; + echo ''; + echo '
'; + if ($preview) echo ''; + echo '
'; + echo ''; + echo ''; + echo '
'; + echo ''; +} +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) { $uid = 0;