函数文档

wp_script_is()

💡 云策文档标注

概述

wp_script_is() 函数用于检查指定脚本是否已添加到 WordPress 的脚本队列中。它接受脚本句柄和状态参数,返回布尔值表示脚本是否处于指定状态。

关键要点

  • 函数用于判断脚本是否已入队,常用于条件逻辑中避免重复加载脚本。
  • 参数 $handle 为必需,指定脚本名称;$status 可选,默认为 'enqueued',可接受 'enqueued'、'registered'、'queue'、'to_do' 和 'done'。
  • 返回布尔值,表示脚本是否处于指定状态。
  • 内部调用 wp_scripts()->query() 实现查询功能。

代码示例

$handle = 'fluidVids.js';
$list = 'enqueued';
if (wp_script_is( $handle, $list )) {
    return;
} else {
    wp_register_script( 'fluidVids.js', plugin_dir_url(__FILE__).'js/fluidvids.min.js');
    wp_enqueue_script( 'fluidVids.js' );
}

📄 原文内容

Determines whether a script has been added to the queue.

Description

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Parameters

$handlestringrequired
Name of the script.
$statusstringoptional
Status of the script to check. Default 'enqueued'.
Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.

Return

bool Whether the script is queued.

Source

function wp_script_is( $handle, $status = 'enqueued' ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

	return (bool) wp_scripts()->query( $handle, $status );
}

Changelog

Version Description
3.5.0 'enqueued' added as an alias of the 'queue' list.
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Basic Example
    This would check if the script named ‘fluidVids.js’ is enqueued. If it is enqueued, it does nothing. If it is not enqueued, the files are then registered and enqueued.

    $handle = 'fluidVids.js';
    $list = 'enqueued';
    if (wp_script_is( $handle, $list )) {
        return;
    } else {
        wp_register_script( 'fluidVids.js', plugin_dir_url(__FILE__).'js/fluidvids.min.js');
        wp_enqueue_script( 'fluidVids.js' );
    }