Advertisement
SergeyBiryukov

Sergey Biryukov

Jan 27th, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2. function my_show_extra_profile_fields( $user ) { ?>
  3.  
  4.     <h3 style="margin-bottom:20px;">ваш аватар</h3>
  5.     <div id="avatar">
  6.         <img src="<?php if(esc_attr( get_the_author_meta( 'foto', $user->ID ) )!='')
  7.         echo get_bloginfo('home').'/wp-content/uploads/avatars/'.esc_attr(get_the_author_meta('foto', $user->ID ));
  8.         else
  9.         echo get_bloginfo('home').'/wp-content/themes/j2/images/avatar.jpg'; ?>" width="100" alt="Аватар" style="float:left;margin-right:15px;">
  10.         <input type="file" value="" name="foto" id="foto" /><br/>
  11.         <span>Новый аватар должен иметь<br> размер 100x100px и быть не <br> больше 50kb, иначе он не <br>загрузится.</span>
  12.         <br clear="all" />
  13.         <input type="submit" class="avatar-save" value="Обновить аватар" name="submit" />
  14.     </div>
  15.  
  16. <?php }
  17.  
  18. add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
  19. add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
  20.  
  21. function my_add_form_enctype() {
  22.     echo ' enctype="multipart/form-data"';
  23. }
  24. add_action('user_edit_form_tag', 'my_add_form_enctype');
  25.  
  26. add_action('show_user_profile', 'my_show_extra_profile_fields');
  27. add_action('edit_user_profile', 'my_show_extra_profile_fields');
  28.  
  29. function my_save_extra_profile_fields( $user_id ) {
  30.     global $my_error_messages;
  31.  
  32.     if ( !current_user_can( 'edit_user', $user_id ) )
  33.         return false;
  34.  
  35.     $image = $_FILES['foto']['tmp_name'];
  36.     if ( empty($image) )
  37.         return false;
  38.  
  39.     $image_info = GetImageSize($image);
  40.     if ( ($_FILES["foto"]["size"] <= 50*1024) and ($image_info[0]<101) and ($image_info[1]<101) )
  41.     {
  42.         move_uploaded_file($image, ABSPATH . get_option('upload_path').'/avatars/'.md5($image));
  43.         update_usermeta($user_id, 'foto', md5($image) );
  44.     } else {
  45.         $my_error_messages = array();
  46.         if ( ($image_info[0]>100) or ($image_info[1]>10) )
  47.             $my_error_messages[] = '<strong>ОШИБКА</strong>: Размер аватара превышает 100x100px!';
  48.         if ( $_FILES["foto"]["size"] > 50*1024 )
  49.             $my_error_messages[] = '<strong>ОШИБКА</strong>: Размер аватара превышает 50kb!';
  50.     }
  51. }
  52.  
  53. function my_update_errors($errors) {
  54.     global $my_error_messages;
  55.  
  56.     foreach ( (array) $my_error_messages as $error_message )
  57.         $errors->add( 'avatar', $error_message);
  58.  
  59.     return $errors;
  60. }
  61. add_action('user_profile_update_errors', 'my_update_errors');
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement