Advertisement
Guest User

Untitled

a guest
Jan 14th, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | None | 0 0
  1. //1st step : the html markup of user avatar. I'd change the way the plugin sending the ajax call.  Put it at the place where you want the user avatar to show. Of cause you will need to activate the user avartar plugin first. Also you need to enqueue the thinkbox and jquery at the page where you use this scritp.
  2.  
  3. <div id="user-avatar-display" >// this remains the same.
  4. <span>Profile Picture</span>
  5. <p id="user-avatar-display-image">
  6.  
  7. <?php echo user_avatar_get_avatar( $current_user->ID, 150);?></p>
  8. // this is the ajax call for adding new avatar.
  9. <a id="user-avatar-link" class="button thickbox" href="<?php echo admin_url('admin-ajax.php'); ?>?action=user_avatar_add_photo&step=1&uid=<?php echo $current_user->ID; ?>&TB_iframe=true&width=720&height=450" title="Customize your profile picture" >Update Picture</a>
  10.  
  11. //display the mystery man if there are no photo uploaded yet.
  12. <input type="hidden" class="noimage" value="<?php echo plugins_url('/images/mystery-man.jpg', __FILE__) ;?>"/>
  13.  
  14. //here is the data that needed for deleting uploaded photo.
  15. <input type="hidden" id="userid" value="<?php echo $current_user->ID ;?>"/>
  16.         <input type="hidden" id="_nonce" value="<?php  echo wp_create_nonce('delete_user_avatar') ;?>"/>
  17.  
  18.  <?php   // Remove the User-Avatar button if there is uploaded image
  19.            
  20.        if ( user_avatar_avatar_exists($current_user->ID) ):?>
  21.        
  22.          <br> <a id="user-avatar-remove"  href="#user-avatar-display" ><?php _e('Remove','user-avatar'); ?></a>
  23.        <?php
  24.        endif;
  25.  ?>
  26. </div>
  27.  
  28.  
  29. <script type="text/javascript">
  30.     function user_avatar_refresh_image(img){
  31.      jQuery('#user-avatar-display-image').html(img);
  32.         location.reload();//reloading the page after edit/add photo to display the delete link.
  33.     }
  34.     function add_remove_avatar_link(){
  35.                    /*nothing to do here, cause we are going to use our own delete link later. This function still here because of the user avatar plugin still need this*/
  36.         }
  37.    
  38.     </script>
  39.  
  40. //2nd step : The ajax response
  41. // this function is called when the delete avatar link is click. I using own ajax function to fire user avartar core delete file function.
  42. function delete_avatar() {
  43.     $uid = $_POST['uid'];
  44.     $nonce = $_POST['nonce'];
  45.     $current_user = wp_get_current_user();
  46.        
  47.         // If user clicks the remove avatar button, in URL deleter_avatar=true
  48.         if( isset($uid) && wp_verify_nonce($nonce, 'delete_user_avatar') && $uid == $current_user->id )
  49.         {      
  50.             user_avatar_delete_files($uid);//here I using user avatar own plugin delete photo function.
  51.        
  52.         }
  53.        
  54.     echo '1';//sending back to the js script to notify that the photo is deleted.
  55.     exit;
  56. }
  57. // need to add these action for ajax
  58. add_action( 'wp_ajax_nopriv_delete_avatar', 'delete_avatar');  
  59. add_action( 'wp_ajax_delete_avatar', 'delete_avatar');
  60.  
  61.  
  62. //3rd step: The js files
  63. //You will need to enqueue this js file to the page you want to use this script.
  64. wp_register_script('profile', plugin_dir_url( __FILE__ ) .'js/profile.js',__FILE__ );
  65. wp_enqueue_script('profile');
  66. wp_localize_script( 'profile', 'Ajax', array( 'url' => admin_url( 'admin-ajax.php' ) ) );//this is wp ajax reference
  67.  
  68. //here is the js in profile.js
  69. jQuery(document).ready(function($) {   
  70.     $( "#user-avatar-remove" ).click(function() {
  71.            var uid = $('#userid').val();
  72.            var nonce = $('#_nonce').val(); 
  73.            
  74.            jQuery.ajax({
  75.                  type: 'POST',   
  76.                  url: Ajax.url,
  77.                  data: ({action : 'delete_avatar',uid:uid,nonce:nonce }),
  78.                  success: function(html) {
  79.                 if(html){
  80.                     var image = $('.noimage').val();
  81.                      $("#user-avatar-display-image")
  82.                         .fadeOut(400, function() {
  83.                             $("#user-avatar-display-image").find('img').attr('src',$(".noimage").val());
  84.                             })
  85.                     .fadeIn(400);
  86.                     $('#user-avatar-remove').remove();
  87.                     }
  88.                
  89.                
  90.                  }
  91.                  });
  92.            
  93.         });    
  94.    
  95. });
  96.  
  97. //that's it.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement