函数文档

wp_credits()

💡 云策文档标注

概述

wp_credits() 函数用于检索 WordPress 贡献者名单,支持指定版本和区域设置,并利用缓存机制优化性能。

关键要点

  • 函数返回贡献者列表数组或错误时返回 false
  • 参数 $version 和 $locale 可选,默认使用当前 WordPress 版本和用户区域设置
  • 通过 get_site_transient() 缓存结果,缓存键为 'wordpress_credits_' . $locale
  • 缓存失效时,从 WordPress API 获取数据并更新缓存
  • 涉及多个相关函数,如 wp_get_wp_version()、get_user_locale()、wp_remote_get() 等

代码示例

function wp_credits( $version = '', $locale = '' ) {
    if ( ! $version ) {
        $version = wp_get_wp_version();
    }

    if ( ! $locale ) {
        $locale = get_user_locale();
    }

    $results = get_site_transient( 'wordpress_credits_' . $locale );

    if ( ! is_array( $results )
        || str_contains( $version, '-' )
        || ( isset( $results['data']['version'] ) && ! str_starts_with( $version, $results['data']['version'] ) )
    ) {
        $url     = "https://api.wordpress.org/core/credits/1.1/?version={$version}&locale={$locale}";
        $options = array( 'user-agent' => 'WordPress/' . $version . '; ' . home_url( '/' ) );

        if ( wp_http_supports( array( 'ssl' ) ) ) {
            $url = set_url_scheme( $url, 'https' );
        }

        $response = wp_remote_get( $url, $options );

        if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
            return false;
        }

        $results = json_decode( wp_remote_retrieve_body( $response ), true );

        if ( ! is_array( $results ) ) {
            return false;
        }

        set_site_transient( 'wordpress_credits_' . $locale, $results, DAY_IN_SECONDS );
    }

    return $results;
}

注意事项

  • 函数在 WordPress 3.2.0 引入,5.6.0 版本添加了 $version 和 $locale 参数
  • 缓存有效期为 DAY_IN_SECONDS(一天),减少对 API 的频繁请求
  • 使用 HTTPS 协议确保安全传输,如果支持 SSL
  • 错误处理包括检查 HTTP 响应码和 JSON 解码结果

📄 原文内容

Retrieves the contributor credits.

Parameters

$versionstringrequired
WordPress version. Defaults to the current version.
$localestringrequired
WordPress locale. Defaults to the current user’s locale.

Return

array|false A list of all of the contributors, or false on error.

Source

function wp_credits( $version = '', $locale = '' ) {
	if ( ! $version ) {
		$version = wp_get_wp_version();
	}

	if ( ! $locale ) {
		$locale = get_user_locale();
	}

	$results = get_site_transient( 'wordpress_credits_' . $locale );

	if ( ! is_array( $results )
		|| str_contains( $version, '-' )
		|| ( isset( $results['data']['version'] ) && ! str_starts_with( $version, $results['data']['version'] ) )
	) {
		$url     = "https://api.wordpress.org/core/credits/1.1/?version={$version}&locale;={$locale}";
		$options = array( 'user-agent' => 'WordPress/' . $version . '; ' . home_url( '/' ) );

		if ( wp_http_supports( array( 'ssl' ) ) ) {
			$url = set_url_scheme( $url, 'https' );
		}

		$response = wp_remote_get( $url, $options );

		if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
			return false;
		}

		$results = json_decode( wp_remote_retrieve_body( $response ), true );

		if ( ! is_array( $results ) ) {
			return false;
		}

		set_site_transient( 'wordpress_credits_' . $locale, $results, DAY_IN_SECONDS );
	}

	return $results;
}

Changelog

Version Description
5.6.0 Added the $version and $locale parameters.
3.2.0 Introduced.