向所有小部件设置添加配置选项

向所有小部件设置添加配置选项

本文介绍了Wordpress:向所有小部件设置添加配置选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Wordpress 中,我的目标是能够为我放在侧边栏中的每个小部件提供一个自定义类.最好我想在每个小部件的小部件设置中添加这个类.还有第 3 方小部件.

In Wordpress, my goal is to be able to give a custom class to every widget that I put in the sidebar.Preferably I would like to add this class in the widget settings of each widget. ALSO 3rd party widgets.

我正在考虑更改类名,因为类名已经传递给 register_sidebar 函数 (%2$s):

I was thinking of changing the classname, because the classname is already passed to the register_sidebar function (%2$s):

<?php
register_sidebar(array('before_widget' => '<aside id="%1$s" class="widget %2$s blue">'));
?>

当然我不应该更改 WP 核心代码或 3rd 方插件.这意味着我需要编写一个插件来挂钩小部件配置过程.

Ofcourse I shouldn't change WP core code or 3rd party plugins. This means that I need to write a plugin that will hook into the widget configuration process.

我已经发现可以通过挂钩操作in_widget_form"来修改所有小部件表单:

I already found out that it's possible to modify all widgets forms, by hooking into action 'in_widget_form':

<?php
add_action('in_widget_form', 'teaserWidgetAddToForm');

function teaserWidgetAddToForm($widget_instance) {
  ?>
  <label for="<?php echo $widget_instance->get_field_id('teaser-classname'); ?>">Custom classname(s):</label>
  <input type="text" id="<?php echo $widget_instance->get_field_id('teaser-classname'); ?>" name="<?php echo $widget_instance->get_field_name('teaser-classname'); ?>">
  <?php
}
?>

这个数据应该是Widget超类保存的,但是怎么找回这个数据(所以后面打开widget设置的时候会显示你之前填写的)

This data should be saved by the Widget super class, but how do you retrieve this data (so when opening the widget settings later shows what you filled in earlier)

而且,这个保存的数据应该放在小部件实例中 - 这是怎么做的?我想通过使用类似的东西:

And, this saved data should be put in the widget instance -how is this done? I guess by using something like:

<?php
$wp_registered_widgets[<widget_id>]['callback'][0]['widget_options']['classname'] = <chosen_class>;
?>

所以基本上我有两个问题:

So basically I have 2 questions:

  • 我是否使用了正确的方法来解决这个问题(单个小部件的样式)
  • 如果是这样,我如何修改小部件实例以保存和检索其他设置,而无需修改 Wordpress 或第 3 方插件源代码.

推荐答案

  1. 默认情况下,每个小部件实例都有一个唯一 ID,可用于为其设置样式.

  1. By default each widget instance has a unique ID which can be used to style it.

为了保存在 in_widget_form 操作中添加的字段中的数据,您还需要有一个 widget_update_callback 过滤器.

In order to save data from fields added in an in_widget_form action, you need to also have a widget_update_callback filter.

function my_widget_update_callback($instance, $new_instance) {
    $instance['my_classnames'] = $new_instance['my_classnames'];
    return $instance;
}

要在表单中显示保存的值,您需要先检索实例设置.

To display the saved value in the form you'll need to first retrieve the instance settings.

function my_in_widget_form($instance) {
    $settings = $instance->get_settings();
    …

最后,我认为添加自定义类的最简单方法是在 widget_display_callback 过滤器中,它在显示小部件之前运行.您必须自己显示小部件,因为您只能从该过滤器返回一个实例,并且实例不控制小部件 CSS 类.

Finally, I think the simplest way to add your custom classes is in a widget_display_callback filter, which runs before a widget is displayed. You have to display the widget yourself because you can only return an instance from this filter, and instances do not control the widget CSS classes.

function my_widget_display_callback($instance, $widget, $args) {
    if (!isset($instance['my_classnames'])) {
        return $instance;
    }

    $widget_classname = $widget->widget_options['classname'];
    $my_classnames = $instance['my_classnames'];

    $args['before_widget'] = str_replace($widget_classname, "{$widget_classname} {$my_classnames}", $args['before_widget']);

    $widget->widget($args, $instance);

    return false;
}

有关与小部件相关的可用挂钩的更多信息,请参阅此文章:http://shibashake.com/wordpress-theme/wordpress-widget-system

See this article for more info on the available hooks related to widgets: http://shibashake.com/wordpress-theme/wordpress-widget-system

这篇关于Wordpress:向所有小部件设置添加配置选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:03