首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在主题自定义程序侧栏中放置一条消息

在主题自定义程序侧栏中放置一条消息
EN

WordPress Development用户
提问于 2018-09-09 14:53:53
回答 1查看 460关注 0票数 0

如何在主题自定义工具侧边栏中创建自定义消息?

最好是在顶部,在这里您可以看到活动主题标题和按钮来更改主题。

原因:对于一个项目,由于各种原因,所有的定制部分都被隐藏了。用户可能会在这里看到控件,所以如果他们想要在站点上更改某些内容,我想给出一条消息来解释应该做什么。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-09-10 01:50:49

要做你想做的事情,有两个解决方案是最优的。

解决方案#1

最简单的方法是使用WordPress自定义器的notifications -因为这个API是专门设计的,用于将消息添加到定制器的位置,用户可以期望看到来自主题的一致通知。

根据您的描述,全局通知将是这里最合理的选择。首先,您需要创建一个JS文件,以便在定制程序中排队。确保您的消息翻译就绪,这也是一个很好的实践,这可以使用wp_局部化_脚本()完成。此代码演示如何实现添加已翻译的消息和脚本:

代码语言:javascript
复制
function wpse313731_custom_notification_enqueue_scripts() {
    $handle = 'wpse313731-custom-notification';

    // Register the script.
    wp_register_script( $handle, plugins_url( "$handle.js", __FILE__ ), array( 'customize-controls' ) );
    // Translate the message.
    $translated_msg = array(
        'msg' => esc_html( 'This is a custom message being added to the WordPress Customizer.', 'your-text-domain' ),
    );

    // Localize script with translated data.
    wp_localize_script( $handle, 'wpse313731', $translated_msg );
    // Enqueue the script.
    wp_enqueue_script( $handle );
}

应该在调用customize_controls_enqueue_scripts钩子时触发此方法,因此我们可以添加以下代码:

代码语言:javascript
复制
add_action( 'customize_controls_enqueue_scripts', 'wpse313731_custom_notification_enqueue_scripts' );

现在,对于JS,我们希望确保定制器已经就绪,然后调用notification来添加我们的自定义通知。在上面的PHP代码中,我们告诉WordPress我们的脚本将被命名为wpse313731-定制通知. JS,所以这个例子显示了wpse313731中的JS代码会是什么样子:

代码语言:javascript
复制
( function( $ ) {

    'use strict';

    wp.customize.bind( 'ready', function () {
        wp.customize.notifications.add(
            'wpse313731-custom-notification',
            new wp.customize.Notification(
                'wpse313731-custom-notification', {
                    dismissible: true,
                    message: wpse313731.msg,
                    type: 'error'
                }
            )
        );
    } );

} )( jQuery );

上面的示例JS代码创建了一个可撤销的错误通知。通知可以是不可解雇的(这意味着用户不能只是关闭消息),如果您只是更改可解雇键false。显示带有红色的错误通知,但可以将类型更改为'none''error''warning''info''success'

这是如何显示这些类型中的每一种以供参考:

我继续使用把这段代码放在一个要点中,这样您就可以安装.zip插件并自己进行测试,进行任何您想要的更改以满足您的需要。

解决方案#2

第二个解决方案是使用JS将消息附加到该区域。首先,您需要创建包含如下内容的模板:

代码语言:javascript
复制
function wpse313731_print_templates() {
    ?>
    
        <p class="wpse313731-custom-message"><?php esc_html_e( 'This is a custom message being added to the WordPress Customizer.', 'your-text-domain' ) ?></p>

然后,我们需要确保添加包含要在customize_controls_print_footer_scripts 钩子中激发的模板的方法:

代码语言:javascript
复制
add_action( 'customize_controls_print_footer_scripts', 'wpse313731_print_templates' );

在JS中,您需要等待面板准备就绪并加载,然后使用wp.template呈现模板(确保不包括上面脚本ID中的tmpl部分)。完成后,我们只需将您的自定义消息附加到主题面板中,这就是您正在讨论的添加消息的部分。看起来会是这样的:

代码语言:javascript
复制
( function( $ ) {
    'use strict';
    wp.customize.bind( 'ready', function () {
        wp.customize.panel( 'themes', function( panel ) {
            panel.deferred.embedded.done( function() {
                var customMessage;
                customMessage = $( wp.template( 'wpse313731-custom-message' )() );
                panel.headContainer.append( customMessage );
            } );
        } );
    } );
} )( jQuery );

当然,JS文件需要加载在自定义程序中,并且应该使用wp_排队_脚本在customize_controls_enqueue_scripts 钩子中加载:

代码语言:javascript
复制
function wpse313731_enqueue_scripts() {
    $handle = 'wpse313731-custom-message';
    wp_enqueue_script( $handle, plugins_url( "$handle.js", __FILE__ ), array( 'customize-controls' ) );
}

add_action( 'customize_controls_enqueue_scripts', 'wpse313731_enqueue_scripts' );

您可能希望添加一些样式来使这看起来更好一些,并且可以像JS一样在customize_controls_enqueue_scripts钩子中加载CSS,但是不使用wp_enqueue_script(),而是使用wp_排队_风格()。在JS中将类添加到主题面板headContainer也可能有帮助,因此如果加载了这个部分,您可以更容易地对其进行样式化。

我还创建了这个要旨,因此您可以安装.zip插件,该插件可以对消息进行样式化,以更好地适应这一领域,并完成解决方案2中讨论的所有上述内容,供您进一步实验。

票数 2
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/313731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档