我刚刚为我的插件类编写了两个方法,这使得注册/排队脚本和样式非常容易。当然,根据需要,最后一次修改时间始终添加为版本参数。也许这会帮助别人
如何使用它们:
/**
* For these examples we assume that your plugin assets are located in
* wp-content/plugins/my-plugin/path/to/assets/
*/
// add your style
$this->registerAsset(\'path/to/assets/your-style.css\');
// add your script works exactly the same
$this->registerAsset(\'path/to/assets/your-script.js\');
// add script with dependency
$this->registerAsset(\'path/to/assets/script-with-dependencies.js\', [
\'jquery\'
]);
// add script with internal dependency
$this->registerAsset(\'path/to/assets/script-with-dependencies.js\', [
\'jquery\',
$this->getAssetHandle(\'path/to/assets/your-script.js\')
]);
// for internal dependencies you can also pass the path of the dependency directly
$this->registerAsset(\'path/to/assets/script-with-dependencies.js\', [
\'jquery\',
\'path/to/assets/your-script.js\'
]);
还有一些选项,但只需查看有关所需源代码中的方法的文档即可:
class myPlugin
{
public function __construct()
{
}
/**
* Registers and enqueues a script or style
*
* @param STRING $path The path of the file you want to register or enqueue (relative to your plugin folder).
* @param ARRAY $dependencies The dependencies as you know from wp_enqueue_script, but allows also paths of other assets that were registered with this method.
* @param BOOLEAN $enqueue If FALSE it will only be registered, otherwise it will also be enqueued.
* @param STRING|NULL $type Type of the asset. Allowed types are \'script\' and \'style\'. If it is NULL, the type is automatically detected from the file extension.
* @param STRING $media To define \'media\' when adding CSS (Ignored for JS assets - JS assets always get TRUE for the in_footer parameter as this is usually the recommended way)
*
* @return BOOLEAN|NULL When $enqueue is TRUE, the return value is always NULL. Otherwise TRUE on success, FALSE on failure.
*/
public function registerAsset($path, $dependencies = [], $enqueue = true, $type = null, $media = \'all\')
{
$path = \'/\' . ltrim($path, \'/\');
$pluginDirName = explode(DIRECTORY_SEPARATOR, ltrim(str_replace(realpath(WP_PLUGIN_DIR), \'\', realpath(__FILE__)), DIRECTORY_SEPARATOR), 2)[0];
$pluginDir = realpath(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $pluginDirName);
if ($type === null) {
$extensions = [\'js\' => \'script\', \'css\' => \'style\'];
$extension = pathinfo($path, PATHINFO_EXTENSION);
$type = isset($extensions[$extension]) ? $extensions[$extension] : null;
}
if (!in_array($type, [\'script\', \'style\']) || !file_exists($pluginDir . $path)) {
return;
}
foreach ($dependencies as $index => $dependency) {
if (preg_match(\'/\\.(js|css)$/\', $dependency) && file_exists($pluginDir . DIRECTORY_SEPARATOR . ltrim($dependency, \'\\\\/\'))) {
$dependencies[$index] = $this->getAssetHandle($dependency);
}
}
$func = \'wp_\' . ($enqueue ? \'enqueue\' : \'register\') . \'_\' . $type;
return $func($this->getAssetHandle($path), plugins_url($pluginDirName . $path), $dependencies, filemtime($pluginDir . $path), $type === \'script\' ? true : $media);
}
/**
* Gets the handle of an asset that registerAsset() uses automatically
*
* @param STRING $path The path that you used with registerAsset()
*
* @return STRING The handle name of the asset
*/
public function getAssetHandle($path)
{
$pluginDirName = explode(DIRECTORY_SEPARATOR, ltrim(str_replace(realpath(WP_PLUGIN_DIR), \'\', realpath(__FILE__)), DIRECTORY_SEPARATOR), 2)[0];
return preg_replace(\'/[^a-zA-Z0-9]+/\', \'-\', $pluginDirName . \'-\' . $path);
}
}