wptrt/customize-section-button

WordPress自定义器的按钮部分。


README

这是一个WordPress自定义器的自定义部分类,允许主题作者创建一个具有“按钮”的部分。其主要目的是提供一个在自定义器中创建“专业”或“升级销售”部分的标准方法。然而,从技术上讲,它也可以用来链接到任何地方。

用法

以下代码应集成到您主题现有的自定义器代码中。

// Use statements must come after the `namespace` statement at the top of the
// file but before any other code.

use WPTRT\Customize\Section\Button;

// Register the "button" section.

add_action( 'customize_register', function( $manager ) {

	$manager->register_section_type( Button::class );

	$manager->add_section(
		new Button( $manager, 'themeslug_pro', [
			'title'       => __( 'ThemeName Pro', 'themeslug' ),
			'button_text' => __( 'Go Pro',        'themeslug' ),
			'button_url'  => 'http://example.com'
		] )
	);

} );

参数

“按钮”部分接受与正常“WP_Customize_Section”相同的所有参数。但是,增加了两个额外的参数。

  • 'button_text' - 显示在部分按钮中的文本。默认为活动主题名称。
  • 'button_url' - 用于部分按钮的URL。回退到“主题URI”或“作者URI”。

加载所需的CSS和JS

CSS和JS文件的开发版本和生产版本都可用。

为了避免加载额外的资源,我们鼓励主题作者将以下文件导入他们的自定义CSS和JS构建过程中

  • /path/to/customize-section-button/resources/js/customize-controls.js
  • /path/to/customize-section-button/resources/scss/customize-controls.scss.

但是,如果您决定直接排入生产资源,请将以下内容集成到您的自定义器代码中。

// Load the JS and CSS.

add_action( 'customize_controls_enqueue_scripts', function() {

	$version = wp_get_theme()->get( 'Version' );

	wp_enqueue_script(
		'wptrt-customize-section-button',
		get_theme_file_uri( 'path/to/customize-section-button/public/js/customize-controls.js' ),
		[ 'customize-controls' ],
		$version,
		true
	);

	wp_enqueue_style(
		'wptrt-customize-section-button',
		get_theme_file_uri( 'path/to/customize-section-button/public/css/customize-controls.css' ),
		[ 'customize-controls' ],
 		$version
	);

} );

自动加载

您需要使用自动加载器。理想情况下,这将是一个Composer。但是,我们有一个基本的自动加载器可用于与主题一起使用,如果需要的话。

Composer

从命令行

composer require wptrt/customize-section-button

WPTRT自动加载器

如果您使用WPTRT自动加载器,请使用以下代码

include get_theme_file_path( 'path/to/autoload/src/Loader.php' );

$loader = new \WPTRT\Autoload\Loader();

$loader->add( 'WPTRT\\Customize\\Section', get_theme_file_path( 'path/to/customize-section-button/src' ) );

$loader->register();