函数文档

paused_themes_notice()

💡 云策文档标注

概述

paused_themes_notice() 函数用于在 WordPress 后台显示一个管理通知,当某些主题因错误被暂停时提醒用户。该函数检查当前页面、用户权限和暂停主题状态,仅在满足条件时输出错误通知。

关键要点

  • 函数仅在非 themes.php 页面、当前用户具有 resume_themes 权限且存在暂停主题时触发
  • 使用 wp_admin_notice() 输出错误类型的通知,消息包含主题加载失败信息和指向主题管理页面的链接
  • 依赖多个核心函数如 current_user_can()、__()、esc_url() 和 admin_url() 来确保安全性和本地化

代码示例

function paused_themes_notice() {
    if ( 'themes.php' === $GLOBALS['pagenow'] ) {
        return;
    }

    if ( ! current_user_can( 'resume_themes' ) ) {
        return;
    }

    if ( ! isset( $GLOBALS['_paused_themes'] ) || empty( $GLOBALS['_paused_themes'] ) ) {
        return;
    }

    $message = sprintf(
        '%s%s%s',
        __( 'One or more themes failed to load properly.' ),
        __( 'You can find more details and make changes on the Themes screen.' ),
        esc_url( admin_url( 'themes.php' ) ),
        __( 'Go to the Themes screen' )
    );
    wp_admin_notice(
        $message,
        array(
            'type'           => 'error',
            'paragraph_wrap' => false,
        )
    );
}

注意事项

  • 函数从 WordPress 5.2.0 版本引入,需确保兼容性
  • 通知仅在特定条件下显示,避免在不必要页面或用户无权限时干扰
  • 消息使用本地化函数 __(),支持多语言环境

📄 原文内容

Renders an admin notice in case some themes have been paused due to errors.

Source

function paused_themes_notice() {
	if ( 'themes.php' === $GLOBALS['pagenow'] ) {
		return;
	}

	if ( ! current_user_can( 'resume_themes' ) ) {
		return;
	}

	if ( ! isset( $GLOBALS['_paused_themes'] ) || empty( $GLOBALS['_paused_themes'] ) ) {
		return;
	}

	$message = sprintf(
		'<p><strong>%s</strong><br>%s</p><p><a href="%s">%s</a></p>',
		__( 'One or more themes failed to load properly.' ),
		__( 'You can find more details and make changes on the Themes screen.' ),
		esc_url( admin_url( 'themes.php' ) ),
		__( 'Go to the Themes screen' )
	);
	wp_admin_notice(
		$message,
		array(
			'type'           => 'error',
			'paragraph_wrap' => false,
		)
	);
}

Changelog

Version Description
5.2.0 Introduced.