函数文档

core_update_footer()

💡 云策文档标注

概述

core_update_footer() 函数用于返回 WordPress 核心更新的页脚消息,根据用户权限和更新状态动态生成不同提示。它检查当前用户是否有更新核心的权限,并基于 WordPress 版本和更新响应类型输出相应信息。

关键要点

  • 函数返回字符串类型的核心更新页脚消息,参数 $msg 为可选字符串。
  • 使用 current_user_can('update_core') 检查用户权限,无权限时返回当前版本号。
  • 通过 get_preferred_from_update_core() 获取核心更新信息,处理对象和属性缺失情况。
  • 针对开发版本(如 alpha、beta、RC)返回特定提示,包含版本号和更新链接。
  • 根据 $cur->response 的值(如 'upgrade' 或 'latest')返回不同消息,包括更新链接或当前版本号。
  • 函数内部使用多个 WordPress 核心函数,如 wp_get_wp_version()、network_admin_url()、__() 和 get_bloginfo()。

代码示例

function core_update_footer( $msg = '' ) {
    if ( ! current_user_can( 'update_core' ) ) {
        return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
    }

    $cur = get_preferred_from_update_core();
    if ( ! is_object( $cur ) ) {
        $cur = new stdClass();
    }
    if ( ! isset( $cur->current ) ) {
        $cur->current = '';
    }
    if ( ! isset( $cur->response ) ) {
        $cur->response = '';
    }

    $is_development_version = preg_match( '/alpha|beta|RC/', wp_get_wp_version() );
    if ( $is_development_version ) {
        return sprintf(
            __( 'You are using a development version (%1$s). Cool! Please stay updated.' ),
            get_bloginfo( 'version', 'display' ),
            network_admin_url( 'update-core.php' )
        );
    }

    switch ( $cur->response ) {
        case 'upgrade':
            return sprintf(
                '%s',
                network_admin_url( 'update-core.php' ),
                sprintf( __( 'Get Version %s' ), $cur->current )
            );
        case 'latest':
        default:
            return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
    }
}

注意事项

  • 用户贡献笔记中提到,原函数在提示新版本可用时未显示当前版本号,建议使用自定义函数并通过 add_filter('update_footer', 'smarter_update_footer', 9999) 钩子覆盖。
  • 自定义函数 smarter_update_footer() 在 'upgrade' 情况下同时显示当前版本和新版本,以提供更完整信息。

📄 原文内容

Returns core update footer message.

Parameters

$msgstringrequired

Return

string

Source

function core_update_footer( $msg = '' ) {
	if ( ! current_user_can( 'update_core' ) ) {
		/* translators: %s: WordPress version. */
		return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
	}

	$cur = get_preferred_from_update_core();

	if ( ! is_object( $cur ) ) {
		$cur = new stdClass();
	}

	if ( ! isset( $cur->current ) ) {
		$cur->current = '';
	}

	if ( ! isset( $cur->response ) ) {
		$cur->response = '';
	}

	$is_development_version = preg_match( '/alpha|beta|RC/', wp_get_wp_version() );

	if ( $is_development_version ) {
		return sprintf(
			/* translators: 1: WordPress version number, 2: URL to WordPress Updates screen. */
			__( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ),
			get_bloginfo( 'version', 'display' ),
			network_admin_url( 'update-core.php' )
		);
	}

	switch ( $cur->response ) {
		case 'upgrade':
			return sprintf(
				'<strong><a href="%s">%s</a></strong>',
				network_admin_url( 'update-core.php' ),
				/* translators: %s: WordPress version. */
				sprintf( __( 'Get Version %s' ), $cur->current )
			);

		case 'latest':
		default:
			/* translators: %s: WordPress version. */
			return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
	}
}

Changelog

Version Description
2.3.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    I find it annoying when the admin footer tells you that a new WordPress version is available (for example “Get Version 4.6.1”) but does not tell you what your current version is.
    Here is a patch:

    // Improve Admin footer update notice.
    if (!function_exists('smarter_update_footer')){
    	function smarter_update_footer( $msg = '' ) {
    		if ( !current_user_can('update_core') )
    			return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
    
    		$cur = get_preferred_from_update_core();
    		if ( ! is_object( $cur ) )
    			$cur = new stdClass;
    
    		if ( ! isset( $cur->current ) )
    			$cur->current = '';
    
    		if ( ! isset( $cur->url ) )
    			$cur->url = '';
    
    		if ( ! isset( $cur->response ) )
    			$cur->response = '';
    
    		switch ( $cur->response ) {
    		case 'development' :
    			return sprintf( __( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ), get_bloginfo( 'version', 'display' ), network_admin_url( 'update-core.php' ) );
    
    		case 'upgrade' :
    			return '<strong>'.sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) ).' - <a href="' . network_admin_url( 'update-core.php' ) . '">' . sprintf( __( 'Get Version %s' ), $cur->current ) . '</a></strong>';
    
    		case 'latest' :
    		default :
    			return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
    		}
    	}
    	add_filter( 'update_footer', 'smarter_update_footer', 9999);
    }