correct author page
This commit is contained in:
@@ -6,27 +6,63 @@ function add_multipart_form_encoding() {
|
||||
echo ' enctype="multipart/form-data"';
|
||||
}
|
||||
|
||||
// 2. Добавляем поле для загрузки аватара
|
||||
// 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) {
|
||||
wp_enqueue_media(); // Подключаем медиабиблиотеку
|
||||
?>
|
||||
<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" />
|
||||
|
||||
<!-- Кнопка выбора из медиатеки -->
|
||||
<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
|
||||
$avatar = get_user_meta($user->ID, 'avatar', true);
|
||||
if ($avatar) {
|
||||
echo '<img src="' . esc_url($avatar) . '" width="100" style="display:block;margin-top:10px;" />';
|
||||
// Показываем текущий аватар
|
||||
$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'
|
||||
));
|
||||
echo '<p><input type="checkbox" name="remove_avatar" id="remove_avatar" /> <label for="remove_avatar">' . __('Remove avatar', 'your-textdomain') . '</label></p>';
|
||||
}
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
</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
|
||||
}
|
||||
|
||||
@@ -34,31 +70,37 @@ function extra_user_profile_fields($user) {
|
||||
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 (!current_user_can('edit_user', $user_id)) return false;
|
||||
|
||||
// Удаление аватара
|
||||
if (!empty($_POST['remove_avatar'])) {
|
||||
delete_user_meta($user_id, 'avatar');
|
||||
delete_user_meta($user_id, 'avatar_attachment_id');
|
||||
return;
|
||||
}
|
||||
|
||||
// Загрузка нового аватара
|
||||
// Загрузка нового файла
|
||||
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'];
|
||||
$upload_overrides = array('test_form' => false);
|
||||
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
|
||||
$movefile = wp_handle_upload($uploadedfile, array('test_form' => false));
|
||||
|
||||
if ($movefile && !isset($movefile['error'])) {
|
||||
update_user_meta($user_id, 'avatar', $movefile['url']);
|
||||
} else {
|
||||
// Обработка ошибки (можно вывести сообщение)
|
||||
wp_die('Upload error: ' . $movefile['error']);
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Сохранение выбранного изображения из медиатеки
|
||||
if (!empty($_POST['avatar_attachment_id'])) {
|
||||
update_user_meta($user_id, 'avatar_attachment_id', intval($_POST['avatar_attachment_id']));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user