钩子文档

site_status_tests

💡 云策文档标注

概述

site_status_tests 过滤器用于修改在站点上运行的站点健康检查测试。它允许开发者添加、移除或自定义直接和异步测试,以适应不同站点的特定需求。

关键要点

  • 过滤器名称:site_status_tests,用于过滤站点健康检查测试数组。
  • 测试类型:分为直接测试(direct)和异步测试(async),异步测试适用于耗时操作以避免管理界面加载延迟。
  • 参数结构:$tests 是一个关联数组,包含 direct 和 async 子数组,每个测试需提供唯一标识符、标签、回调函数等。
  • 版本变更:WordPress 5.6.0 引入了 async_direct_test 和 skip_cron 参数,5.2.0 首次引入此过滤器。

代码示例

// 移除背景更新测试的示例
function remove_background_updates_test( $tests ) {
    unset( $tests['async']['background_updates'] );
    return $tests;
}
add_filter( 'site_status_tests', 'remove_background_updates_test' );

// 一键禁用多个测试的示例(需谨慎使用)
add_filter('site_status_tests', function (array $test_type) {
    unset($test_type['direct']['php_version'],
        $test_type['direct']['theme_version'],
        $test_type['async']['background_updates']);
    return $test_type;
}, 10, 1);

注意事项

  • 测试标识符应唯一,建议插件和主题使用前缀以避免冲突。
  • 异步测试通过 admin-ajax.php 或 REST API 端点执行,需注意认证问题。
  • 禁用测试可能影响站点安全,如 PHP 版本或 .git 目录警告,应基于实际需求评估。

📄 原文内容

Filters which site status tests are run on a site.

Description

The site health is determined by a set of tests based on best practices from both the WordPress Hosting Team and web standards in general.

Some sites may not have the same requirements, for example the automatic update checks may be handled by a host, and are therefore disabled in core.
Or maybe you want to introduce a new test, is caching enabled/disabled/stale for example.

Tests may be added either as direct, or asynchronous ones. Any test that may require some time to complete should run asynchronously, to avoid extended loading periods within wp-admin.

Parameters

$testsarray[]
An associative array of direct and asynchronous tests.

  • direct array[]
    An array of direct tests.

    • ...$identifier array
      $identifier should be a unique identifier for the test. Plugins and themes are encouraged to prefix test identifiers with their slug to avoid collisions between tests.

      • label string
        The friendly label to identify the test.
      • test callable
        The callback function that runs the test and returns its result.
      • skip_cron bool
        Whether to skip this test when running as cron.
        }
    • async array[]
      An array of asynchronous tests.

      • ...$identifier array
        $identifier should be a unique identifier for the test. Plugins and themes are encouraged to prefix test identifiers with their slug to avoid collisions between tests.

        • label string
          The friendly label to identify the test.
        • test string
          An admin-ajax.php action to be called to perform the test, or if $has_rest is true, a URL to a REST API endpoint to perform the test.
        • has_rest bool
          Whether the $test property points to a REST API endpoint.
        • skip_cron bool
          Whether to skip this test when running as cron.
        • async_direct_test callable
          A manner of directly calling the test marked as asynchronous, as the scheduled event can not authenticate, and endpoints may require authentication.
          }

Source

$tests = apply_filters( 'site_status_tests', $tests );

Changelog

Version Description
5.6.0 Added the async_direct_test array key for asynchronous tests.
Added the skip_cron array key for all tests.
5.2.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Example usage to remove a test:

    function remove_background_updates_test( $tests ) {
    	unset( $tests['async']['background_updates'] );
    	return $tests;
    }
    add_filter( 'site_status_tests', 'remove_background_updates_test' );

    Also check out the Site Health Manager plugin that allows you to easily remove custom status test using this hook.

  2. Skip to note 6 content

    Here’s a quick one-liner to disable 3 Site Health tests that tend to be false alarms. You can drop it into a plugin or your theme’s functions.php.

    • PHP version (which is outside of user control anyway)
    • theme version (complains when it doesn’t find one of the WordPress twenty**** themes)
    • background updates (complains when you have a .git directory in your site root, for example)
    add_filter('site_status_tests', function (array $test_type) {
      unset($test_type['direct']['php_version'],
        $test_type['direct']['theme_version'],
        $test_type['async']['background_updates']);
      return $test_type;
    }, 10, 1);

You must log in before being able to contribute a note or feedback.

{“prefetch”:[{“source”:”document”,”where”:{“and”:[{“href_matches”:”/*”},{“not”:{“href_matches”:[“/wp-*.php”,”/wp-admin/*”,”/files/*”,”/wp-content/*”,”/wp-content/plugins/*”,”/wp-content/themes/wporg-developer-2023/*”,”/wp-content/themes/wporg-parent-2021/*”,”/*\?(.+)”]}},{“not”:{“selector_matches”:”a[rel~=”nofollow”]”}},{“not”:{“selector_matches”:”.no-prefetch, .no-prefetch a”}}]},”eagerness”:”conservative”}]}

var prism_settings = {“pluginUrl”:”https://developer.wordpress.org/wp-content/plugins/code-syntax-block/”};
//# sourceURL=mkaz-code-syntax-prism-js-js-extra

var autocomplete = {“ajaxurl”:”https://developer.wordpress.org/wp-admin/admin-ajax.php”,”nonce”:”a22d1a9404″,”post_type”:”wp-parser-hook”};
//# sourceURL=autocomplete-js-extra

wp.i18n.setLocaleData( { ‘text directionu0004ltr’: [ ‘ltr’ ] } );
//# sourceURL=wp-i18n-js-after

var wporgFunctionReferenceI18n = {“copy”:”Copy”,”copied”:”Code copied”,”expand”:”Expand code”,”collapse”:”Collapse code”,”sourceFile”:”wp-admin/includes/class-wp-site-health.php”};
//# sourceURL=wporg-developer-function-reference-js-extra

var quicktagsL10n = {“closeAllOpenTags”:”Close all open tags”,”closeTags”:”close tags”,”enterURL”:”Enter the URL”,”enterImageURL”:”Enter the URL of the image”,”enterImageDescription”:”Enter a description of the image”,”textdirection”:”text direction”,”toggleTextdirection”:”Toggle Editor Text Direction”,”dfw”:”Distraction-free writing mode”,”strong”:”Bold”,”strongClose”:”Close bold tag”,”em”:”Italic”,”emClose”:”Close italic tag”,”link”:”Insert link”,”blockquote”:”Blockquote”,”blockquoteClose”:”Close blockquote tag”,”del”:”Deleted text (strikethrough)”,”delClose”:”Close deleted text tag”,”ins”:”Inserted text”,”insClose”:”Close inserted text tag”,”image”:”Insert image”,”ul”:”Bulleted list”,”ulClose”:”Close bulleted list tag”,”ol”:”Numbered list”,”olClose”:”Close numbered list tag”,”li”:”List item”,”liClose”:”Close list item tag”,”code”:”Code”,”codeClose”:”Close code tag”,”more”:”Insert Read More tag”};
//# sourceURL=quicktags-js-extra

var wporg_note_feedback = {“show”:”Show feedback”,”hide”:”Hide feedback”,”hide_feedback”:”Hide feedback form”,”add_feedback”:”Add feedback”};
//# sourceURL=wporg-developer-user-notes-feedback-js-extra

var wporg_note_preview = {“ajaxurl”:”https://developer.wordpress.org/wp-admin/admin-ajax.php”,”nonce”:”de3c1c9510″,”preview”:”preview note”,”preview_empty”:”Nothing to preview”,”is_admin”:””};
//# sourceURL=wporg-developer-preview-js-extra

_stq = window._stq || [];
_stq.push([ “view”, {“v”:”ext”,”blog”:”209306761″,”post”:”122881″,”tz”:”0″,”srv”:”developer.wordpress.org”,”j”:”1:15.5″} ]);
_stq.push([ “clickTrackerInit”, “209306761”, “122881” ]);
//# sourceURL=jetpack-stats-js-before

var wporgGlobalHeaderI18n = {“openSearchLabel”:”Open Search”,”closeSearchLabel”:”Close Search”,”overflowMenuLabel”:”More menu”};
//# sourceURL=wporg-global-header-script-js-extra

{“baseUrl”:”https://s.w.org/images/core/emoji/17.0.2/72×72/”,”ext”:”.png”,”svgUrl”:”https://s.w.org/images/core/emoji/17.0.2/svg/”,”svgExt”:”.svg”,”source”:{“concatemoji”:”https://developer.wordpress.org/wp-includes/js/wp-emoji-release.min.js?ver=7.1-alpha-62239″}}

/*! This file is auto-generated */
const a=JSON.parse(document.getElementById(“wp-emoji-settings”).textContent),o=(window._wpemojiSettings=a,”wpEmojiSettingsSupports”),s=[“flag”,”emoji”];function i(e){try{var t={supportTests:e,timestamp:(new Date).valueOf()};sessionStorage.setItem(o,JSON.stringify(t))}catch(e){}}function c(e,t,n){e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillText(t,0,0);t=new Uint32Array(e.getImageData(0,0,e.canvas.width,e.canvas.height).data);e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillText(n,0,0);const a=new Uint32Array(e.getImageData(0,0,e.canvas.width,e.canvas.height).data);return t.every((e,t)=>e===a[t])}function p(e,t){e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillText(t,0,0);var n=e.getImageData(16,16,1,1);for(let e=0;e{s[e]=t(o,e,n,a)}),s}function r(e){var t=document.createElement(“script”);t.src=e,t.defer=!0,document.head.appendChild(t)}a.supports={everything:!0,everythingExceptFlag:!0},new Promise(t=>{let n=function(){try{var e=JSON.parse(sessionStorage.getItem(o));if(“object”==typeof e&&”number”==typeof e.timestamp&&(new Date).valueOf(){i(n=e.data),r.terminate(),t(n)})}catch(e){}i(n=f(s,u,c,p))}t(n)}).then(e=>{for(const n in e)a.supports[n]=e[n],a.supports.everything=a.supports.everything&&a.supports[n],”flag”!==n&&(a.supports.everythingExceptFlag=a.supports.everythingExceptFlag&&a.supports[n]);var t;a.supports.everythingExceptFlag=a.supports.everythingExceptFlag&&!a.supports.flag,a.supports.everything||((t=a.source||{}).concatemoji?r(t.concatemoji):t.wpemoji&&t.twemoji&&(r(t.twemoji),r(t.wpemoji)))});
//# sourceURL=https://developer.wordpress.org/wp-includes/js/wp-emoji-loader.min.js