add author avatar

This commit is contained in:
2025-07-28 21:53:09 +03:00
parent 3207928a5c
commit 329fff0696
6 changed files with 111 additions and 6 deletions

View File

@@ -1,15 +1,29 @@
<?php get_header(); ?>
<div class="content-middle articles-wrapper">
<?php get_template_part('partials/rubrics-mobile'); ?>
<div class="section-title desktop">
<div class="section-title-author">
<?php
// Получаем ID текущего автора
$author_id = get_queried_object_id();
// Пробуем получить кастомный аватар (из вашего кода)
$custom_avatar = get_user_meta($author_id, 'avatar', true);
// Если аватар есть — выводим его, иначе — стандартный 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 {
echo '<div class="author-avatar">' . get_avatar($author_id, 100, '', '', array('class' => 'avatar-round')) . '</div>';
}
?>
<h1 class="section-title__title"><?= get_the_author(); ?></h1>
</div>
<?php if (!is_paged()) {
$author_description = get_the_author_meta('description');
if (!empty($author_description)) {
echo '<div class="article-item__text">';
echo '<div class="author-desc">';
echo nl2br(esc_html($author_description));
echo '</div>';
}
@@ -22,7 +36,7 @@
'post_type' => 'post',
'posts_per_page' => 11,
'paged' => $paged,
'author' => get_queried_object_id()
'author' => $author_id
);
$query = new WP_Query($args);
?>
@@ -31,4 +45,4 @@
<?php custom_pagination(); ?>
</div>
<?php get_footer(); ?>
<?php get_footer(); ?>

View File

@@ -23,8 +23,14 @@ COPY vite.config.js vite.config.js
COPY ./certs /certs
# Открываем порт, который использует dev-сервер (например, Vite)
EXPOSE 5173
# Запускаем dev-сервер (например, `npm run dev` для Vite)
CMD ["npm", "run", "dev"]
# Определяем команду, которая будет выполняться при каждом запуске контейнера
#CMD npm run build && chown -R 1000:1000 ./dist
#CMD ["npm", "run", "build"]
ENTRYPOINT ["npm", "run"]
#ENTRYPOINT ["npm", "run"]

View File

@@ -21,6 +21,7 @@ import './styles/components/single-partner-item.css';
import './styles/components/suggestion-item.css';
import './styles/components/agro-informer.css';
import './styles/components/cookie-consent.css';
import './styles/components/author.css';
// rfo

View File

@@ -0,0 +1,19 @@
.section-title-author{
padding: 2.5rem 0 0 2.5rem;
display: flex;
align-items: center; /* центрирование по вертикали */
gap: 18px;
}
.author-avatar{
width: 10rem;
height: 10rem;
border-radius: 50%;
overflow: hidden;
position: relative;
}
.author-desc{
margin-top: 0.5rem;
padding: 1.5rem 1.25rem;
}

View File

@@ -4,6 +4,7 @@ setlocale(LC_TIME, 'ru_RU.UTF-8');
require get_template_directory() . '/inc/rubrics-menu.php'; // Рубрики в меню
require get_template_directory() . '/inc/special.php'; // Подключаем спецпроекты
require get_template_directory() . '/inc/spravochniki.php'; // Подключаем справочники
require get_template_directory() . '/inc/avatar.php'; // Подключаем фото авторов
// Полностью отключить XML-RPC
add_filter( 'xmlrpc_enabled', '__return_false' );

64
inc/avatar.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
// 1. Добавляем enctype="multipart/form-data" к форме профиля
add_action('user_edit_form_tag', 'add_multipart_form_encoding');
function add_multipart_form_encoding() {
echo ' enctype="multipart/form-data"';
}
// 2. Добавляем поле для загрузки аватара
add_action('show_user_profile', 'extra_user_profile_fields');
add_action('edit_user_profile', 'extra_user_profile_fields');
function extra_user_profile_fields($user) {
?>
<h3><?php _e("Custom Avatar", "your-textdomain"); ?></h3>
<table class="form-table">
<tr>
<th><label for="avatar"><?php _e("Avatar"); ?></label></th>
<td>
<input type="file" name="avatar" id="avatar" />
<?php
$avatar = get_user_meta($user->ID, 'avatar', true);
if ($avatar) {
echo '<img src="' . esc_url($avatar) . '" width="100" style="display:block;margin-top:10px;" />';
echo '<p><input type="checkbox" name="remove_avatar" id="remove_avatar" /> <label for="remove_avatar">' . __('Remove avatar', 'your-textdomain') . '</label></p>';
}
?>
</td>
</tr>
</table>
<?php
}
// 3. Сохраняем аватар
add_action('personal_options_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) {
if (!current_user_can('edit_user', $user_id)) {
return false;
}
// Удаление аватара
if (!empty($_POST['remove_avatar'])) {
delete_user_meta($user_id, 'avatar');
return;
}
// Загрузка нового аватара
if (!empty($_FILES['avatar']['name'])) {
if (!function_exists('wp_handle_upload')) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
}
$uploadedfile = $_FILES['avatar'];
$upload_overrides = array('test_form' => false);
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile && !isset($movefile['error'])) {
update_user_meta($user_id, 'avatar', $movefile['url']);
} else {
// Обработка ошибки (можно вывести сообщение)
wp_die('Upload error: ' . $movefile['error']);
}
}
}