underpin/style-loader

1.2.0 2021-11-23 14:21 UTC

This package is auto-updated.

Last update: 2024-09-23 22:53:23 UTC


README

一个帮助向WordPress网站添加样式的加载器。

安装

使用Composer

composer require underpin/loaders/styles

手动安装

此插件使用内置的自动加载器,所以只要它在Underpin之前被引入,它应该会按预期工作。

require_once(__DIR__ . '/underpin-styles/styles.php');

设置

  1. 安装Underpin。请参阅Underpin文档
  2. 根据需要注册新样式。

示例

一个非常基本的示例可能如下所示。

underpin()->styles()->add( 'test', [
	'src'         => 'path/to/style/src',
	'name'        => 'test',
	'description' => 'The description',
] );

或者,您可以通过扩展Style并直接引用扩展类来实现

underpin()->styles()->add('key','Namespace\To\Class');

添加样式

要添加样式,请运行加载器并引用样式ID,如下所示

underpin()->style()->enqueue('test'); // Enqueue the test style

使用中间件添加

在您始终需要添加样式的场合,您可以使用提供的添加中间件。

在管理界面添加

underpin()->styles()->add( 'test', [
        'handle'      => 'test',
        'src'         => 'path/to/style/src',
        'name'        => 'test',
        'description' => 'The description',
        'middlewares' => [
          'Underpin_Styles\Factories\Enqueue_Admin_Style'
        ]
] );

在前端添加

underpin()->styles()->add( 'test', [
        'handle'      => 'test',
        'src'         => 'path/to/style/src',
        'name'        => 'test',
        'description' => 'The description',
        'middlewares' => [
          'Underpin_Styles\Factories\Enqueue_Style'
        ]
] );

在前端和后端同时添加

underpin()->styles()->add( 'test', [
        'handle'      => 'test',
        'src'         => 'path/to/style/src',
        'name'        => 'test',
        'description' => 'The description',
        'middlewares' => [
          'Underpin_Styles\Factories\Enqueue_Style',
          'Underpin_Styles\Factories\Enqueue_Admin_Style'
        ]
] );

创建您自己的中间件

数组middlewares使用Underpin::make_class创建类实例。这意味着您可以选择

  1. 一个字符串,它引用了Style_Middleware的一个实例(请参阅上面的示例)。
  2. 一个参数数组,用于即时构建Style_Middleware的实例。
underpin()->styles()->add( 'test', [
	'handle'      => 'test',
	'src'         => 'path/to/style/src',
	'name'        => 'test',
	'description' => 'The description',
	'middlewares' => [
		'Underpin_Styles\Factories\Enqueue_Style',            // Will enqueue the style on the front end all the time.
		[                                                     // Will instantiate an instance of Style_Middleware_Instance using the provided arguments
			'name'                => 'Custom setup params',
			'description'         => 'Sets up custom parameters specific to this style',
			'priority'            => 10, // Optional. Default 10.
			'do_actions_callback' => function ( \Underpin_Styles\Abstracts\Style $loader_item ) {
				// Do actions
			},
		],
	],
] );