correct author page

This commit is contained in:
argoexpert press
2025-07-28 22:50:59 +03:00
parent 329fff0696
commit 6228d8ba21
2 changed files with 69 additions and 29 deletions

View File

@@ -5,18 +5,16 @@
<div class="section-title-author"> <div class="section-title-author">
<?php <?php
// Получаем ID текущего автора // Получаем ID текущего автора
$author_id = get_queried_object_id(); //$author_id = get_queried_object_id();
$author_id = get_the_author_meta('ID');
$avatar_id = get_user_meta($author_id, 'avatar_attachment_id', true);
// Пробуем получить кастомный аватар (из вашего кода) if ($avatar_id) {
$custom_avatar = get_user_meta($author_id, 'avatar', true); echo wp_get_attachment_image($avatar_id, 'medium', false, array('class' => 'author-avatar'));
// Если аватар есть — выводим его, иначе — стандартный Gravatar
if ($custom_avatar) {
echo '<div class="author-avatar"><img src="' . esc_url($custom_avatar) . '" alt="' . esc_attr(get_the_author_meta('display_name', $author_id)) . '" width="100" height="100" class="avatar-round" /></div>';
} else { } else {
// Fallback: Gravatar или дефолтное изображение
echo '<div class="author-avatar">' . get_avatar($author_id, 100, '', '', array('class' => 'avatar-round')) . '</div>'; echo '<div class="author-avatar">' . get_avatar($author_id, 100, '', '', array('class' => 'avatar-round')) . '</div>';
} }?>
?>
<h1 class="section-title__title"><?= get_the_author(); ?></h1> <h1 class="section-title__title"><?= get_the_author(); ?></h1>
</div> </div>

View File

@@ -6,27 +6,63 @@ function add_multipart_form_encoding() {
echo ' enctype="multipart/form-data"'; echo ' enctype="multipart/form-data"';
} }
// 2. Добавляем поле для загрузки аватара // 2. Добавляем поле для аватара + медиабиблиотеку
add_action('show_user_profile', 'extra_user_profile_fields'); add_action('show_user_profile', 'extra_user_profile_fields');
add_action('edit_user_profile', 'extra_user_profile_fields'); add_action('edit_user_profile', 'extra_user_profile_fields');
function extra_user_profile_fields($user) { function extra_user_profile_fields($user) {
wp_enqueue_media(); // Подключаем медиабиблиотеку
?> ?>
<h3><?php _e("Custom Avatar", "your-textdomain"); ?></h3> <h3><?php _e("Custom Avatar", "your-textdomain"); ?></h3>
<table class="form-table"> <table class="form-table">
<tr> <tr>
<th><label for="avatar"><?php _e("Avatar"); ?></label></th> <th><label for="avatar"><?php _e("Avatar"); ?></label></th>
<td> <td>
<!-- Поле для загрузки файла -->
<input type="file" name="avatar" id="avatar" /> <input type="file" name="avatar" id="avatar" />
<!-- Кнопка выбора из медиатеки -->
<button type="button" class="button" id="select-avatar"><?php _e('Select from Media Library', 'your-textdomain'); ?></button>
<input type="hidden" name="avatar_attachment_id" id="avatar_attachment_id" value="<?php echo esc_attr(get_user_meta($user->ID, 'avatar_attachment_id', true)); ?>" />
<?php <?php
$avatar = get_user_meta($user->ID, 'avatar', true); // Показываем текущий аватар
if ($avatar) { $attachment_id = get_user_meta($user->ID, 'avatar_attachment_id', true);
echo '<img src="' . esc_url($avatar) . '" width="100" style="display:block;margin-top:10px;" />'; if ($attachment_id) {
echo wp_get_attachment_image($attachment_id, 'thumbnail', false, array(
'style' => 'display:block;margin-top:10px;',
'class' => 'avatar-preview'
));
echo '<p><input type="checkbox" name="remove_avatar" id="remove_avatar" /> <label for="remove_avatar">' . __('Remove avatar', 'your-textdomain') . '</label></p>'; echo '<p><input type="checkbox" name="remove_avatar" id="remove_avatar" /> <label for="remove_avatar">' . __('Remove avatar', 'your-textdomain') . '</label></p>';
} }
?> ?>
</td> </td>
</tr> </tr>
</table> </table>
<script>
jQuery(document).ready(function($) {
// Открываем медиабиблиотеку при клике
$('#select-avatar').click(function(e) {
e.preventDefault();
var frame = wp.media({
title: '<?php _e("Select Avatar", "your-textdomain"); ?>',
button: { text: '<?php _e("Use this image", "your-textdomain"); ?>' },
multiple: false
});
frame.on('select', function() {
var attachment = frame.state().get('selection').first().toJSON();
$('#avatar_attachment_id').val(attachment.id);
$('.avatar-preview').remove();
$(this).parent().append(
'<img src="' + attachment.sizes.thumbnail.url + '" style="display:block;margin-top:10px;" class="avatar-preview" />'
);
});
frame.open();
});
});
</script>
<?php <?php
} }
@@ -34,31 +70,37 @@ function extra_user_profile_fields($user) {
add_action('personal_options_update', 'save_extra_user_profile_fields'); add_action('personal_options_update', 'save_extra_user_profile_fields');
add_action('edit_user_profile_update', 'save_extra_user_profile_fields'); add_action('edit_user_profile_update', 'save_extra_user_profile_fields');
function save_extra_user_profile_fields($user_id) { function save_extra_user_profile_fields($user_id) {
if (!current_user_can('edit_user', $user_id)) { if (!current_user_can('edit_user', $user_id)) return false;
return false;
}
// Удаление аватара // Удаление аватара
if (!empty($_POST['remove_avatar'])) { if (!empty($_POST['remove_avatar'])) {
delete_user_meta($user_id, 'avatar'); delete_user_meta($user_id, 'avatar_attachment_id');
return; return;
} }
// Загрузка нового аватара // Загрузка нового файла
if (!empty($_FILES['avatar']['name'])) { if (!empty($_FILES['avatar']['name'])) {
if (!function_exists('wp_handle_upload')) { require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
}
$uploadedfile = $_FILES['avatar']; $uploadedfile = $_FILES['avatar'];
$upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($uploadedfile, array('test_form' => false));
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile && !isset($movefile['error'])) { if ($movefile && !isset($movefile['error'])) {
update_user_meta($user_id, 'avatar', $movefile['url']); $attachment_id = wp_insert_attachment(array(
} else { 'post_mime_type' => $movefile['type'],
// Обработка ошибки (можно вывести сообщение) 'post_title' => preg_replace('/\.[^.]+$/', '', basename($movefile['file'])),
wp_die('Upload error: ' . $movefile['error']); 'post_status' => 'inherit'
), $movefile['file']);
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $movefile['file']));
update_user_meta($user_id, 'avatar_attachment_id', $attachment_id);
}
} }
} }
// Сохранение выбранного изображения из медиатеки
if (!empty($_POST['avatar_attachment_id'])) {
update_user_meta($user_id, 'avatar_attachment_id', intval($_POST['avatar_attachment_id']));
}
} }