Files
agroexpert/inc/avatar.php

106 lines
4.7 KiB
PHP
Raw Permalink Normal View History

2025-07-28 21:53:09 +03:00
<?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"';
}
2025-07-28 22:50:59 +03:00
// 2. Добавляем поле для аватара + медиабиблиотеку
2025-07-28 21:53:09 +03:00
add_action('show_user_profile', 'extra_user_profile_fields');
add_action('edit_user_profile', 'extra_user_profile_fields');
function extra_user_profile_fields($user) {
2025-07-28 22:50:59 +03:00
wp_enqueue_media(); // Подключаем медиабиблиотеку
2025-07-28 21:53:09 +03:00
?>
<h3><?php _e("Custom Avatar", "your-textdomain"); ?></h3>
<table class="form-table">
<tr>
<th><label for="avatar"><?php _e("Avatar"); ?></label></th>
<td>
2025-07-28 22:50:59 +03:00
<!-- Поле для загрузки файла -->
2025-07-28 21:53:09 +03:00
<input type="file" name="avatar" id="avatar" />
2025-07-28 22:50:59 +03:00
<!-- Кнопка выбора из медиатеки -->
<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)); ?>" />
2025-07-28 21:53:09 +03:00
<?php
2025-07-28 22:50:59 +03:00
// Показываем текущий аватар
$attachment_id = get_user_meta($user->ID, 'avatar_attachment_id', true);
if ($attachment_id) {
echo wp_get_attachment_image($attachment_id, 'thumbnail', false, array(
'style' => 'display:block;margin-top:10px;',
'class' => 'avatar-preview'
));
2025-07-28 21:53:09 +03:00
echo '<p><input type="checkbox" name="remove_avatar" id="remove_avatar" /> <label for="remove_avatar">' . __('Remove avatar', 'your-textdomain') . '</label></p>';
}
?>
</td>
</tr>
</table>
2025-07-28 22:50:59 +03:00
<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>
2025-07-28 21:53:09 +03:00
<?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) {
2025-07-28 22:50:59 +03:00
if (!current_user_can('edit_user', $user_id)) return false;
2025-07-28 21:53:09 +03:00
// Удаление аватара
if (!empty($_POST['remove_avatar'])) {
2025-07-28 22:50:59 +03:00
delete_user_meta($user_id, 'avatar_attachment_id');
2025-07-28 21:53:09 +03:00
return;
}
2025-07-28 22:50:59 +03:00
// Загрузка нового файла
2025-07-28 21:53:09 +03:00
if (!empty($_FILES['avatar']['name'])) {
2025-07-28 22:50:59 +03:00
require_once(ABSPATH . 'wp-admin/includes/file.php');
2025-07-28 21:53:09 +03:00
$uploadedfile = $_FILES['avatar'];
2025-07-28 22:50:59 +03:00
$movefile = wp_handle_upload($uploadedfile, array('test_form' => false));
2025-07-28 21:53:09 +03:00
if ($movefile && !isset($movefile['error'])) {
2025-07-28 22:50:59 +03:00
$attachment_id = wp_insert_attachment(array(
'post_mime_type' => $movefile['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($movefile['file'])),
'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);
}
2025-07-28 21:53:09 +03:00
}
}
2025-07-28 22:50:59 +03:00
// Сохранение выбранного изображения из медиатеки
if (!empty($_POST['avatar_attachment_id'])) {
update_user_meta($user_id, 'avatar_attachment_id', intval($_POST['avatar_attachment_id']));
}
2025-07-28 21:53:09 +03:00
}