首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何仅当头像存在时才显示它?

如何仅当头像存在时才显示它?
EN

Stack Overflow用户
提问于 2015-12-01 04:09:02
回答 3查看 1.8K关注 0票数 1

Wordpress中有没有什么功能可以在id不存在的情况下隐藏重力头像?

我有很多作者没有gravatar,所有的作者都是这样显示的:

代码语言:javascript
复制
http://prntscr.com/98zsji    

我使用了函数validate_gravatar($email);,但它生成了一个错误:

代码语言:javascript
复制
Fatal error: Call to undefined function validate_gravatar()

如果我打印$user_email,它会正确显示用户的电子邮件。

EN

回答 3

Stack Overflow用户

发布于 2015-12-01 04:31:22

您可以使用validate_gravatar,它接受用户的电子邮件地址并返回true或false。

代码语言:javascript
复制
validate_gravatar($email); // returns true or false

如何在你的代码中使用它:

代码语言:javascript
复制
    $user_email = get_the_author_meta('user_email');        

    if(validate_gravatar($user_email)) { 
        $author_avatar = get_avatar( get_the_author_meta( 'user_email', $author_id ), '78', '', get_the_author_meta( 'display_name', $author_id ) );
    }

    // Now just echo where ever you want the image, it will show a default image if no gravatar is present.
    if(isset($author_avatar) && !empty($author_avatar)){
        echo '<img src="'.$author_avatar.'" />';
    }

   // In your Functions.php
   function validate_gravatar($email) {
    $hash = md5(strtolower(trim($email)));
    $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
    $headers = @get_headers($uri);
    if (!preg_match("|200|", $headers[0])) {
        $has_valid_avatar = FALSE;
    } else {
        $has_valid_avatar = TRUE;
    }
    return $has_valid_avatar;
   }
票数 1
EN

Stack Overflow用户

发布于 2016-03-04 18:23:12

下面是实现这一功能的过滤器:

代码语言:javascript
复制
function ns_filter_avatar($avatar, $id_or_email, $size, $default, $alt, $args) 
{
    $headers = @get_headers( $args['url'] );
    if( ! preg_match("|200|", $headers[0] ) ) {
        return;
    }
    return $avatar; 
}
add_filter('get_avatar','ns_filter_avatar', 10, 6);

要工作,您必须将值404作为第三个参数$default添加到get_avatar()。示例:

代码语言:javascript
复制
get_avatar( $user_email, 45, '404' )
票数 1
EN

Stack Overflow用户

发布于 2019-11-04 20:56:33

我使用下面的代码已经有一段时间了,并且总是为我自己工作。代码使用PHPCS和WordPress主题标准进行了检查,没有错误或警告。

脚本profile-image.js仅在相应的页面user-edit.php中排队,这也适用于媒体上传和Thickbox脚本,此过程将避免在您的WordPress管理区发生任何可能的脚本冲突。

代码语言:javascript
复制
/**
*
* Add custom user profile information
*
*/
add_action( 'show_user_profile', 'ns_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'ns_show_extra_profile_fields' );
function ns_show_extra_profile_fields( $user ) { ?>
  <h3>Extra profile information</h3>
   <table class="form-table">
    <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 />
            <span class="description">Please upload your image for your profile.</span>
        </td>
    </tr>
   </table>
  <?php
 }

 /**
 * Enqueue a script in the WordPress admin user-edit.php.
 *
 * @param int $pagenow Hook suffix for the current admin page.
 */
 function ns_selectively_enqueue_admin_script( $hook ) {
   global $pagenow;
   if ($pagenow != 'user-edit.php') {
     return;
   }
   wp_enqueue_script('media-upload');
   wp_enqueue_script('thickbox');
   wp_enqueue_style('thickbox');
   wp_register_script( 'profile-image', get_template_directory_uri().'/js/profile-image.js', array('jquery-core'), false, true );
   wp_enqueue_script( 'profile-image' );
  }
  add_action( 'admin_enqueue_scripts', 'ns_selectively_enqueue_admin_script' );

  /*
  * Save custom user profile data
  *
  */
  add_action( 'personal_options_update', 'ns_save_extra_profile_fields' );
  add_action( 'edit_user_profile_update', 'ns_save_extra_profile_fields' );
  function ns_save_extra_profile_fields( $user_id ) {

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

    if(isset($_POST['image'])) {
     $imageprofile = sanitize_text_field( wp_unslash( $_POST['image'] ) );
     update_user_meta( $user_id, 'image', $imageprofile );
    }
  }

profile-image.js:

代码语言: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;
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34007075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档