2011-10-04 71 views
1

我將盡我所能解釋我在這裏要做的事情。如何在登錄表單中顯示用戶上傳的頭像?

我爲我的wordpress創建了一個登錄表單,它允許用戶直接從主頁登錄,其中包括已登錄的配置文件的頭像。以下是我提出的代碼。

<div id="loginDiv"> 

<!-- If User Is Logged In Display This --> 

<?php if (is_user_logged_in()) { ?> 

Welcome <strong><?php echo $user_identity ?></strong>. 

<?php echo get_avatar($id_or_email,$size='100'); ?> 

<form name="loginform" id="loginform" action="<?php echo wp_logout_url(); ?>" method="post"> 

<input type="submit" name="wp-submit" id="wp-submit" value="LOG OUT" /> 
<a href="http://www.bignotch.com/wp-admin/profile.php">EDIT YOUR PROFILE</a> 

<input type="hidden" name="redirect_to" value="<?php bloginfo('url'); ?>" /> 
</form> 

<!-- If User Is NOT Logged In Display This --> 

<? } else { ?> 

<form name="loginform" id="loginform" action="<?php bloginfo('url'); ?>/wp-login.php" method="post"> 

<label for="log">Username</label> <input type="text" name="log" id="user_login" class="input" value="" size="20" tabindex="10" /></p> 
<label for="pwd">Password</label> <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></p> 

<input type="submit" name="wp-submit" id="wp-submit" value="LOG IN" /> <input type="button" value="SIGNUP" onClick="parent.location='<?php bloginfo('url'); ?>/wp-login.php?action=register'" /> 

<a href="<?php bloginfo('url'); ?>/wp-login.php?action=lostpassword">Lost your password?</a> 

<input type="hidden" name="redirect_to" value="<?php bloginfo('url'); ?>/members" /> 
<input type="hidden" name="testcookie" value="1" /> 
</form> 

<? } ?></div> 

這裏的問題是,WordPress的要求用戶去「gravatar」網站,以創建一個化身。這對我沒有好處。所以,我安裝了簡單的本地化身插件,允許用戶上傳自己的用戶照片,但我無法弄清楚如何讓我的圖像顯示在我的自定義登錄表單中。

的代碼我使用在登錄形式來顯示所述化身的部分是:

<?php echo get_avatar($id_or_email,$size='100'); ?> 

但似乎它試圖抓住的Gravatar圖像。我需要這個來抓取用戶上傳的圖片。我怎樣才能做到這一點?

編輯:

下面是簡單的本地阿凡達插件的PHP代碼:

class simple_local_avatars 
{ 
    function simple_local_avatars() { 
     add_filter('get_avatar', array($this, 'get_avatar'), 10, 5); 

     add_action('admin_init', array($this, 'admin_init')); 

     add_action('show_user_profile', array($this, 'edit_user_profile')); 
     add_action('edit_user_profile', array($this, 'edit_user_profile')); 

     add_action('personal_options_update', array($this, 'edit_user_profile_update')); 
     add_action('edit_user_profile_update', array($this, 'edit_user_profile_update')); 

     add_filter('avatar_defaults', array($this, 'avatar_defaults')); 
    } 

    function get_avatar($avatar = '', $id_or_email, $size = '96', $default = '', $alt = false) { 

     if (is_numeric($id_or_email)) 
      $user_id = (int) $id_or_email; 
     elseif (is_string($id_or_email) && ($user = get_user_by_email($id_or_email))) 
      $user_id = $user->ID; 
     elseif (is_object($id_or_email) && ! empty($id_or_email->user_id)) 
      $user_id = (int) $id_or_email->user_id; 

     if (empty($user_id)) 
      return $avatar; 

     $local_avatars = get_user_meta($user_id, 'simple_local_avatar', true); 

     if (empty($local_avatars) || empty($local_avatars['full'])) 
      return $avatar; 

     $size = (int) $size; 

     if (empty($alt)) 
      $alt = get_the_author_meta('display_name', $user_id); 

     // generate a new size 
     if (empty($local_avatars[$size])) { 
      $upload_path = wp_upload_dir(); 
      $avatar_full_path = str_replace($upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full']); 
      $image_sized = image_resize($avatar_full_path, $size, $size, true); 

      // deal with original being >= to original image (or lack of sizing ability) 
      $local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars['full'] : str_replace($upload_path['basedir'], $upload_path['baseurl'], $image_sized); 

      update_user_meta($user_id, 'simple_local_avatar', $local_avatars); 
     } elseif (substr($local_avatars[$size], 0, 4) != 'http') { 
      $local_avatars[$size] = site_url($local_avatars[$size]); 
     } 

     $author_class = is_author($user_id) ? ' current-author' : '' ; 
     $avatar = "<img alt='" . esc_attr($alt) . "' src='" . $local_avatars[$size] . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />"; 

     return apply_filters('simple_local_avatar', $avatar); 
    } 

    function admin_init() { 
     load_plugin_textdomain('simple-local-avatars', false, dirname(plugin_basename(__FILE__)) . '/localization/'); 

     register_setting('discussion', 'simple_local_avatars_caps', array($this, 'sanitize_options')); 
     add_settings_field('simple-local-avatars-caps', __('Local Avatar Permissions','simple-local-avatars'), array($this, 'avatar_settings_field'), 'discussion', 'avatars'); 
    } 

    function sanitize_options($input) { 
     $new_input['simple_local_avatars_caps'] = empty($input['simple_local_avatars_caps']) ? 0 : 1; 
     return $new_input; 
    } 

    function avatar_settings_field($args) {  
     $options = get_option('simple_local_avatars_caps'); 

     echo ' 
      <label for="simple_local_avatars_caps"> 
       <input type="checkbox" name="simple_local_avatars_caps" id="simple_local_avatars_caps" value="1" ' . @checked($options['simple_local_avatars_caps'], 1, false) . ' /> 
       ' . __('Only allow users with file upload capabilities to upload local avatars (Authors and above)','simple-local-avatars') . ' 
      </label> 
     '; 
    } 

    function edit_user_profile($profileuser) { 
    ?> 
    <h3><?php _e('Avatar','simple-local-avatars'); ?></h3> 

    <table class="form-table"> 
     <tr> 
      <th><label for="simple-local-avatar"><?php _e('Upload Avatar','simple-local-avatars'); ?></label></th> 
      <td style="width: 50px;" valign="top"> 
       <?php echo get_avatar($profileuser->ID); ?> 
      </td> 
      <td> 
      <?php 
       $options = get_option('simple_local_avatars_caps'); 

       if (empty($options['simple_local_avatars_caps']) || current_user_can('upload_files')) { 
        do_action('simple_local_avatar_notices'); 
        wp_nonce_field('simple_local_avatar_nonce', '_simple_local_avatar_nonce', false); 
      ?> 
        <input type="file" name="simple-local-avatar" id="simple-local-avatar" /><br /> 
      <?php 
        if (empty($profileuser->simple_local_avatar)) 
         echo '<span class="description">' . __('No local avatar is set. Use the upload field to add a local avatar.','simple-local-avatars') . '</span>'; 
        else 
         echo ' 
          <input type="checkbox" name="simple-local-avatar-erase" value="1" /> ' . __('Delete local avatar','simple-local-avatars') . '<br /> 
          <span class="description">' . __('Replace the local avatar by uploading a new avatar, or erase the local avatar (falling back to a gravatar) by checking the delete option.','simple-local-avatars') . '</span> 
         ';  
       } else { 
        if (empty($profileuser->simple_local_avatar)) 
         echo '<span class="description">' . __('No local avatar is set. Set up your avatar at Gravatar.com.','simple-local-avatars') . '</span>'; 
        else 
         echo '<span class="description">' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.','simple-local-avatars') . '</span>'; 
       } 
      ?> 
      </td> 
     </tr> 
    </table> 
    <script type="text/javascript">var form = document.getElementById('your-profile');form.encoding = 'multipart/form-data';form.setAttribute('enctype', 'multipart/form-data');</script> 
    <?php  
    } 

    function edit_user_profile_update($user_id) { 
     if (! isset($_POST['_simple_local_avatar_nonce']) || ! wp_verify_nonce($_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce'))   //security 
      return; 

     if (! empty($_FILES['simple-local-avatar']['name'])) { 
      $mimes = array(
       'jpg|jpeg|jpe' => 'image/jpeg', 
       'gif' => 'image/gif', 
       'png' => 'image/png', 
       'bmp' => 'image/bmp', 
       'tif|tiff' => 'image/tiff' 
      ); 

      // front end (theme my profile etc) support 
      if (! function_exists('wp_handle_upload')) 
       require_once(ABSPATH . 'wp-admin/includes/file.php'); 

      $this->avatar_delete($user_id); // delete old images if successful 

      $avatar = wp_handle_upload($_FILES['simple-local-avatar'], array('mimes' => $mimes, 'test_form' => false, 'unique_filename_callback' => array($this, 'unique_filename_callback'))); 

      if (empty($avatar['file'])) {  // handle failures 
       switch ($avatar['error']) { 
        case 'File type does not meet security guidelines. Try another.' : 
         add_action('user_profile_update_errors', create_function('$a','$a->add("avatar_error",__("Please upload a valid image file for the avatar.","simple-local-avatars"));'));    
         break; 
        default : 
         add_action('user_profile_update_errors', create_function('$a','$a->add("avatar_error","<strong>".__("There was an error uploading the avatar:","simple-local-avatars")."</strong> ' . esc_attr($avatar['error']) . '");')); 
       } 

       return; 
      } 

      update_user_meta($user_id, 'simple_local_avatar', array('full' => $avatar['url']));  // save user information (overwriting old) 
     } elseif (! empty($_POST['simple-local-avatar-erase'])) { 
      $this->avatar_delete($user_id); 
     } 
    } 

    /** 
    * remove the custom get_avatar hook for the default avatar list output on options-discussion.php 
    */ 
    function avatar_defaults($avatar_defaults) { 
     remove_action('get_avatar', array($this, 'get_avatar')); 
     return $avatar_defaults; 
    } 

    /** 
    * delete avatars based on user_id 
    */ 
    function avatar_delete($user_id) { 
     $old_avatars = get_user_meta($user_id, 'simple_local_avatar', true); 
     $upload_path = wp_upload_dir(); 

     if (is_array($old_avatars)) { 
      foreach ($old_avatars as $old_avatar) { 
       $old_avatar_path = str_replace($upload_path['baseurl'], $upload_path['basedir'], $old_avatar); 
       @unlink($old_avatar_path);  
      } 
     } 

     delete_user_meta($user_id, 'simple_local_avatar'); 
    } 

    function unique_filename_callback($dir, $name, $ext) { 
     $user = wp_get_current_user(); 
     $name = sanitize_file_name($user->display_name . '_avatar'); 

     $number = 1; 

     while (file_exists($dir . "/$name$ext")) { 
      $name = $name . '_' . $number; 
      $number++; 
     } 

     return $name . $ext; 
    } 
} 

$simple_local_avatars = new simple_local_avatars; 

/** 
* more efficient to call simple local avatar directly in theme and avoid gravatar setup 
* 
* @param int|string|object $id_or_email A user ID, email address, or comment object 
* @param int $size Size of the avatar image 
* @param string $default URL to a default image to use if no avatar is available 
* @param string $alt Alternate text to use in image tag. Defaults to blank 
* @return string <img> tag for the user's avatar 
*/ 
function get_simple_local_avatar($id_or_email, $size = '96', $default = '', $alt = false) { 
    global $simple_local_avatars; 
    $avatar = $simple_local_avatars->get_avatar('', $id_or_email, $size, $default, $alt); 

    if (empty ($avatar)) 
     $avatar = get_avatar($id_or_email, $size, $default, $alt); 

    return $avatar; 
} 

/** 
* on uninstallation, remove the custom field from the users and delete the local avatars 
*/ 

register_uninstall_hook(__FILE__, 'simple_local_avatars_uninstall'); 

function simple_local_avatars_uninstall() { 
    $simple_local_avatars = new simple_local_avatars; 
    $users = get_users_of_blog(); 

    foreach ($users as $user) 
     $simple_local_avatars->avatar_delete($user->user_id); 

    delete_option('simple_local_avatars_caps'); 
} 

回答

1

而不是使用

echo get_avatar($id_or_email,$size='100'); 

嘗試調用此

全球$當前用戶;

echo get_avatar($ current_user-> id,100);

+0

我試過這個,但它只是抓住了wordpress gravatar而不是** Simple Local Avatars *頭像圖片。我一直在研究wordpress插件中的一些變量,並將不同的代碼串放在一起。 –

+0

如果使用全局$ current_user;在你想要回顯頭像的那一行之前,用戶從後端的wp profile頁面上傳一張圖片,它會使用它。請嘗試使用相同的代碼並確保上傳新的圖像(同時檢查您是否使用任何緩存解決方案) – sbrajesh

+0

是的!這樣做!不言而喻,你是最棒的人!我花了幾個小時試圖理解腳本,足以爲此提供工作代碼。顯然,我需要多學習一點。感謝你! –

1

首先你需要掛鉤簡單的局部頭像和的bbPress的插件做補充

bbPress的主題文件夾內,這條線(其中任何你使用的主題)內設置操作()

的bbPress-的functions.php

add_action('bbp_sla_edit_user_profile', array('simple_local_avatars', 'edit_user_profile'));

在bbPress的主題文件夾的形式,用戶edit.php添加此行

<?php global $current_user; do_action('bbp_sla_edit_user_profile', $current_user); ?>

相關問題