/ Wordpress / Добавить файл к профилю пользователя

Добавить файл к профилю пользователя

16.04.2020

807

Добавим поле для загрузки файла в профиле пользователя

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>

    <h3>Extra profile information</h3>

    <table class="form-table ozon-certife">

        <tr>
			<th><label for="image">Profile Image</label></th>
            <td>
                <img src="<?php echo esc_attr( get_the_author_meta( 'image', $user->ID ) ); ?>" style="height:50px;">
                <input type="text" name="image" id="image" value="<?php echo esc_attr( get_the_author_meta( 'image', $user->ID ) ); ?>" class="regular-text" /><input type='button' class="button-primary" value="Upload Image" id="uploadimage"/><br />
            </td>
        </tr>	

    </table>
<?php }


// Script for saving profile image
add_action('admin_footer','my_profile_upload_js');
function my_profile_upload_js() { 

?><script type="text/javascript">
	jQuery(document).ready(function() {
	
		jQuery(document).find("input[id^='uploadimage']").live('click', function(){
			//var num = this.id.split('-')[1];
			formfield = jQuery('#image').attr('name');
			tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
 
			window.send_to_editor = function(html) {
				imgurl = jQuery('img',html).attr('src');
				jQuery('#image').val(imgurl);
				
				tb_remove();
			}
 
			return false;
		});
	});
</script><?php }

function profile_upload_file_admin_enqueue_script() {  
	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');
	wp_enqueue_style('thickbox');
}  
add_action( 'admin_footer', 'profile_upload_file_admin_enqueue_script', 1 );


// Save custom user profile data

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    if (!empty( $_POST['image'] )) { update_user_meta( $user_id, 'image', $_POST['image'] ); } else { delete_user_meta( $user_id, 'image', $_POST['image'] ); }
}

Вывести изображение

$author_id = $current_user->ID; //либо подставить конкретный ID
echo get_user_meta($author_id, 'image', 1);

Поделиться в соц. сетях:

  • Комментарии
  • Вложения

Добавить комментарий

Пока нет комментариев. Будь первым!

Добавить файл к профилю пользователя
Навигация
Рекомендации для васНавигацияOpttour.ru
Спасибо! Наш менеджер свяжется с Вами в течении 5 минут.