首页>建站相关>wordpress的get_avatar_data()函数位于哪个文件

wordpress的get_avatar_data()函数位于哪个文件

获取头像应用到了这个函数,这个函数位于wp-includes文件夹的link-template.php这个php文件当中;

具体代码

4.9.26版本的代码如下:

function get_avatar_data( $id_or_email, $args = null ) {
    $args = wp_parse_args( $args, array(
        'size'           => 96,
        'height'         => null,
        'width'          => null,
        'default'        => get_option( 'avatar_default', 'mystery' ),
        'force_default'  => false,
        'rating'         => get_option( 'avatar_rating' ),
        'scheme'         => null,
        'processed_args' => null, // if used, should be a reference
        'extra_attr'     => '',
    ) );

    if ( is_numeric( $args['size'] ) ) {
        $args['size'] = absint( $args['size'] );
        if ( ! $args['size'] ) {
            $args['size'] = 96;
        }
    } else {
        $args['size'] = 96;
    }

    if ( is_numeric( $args['height'] ) ) {
        $args['height'] = absint( $args['height'] );
        if ( ! $args['height'] ) {
            $args['height'] = $args['size'];
        }
    } else {
        $args['height'] = $args['size'];
    }

    if ( is_numeric( $args['width'] ) ) {
        $args['width'] = absint( $args['width'] );
        if ( ! $args['width'] ) {
            $args['width'] = $args['size'];
        }
    } else {
        $args['width'] = $args['size'];
    }

    if ( empty( $args['default'] ) ) {
        $args['default'] = get_option( 'avatar_default', 'mystery' );
    }

    switch ( $args['default'] ) {
        case 'mm' :
        case 'mystery' :
        case 'mysteryman' :
            $args['default'] = 'mm';
            break;
        case 'gravatar_default' :
            $args['default'] = false;
            break;
    }

    $args['force_default'] = (bool) $args['force_default'];

    $args['rating'] = strtolower( $args['rating'] );

    $args['found_avatar'] = false;

    /**
     * Filters whether to retrieve the avatar URL early.
     *
     * Passing a non-null value in the 'url' member of the return array will
     * effectively short circuit get_avatar_data(), passing the value through
     * the {@see 'get_avatar_data'} filter and returning early.
     *
     * @since 4.2.0
     *
     * @param array  $args        Arguments passed to get_avatar_data(), after processing.
     * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
     *                            user email, WP_User object, WP_Post object, or WP_Comment object.
     */
    $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );

    if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
        /** This filter is documented in wp-includes/link-template.php */
        return apply_filters( 'get_avatar_data', $args, $id_or_email );
    }

    $email_hash = '';
    $user = $email = false;

    if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
        $id_or_email = get_comment( $id_or_email );
    }

    // Process the user identifier.
    if ( is_numeric( $id_or_email ) ) {
        $user = get_user_by( 'id', absint( $id_or_email ) );
    } elseif ( is_string( $id_or_email ) ) {
        if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
            // md5 hash
            list( $email_hash ) = explode( '@', $id_or_email );
        } else {
            // email address
            $email = $id_or_email;
        }
    } elseif ( $id_or_email instanceof WP_User ) {
        // User Object
        $user = $id_or_email;
    } elseif ( $id_or_email instanceof WP_Post ) {
        // Post Object
        $user = get_user_by( 'id', (int) $id_or_email->post_author );
    } elseif ( $id_or_email instanceof WP_Comment ) {
        /**
         * Filters the list of allowed comment types for retrieving avatars.
         *
         * @since 3.0.0
         *
         * @param array $types An array of content types. Default only contains 'comment'.
         */
        $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
        if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
            $args['url'] = false;
            /** This filter is documented in wp-includes/link-template.php */
            return apply_filters( 'get_avatar_data', $args, $id_or_email );
        }

        if ( ! empty( $id_or_email->user_id ) ) {
            $user = get_user_by( 'id', (int) $id_or_email->user_id );
        }
        if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
            $email = $id_or_email->comment_author_email;
        }
    }

    if ( ! $email_hash ) {
        if ( $user ) {
            $email = $user->user_email;
        }

        if ( $email ) {
            $email_hash = md5( strtolower( trim( $email ) ) );
        }
    }

    if ( $email_hash ) {
        $args['found_avatar'] = true;
        $gravatar_server = hexdec( $email_hash[0] ) % 3;
    } else {
        $gravatar_server = rand( 0, 2 );
    }

    $url_args = array(
        's' => $args['size'],
        'd' => $args['default'],
        'f' => $args['force_default'] ? 'y' : false,
        'r' => $args['rating'],
    );

    if ( is_ssl() ) {
        $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
    } else {
        $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
    }

    $url = add_query_arg(
        rawurlencode_deep( array_filter( $url_args ) ),
        set_url_scheme( $url, $args['scheme'] )
    );

    /**
     * Filters the avatar URL.
     *
     * @since 4.2.0
     *
     * @param string $url         The URL of the avatar.
     * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
     *                            user email, WP_User object, WP_Post object, or WP_Comment object.
     * @param array  $args        Arguments passed to get_avatar_data(), after processing.
     */
    $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );

    /**
     * Filters the avatar data.
     *
     * @since 4.2.0
     *
     * @param array  $args        Arguments passed to get_avatar_data(), after processing.
     * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
     *                            user email, WP_User object, WP_Post object, or WP_Comment object.
     */
    return apply_filters( 'get_avatar_data', $args, $id_or_email );
}

标签: wordpress

移动端可扫我直达哦~

推荐阅读

wordpress 2025-04-07

wordpress主题目录穿越导致的bug

尝试从知乎的热榜api提取数据并保存到本地,隔几个小时刷新一次,前几天运行的好好的,这几天突然发现数据有几天没有更新了。手动刷新了一下数据获取页,一直提示无需更新,将文件下载到本地测试,功能又是完好的。开了wp的debug模式,修正了...

建站相关 wordpress

wordpress 2025-03-31

wordpress 重新生成缩略图

测试wp缩略图功能的时候不小心把所有的缩略图都删掉了,但保留了原图,不想一个个重新上传以生成缩略图,就使用了这段代码,注意生成成功之后,这段代码就可以删掉了。忽然想起之前钉钉宣传的某个功能,阅后即焚~~function regener...

建站相关 wordpress

wordpress 2025-03-29

WordPress的主循环与WP_Query

WordPress的主循环和WP_Query是主题开发中最重要的两个概念,它们负责从数据库获取内容并显示在页面上。主循环 (The Loop)主循环是WordPress用来显示文章的核心机制。它是一个PHP代码结构,用于遍历当前页面请...

建站相关 wordpress

wordpress 2025-03-22

在phpstudy中为wordpress开启伪静态

原文修改主题都是在服务器上一边在线修改一边调试,用上了phpstudy后才发现自己之前的方式有多没有效率。但测试设置固定链接的时候遇到了一个问题,就是设置前也无风雨也无晴,设置后统一返回404。这个问题之前尝试搭建站点的时候也遇到过,...

建站相关 wordpress

wordpress 2025-03-17

WordPress分页中遇到404错误:posts_per_page

这个问题可能更多为主题开发者所遇见,一款推向市场的主题一般都会几经测试,应该不容易到客户手中才发现这个问题。所以网络上的相关讨论不多,博主也是调试了几天才大致有了一些思路:后台的默认参数在wordpress的后台设置里,是可以设置归档...

建站相关 wordpress

wordpress 2025-03-14

WORDPRESS HEADER模块常用函数

在 WordPress 开发中,header.php 文件是主题的重要组成部分,用于定义网站的头部内容。以下是一些在 header.php 中常用的 WordPress 函数及其用途,如果嫌部分函数生成的默认模板不需要的元素过多,也可...

建站相关 wordpress

wordpress 2025-03-12

wordpress的前后台数据交换ajax

ajax是个耳熟能详的词儿,但因为有点儿复杂,博主一直是规避学习的,今天刚好碰到一个前台jquery向wp后台申请数据的问题。躲不过,那就慢慢调试吧。钩子wp的ajax还区分了用户,对于不同的用户(登录与否)采用不同的钩子,不过这里只...

建站相关 wordpress

wordpress 2025-03-07

WordPress中add_meta_box函数参数详解

add_meta_box 是 WordPress 中用于在后台编辑页面添加自定义元框(Meta Box)的函数。它允许开发者在文章、页面、自定义文章类型等编辑页面中添加自定义字段或内容。以下是 add_meta_box 函数的参数及其...

建站相关 wordpress

wordpress 2025-03-07

wordpress 手动添加自定义字段

自定义字段可以扩展文章的信息,也有很多相关的成熟的插件,比如Advanced Custom Fields (ACF) 插件,如果希望添加的字段不多,也不愿意为此安装过多的插件,我们也可以考虑手动来添加它。为post文章添加字段// 添...

建站相关 wordpress