问题描述

我想以编程方式向我的两个侧边栏添加小工具。我找不到任何正式的做法吗?

我开始查看数据库。我发现这是’sidebars_widgets’ 选项,它将小工具放在边栏上。当查看选项时,窗口小工具名称的末尾添加如下:widget_name-6 。那个号码来自哪里?

关于如何解决这个问题的任何想法?

最佳解决方案

当我开始这个答案时,应该只是一个小笔记。那么我失败了抱歉! 留在我身边,有一个隐藏在深处的好东西…

如何存储 WordPress 小工具

窗口小工具列表存储在名为'sidebars_widgets'的选项中。 var_export()可能会给出以下内容:

array (
  'wp_inactive_widgets' =>
  array (
  ),
  'top-widget' =>
  array (
  ),
  'bottom-widget' =>
  array (
  ),
  'array_version' => 3,
)

忽略'wp_inactive_widgets''array_version'。我们不必关心那些。其他键是注册的侧边栏的标识符。在这种情况下,侧边栏可能已经注册了此代码:

// Register two sidebars.
$sidebars = array ( 'a' => 'top-widget', 'b' => 'bottom-widget' );
foreach ( $sidebars as $sidebar )
{
    register_sidebar(
        array (
            'name'          => $sidebar,
            'id'            => $sidebar,
            'before_widget' => '',
            'after_widget'  => ''
        )
    );
}

默认情况下,侧边栏注册后为空。当然。

对于每个注册的窗口小工具类,都会创建一个单独的选项,其中包含所有必该选项以字符串 widget_为前缀。要获取所有活动的 RSS 小工具的选项,我们必须研究…

get_option( 'widget_rss' );

可能输出:

array (
  2 =>
  array (
    'title' => 'WordPress Stack Exchange',
    'url' => 'http://wordpress.stackexchange.com/feeds',
    'link' => 'http://wordpress.stackexchange.com/questions',
    'items' => 5,
    'show_summary' => 1,
    'show_author' => 0,
    'show_date' => 0,
  ),
)

注意数字 2. 多个实例的参数都存储在按数字排序的这一个选项中。

要查看哪些小工具类已经被 WordPress 知道,请转到 wp-admin/options.php 并向下滚动,直到看到如下所示:

是的,序列化的数据。不,你不能在这里阅读。别担心,你不必。

演示小工具

为了更好地说明内部工作,我写了一个非常简单的演示小工具:

/**
 * Super simple widget.
 */
class T5_Demo_Widget extends WP_Widget
{
    public function __construct()
    {                      // id_base        ,  visible name
        parent::__construct( 't5_demo_widget', 'T5 Demo Widget' );
    }

    public function widget( $args, $instance )
    {
        echo $args['before_widget'], wpautop( $instance['text'] ), $args['after_widget'];
    }

    public function form( $instance )
    {
        $text = isset ( $instance['text'] )
            ? esc_textarea( $instance['text'] ) : '';
        printf(
            '<textarea class="widefat" rows="7" cols="20" id="%1$s" name="%2$s">%3$s</textarea>',
            $this->get_field_id( 'text' ),
            $this->get_field_name( 'text' ),
            $text
        );
    }
}

注意构造函数:'t5_demo_widget'$id_base,这个小工具的标识符。如您在屏幕截图中所看到的,其参数存储在选项 widget_t5_demo_widget 中。所有您的自定义小工具将被视为这样。你不必猜名字。而且,由于您已经编写了您的小工具 (可能),您可以知道类 $instance 参数中的所有参数。

主题基础

首先你必须注册一些侧边栏和自定义小工具。对此的正确操作很容易记住:'widgets_init'。将所有内容放入容器 – 类或功能。为了简单起见,我将使用一个名为 t5_default_widget_demo()的函数。

所有以下代码都进入 functions.phpT5_Demo_Widget 类应该已经加载了。我只是把它放入同一个文件…

add_action( 'widgets_init', 't5_default_widget_demo' );

function t5_default_widget_demo()
{
    // Register our own widget.
    register_widget( 'T5_Demo_Widget' );

    // Register two sidebars.
    $sidebars = array ( 'a' => 'top-widget', 'b' => 'bottom-widget' );
    foreach ( $sidebars as $sidebar )
    {
        register_sidebar(
            array (
                'name'          => $sidebar,
                'id'            => $sidebar,
                'before_widget' => '',
                'after_widget'  => ''
            )
        );
    }

到目前为止,这么简单。我们的主题现在是小工具就绪,演示小工具是已知的。现在的乐趣。

$active_widgets = get_option( 'sidebars_widgets' );

if ( ! empty ( $active_widgets[ $sidebars['a'] ] )
    or ! empty ( $active_widgets[ $sidebars['b'] ] )
)
{   // Okay, no fun anymore. There is already some content.
    return;
}

你真的不想破坏用户设置。如果侧边栏中已经有一些内容,那么您的代码不应该运行它。这就是为什么我们在这种情况下停止。

好吧,假设边栏是空的… 我们需要一个柜台:

$counter = 1;

小工具编号。这些数字是 WordPress 的第二个标识符。

我们让数组改变它:

$active_widgets = get_option( 'sidebars_widgets' );

我们还需要一个柜台 (稍后再说):

$counter = 1;

这里是我们如何使用计数器,侧边栏名称和小工具参数 (我们只有一个参数:text) 。

// Add a 'demo' widget to the top sidebar …
$active_widgets[ $sidebars['a'] ][0] = 't5_demo_widget-' . $counter;
// … and write some text into it:
$demo_widget_content[ $counter ] = array ( 'text' => "This works!nnAmazing!" );

$counter++;

请注意如何创建窗口小工具标识符:id_base,减号-和计数器。小工具的内容存储在另一个变量 $demo_widget_content 中。这是计数器,键和数组参数存储在数组中。

当我们完成避免碰撞时,我们增加一个计数器。

那很简单。现在是一个 RSS 小工具。更多的领域,更有趣!

$active_widgets[ $sidebars['a'] ][] = 'rss-' . $counter;
// The latest 15 questions from WordPress Stack Exchange.
$rss_content[ $counter ] = array (
    'title'        => 'WordPress Stack Exchange',
    'url'          => 'http://wordpress.stackexchange.com/feeds',
    'link'         => 'http://wordpress.stackexchange.com/questions',
    'items'        => 15,
    'show_summary' => 0,
    'show_author'  => 1,
    'show_date'    => 1,
);
update_option( 'widget_rss', $rss_content );

$counter++;

这里有一些新的东西:update_option()这将存储 RSS 窗口参数在一个单独的选项。 WordPress 会在以后自动找到。我们没有保存演示小工具参数,因为现在我们将第二个实例添加到第二个侧边栏…

// Okay, now to our second sidebar. We make it short.
$active_widgets[ $sidebars['b'] ][] = 't5_demo_widget-' . $counter;
#$demo_widget_content = get_option( 'widget_t5_demo_widget', array() );
$demo_widget_content[ $counter ] = array ( 'text' => 'The second instance of our amazing demo widget.' );
update_option( 'widget_t5_demo_widget', $demo_widget_content );

… 并立即保存 t5_demo_widget 的所有参数。不需要更新相同的选项两次。

那么现在有足够的小工具呢,我们也来保存 sidebars_widgets

update_option( 'sidebars_widgets', $active_widgets );

现在 WordPress 将知道有一些注册的小工具,每个小工具的参数都存储在哪里。 sidebar_widgets 上的 var_export()将如下所示:

array (
  'wp_inactive_widgets' =>
  array (
  ),
  'top-widget' =>
  array (
    0 => 't5_demo_widget-1',
    1 => 'rss-2',
  ),
  'bottom-widget' =>
  array (
    0 => 't5_demo_widget-3',
  ),
  'array_version' => 3,
)

complete code 再次:

add_action( 'widgets_init', 't5_default_widget_demo' );

function t5_default_widget_demo()
{
    // Register our own widget.
    register_widget( 'T5_Demo_Widget' );

    // Register two sidebars.
    $sidebars = array ( 'a' => 'top-widget', 'b' => 'bottom-widget' );
    foreach ( $sidebars as $sidebar )
    {
        register_sidebar(
            array (
                'name'          => $sidebar,
                'id'            => $sidebar,
                'before_widget' => '',
                'after_widget'  => ''
            )
        );
    }

    // Okay, now the funny part.

    // We don't want to undo user changes, so we look for changes first.
    $active_widgets = get_option( 'sidebars_widgets' );

    if ( ! empty ( $active_widgets[ $sidebars['a'] ] )
        or ! empty ( $active_widgets[ $sidebars['b'] ] )
    )
    {   // Okay, no fun anymore. There is already some content.
        return;
    }

    // The sidebars are empty, let's put something into them.
    // How about a RSS widget and two instances of our demo widget?

    // Note that widgets are numbered. We need a counter:
    $counter = 1;

    // Add a 'demo' widget to the top sidebar …
    $active_widgets[ $sidebars['a'] ][0] = 't5_demo_widget-' . $counter;
    // … and write some text into it:
    $demo_widget_content[ $counter ] = array ( 'text' => "This works!nnAmazing!" );
    #update_option( 'widget_t5_demo_widget', $demo_widget_content );

    $counter++;

    // That was easy. Now a RSS widget. More fields, more fun!
    $active_widgets[ $sidebars['a'] ][] = 'rss-' . $counter;
    // The latest 15 questions from WordPress Stack Exchange.
    $rss_content[ $counter ] = array (
        'title'        => 'WordPress Stack Exchange',
        'url'          => 'http://wordpress.stackexchange.com/feeds',
        'link'         => 'http://wordpress.stackexchange.com/questions',
        'items'        => 15,
        'show_summary' => 0,
        'show_author'  => 1,
        'show_date'    => 1,
    );
    update_option( 'widget_rss', $rss_content );

    $counter++;

    // Okay, now to our second sidebar. We make it short.
    $active_widgets[ $sidebars['b'] ][] = 't5_demo_widget-' . $counter;
    #$demo_widget_content = get_option( 'widget_t5_demo_widget', array() );
    $demo_widget_content[ $counter ] = array ( 'text' => 'The second instance of our amazing demo widget.' );
    update_option( 'widget_t5_demo_widget', $demo_widget_content );

    // Now save the $active_widgets array.
    update_option( 'sidebars_widgets', $active_widgets );
}

如果你现在去 wp-admin/widgets.php,你会看到三个 pre-set 小工具:

就是这样使用 …

dynamic_sidebar( 'top-widget' );
dynamic_sidebar( 'bottom-widget' );

… 打印小工具。

有一个小故障:您必须先加载 front-end 两次才能进行初始注册。如果有人能在这里帮忙,我将非常感激。

次佳解决方案

感谢您分享您的解决方案。我使用了这个问题中描述的内容来创建一个可以很容易地初始化边栏的代码。它非常灵活,您可以创建尽可能多的小工具,而无需修改代码。只需使用过滤器钩子并传递数组中的参数。以下是已注释的代码:

function initialize_sidebars(){

  $sidebars = array();
  // Supply the sidebars you want to initialize in a filter
  $sidebars = apply_filters( 'alter_initialization_sidebars', $sidebars );

  $active_widgets = get_option('sidebars_widgets');

  $args = array(
    'sidebars' => $sidebars,
    'active_widgets' => $active_widgets,
    'update_widget_content' => array(),
  );

  foreach ( $sidebars as $current_sidebar_short_name => $current_sidebar_id ) {

    $args['current_sidebar_short_name'] = $current_sidebar_short_name;
    // we are passing our arguments as a reference, so we can modify their contents
    do_action( 'your_plugin_sidebar_init', array( &$args ) );

  }
  // we only need to update sidebars, if the sidebars are not initialized yet
  // and we also have data to initialize the sidebars with
  if ( ! empty( $args['update_widget_content'] ) ) {

    foreach ( $args['update_widget_content'] as $widget => $widget_occurence ) {

      // the update_widget_content array stores all widget instances of each widget
      update_option( 'widget_' . $widget, $args['update_widget_content'][ $widget ] );

    }
    // after we have updated all the widgets, we update the active_widgets array
    update_option( 'sidebars_widgets', $args['active_widgets'] );

  }

}

这是一个帮助函数,它检查边栏中是否已经有内容:

function check_sidebar_content( $active_widgets, $sidebars, $sidebar_name ) {

  $sidebar_contents = $active_widgets[ $sidebars[ $sidebar_name ] ];

  if ( ! empty( $sidebar_contents ) ) {

    return $sidebar_contents;

  }

  return false;

}

现在我们需要创建一个挂钩到’sidebar_init’ 操作的函数。

add_action( 'your_plugin_sidebar_init', 'add_widgets_to_sidebar' );

function add_widgets_to_sidebar( $args ) {

  extract( $args[0] );

  // We check if the current sidebar already has content and if it does we exit
  $sidebar_element = check_sidebar_content( $active_widgets, $sidebars, $current_sidebar_short_name );

  if ( $sidebar_element !== false  ) {

    return;

  }

  do_action( 'your_plugin_widget_init', array( &$args ) );

}

而现在的 widget 初始化:

add_action( 'your_plugin_widget_init', 'your_plugin_initialize_widgets' );

function your_plugin_initialize_widgets( $args ) {

  extract( $args[0][0] );

  $widgets = array();

  // Here the widgets previously defined in filter functions are initialized,
  // but only those corresponding to the current sidebar
  $widgets = apply_filters( 'alter_initialization_widgets_' . $current_sidebar_short_name, $widgets );

  if ( ! empty( $widgets ) ) {

    do_action( 'create_widgets_for_sidebar', array( &$args ), $widgets );

  }

}

最后一个操作是在每个侧边栏中创建小工具:

add_action( 'create_widgets_for_sidebar', 'your_plugin_create_widgets', 10, 2 );

function your_plugin_create_widgets( $args, $widgets ) {

  extract( $args[0][0][0] );

  foreach ( $widgets as $widget => $widget_content ) {

    // The counter is increased on a widget basis. For instance, if you had three widgets,
    // two of them being the archives widget and one of the being a custom widget, then the
    // correct counter appended to each one of them would be archive-1, archive-2 and custom-1.
    // So the widget counter is not a global counter but one which counts the instances (the
    // widget_occurrence as I have called it) of each widget.
    $counter = count_widget_occurence( $widget, $args[0][0][0]['update_widget_content'] );

    // We add each instance to the active widgets...
    $args[0][0][0]['active_widgets'][ $sidebars[ $current_sidebar_short_name ] ][] = $widget . '-' . $counter;

    // ...and also save the content in another associative array.
    $args[0][0][0]['update_widget_content'][ $widget ][ $counter ] = $widget_content;

  }

}

此功能用于跟踪已定义特定小工具的实例数量:

function count_widget_occurence( $widget, $update_widget_content ) {

  $widget_occurrence = 0;

  // We look at the update_widget_content array which stores each
  // instance of the current widget with the current counter in an
  // associative array. The key of this array is the name of the
  // current widget.
      // Having three archives widgets for instance would look like this:
      // 'update_widget_content'['archives'] => [1][2][3]
  if ( array_key_exists( $widget, $update_widget_content ) ) {

    $widget_counters = array_keys( $update_widget_content[ $widget ] );

    $widget_occurrence = end( $widget_counters );

  }

  $widget_occurrence++;

  return $widget_occurrence;

}

我们最后需要做的是实际分配值。利用这些过滤功能:

add_filter( 'alter_initialization_sidebars', 'current_initialization_sidebars' ) ;
// Use this filter hook to specify which sidebars you want to initialize
function current_initialization_sidebars( $sidebars ) {

  // The sidebars are assigned in this manner.
  // The array key is very important because it is used as a suffix in the initialization function
  // for each sidebar. The value is what is used in the html attributes.
  $sidebars['info'] = 'info-sidebar';

  return $sidebars;

}

和:

add_filter( 'alter_initialization_widgets_info', 'current_info_widgets' );
// Add a filter hook for each sidebar you have. The hook name is derived from
// the array keys passed in the alter_initialization_sidebars filter.
// Each filter has a name of 'alter_initialization_widgets_' and the array
// key appended to it.

function current_info_widgets( $widgets ) {
  // This filter function is used to add widgets to the info sidebar. Add each widget
  // you want to assign to this sidebar to an array.

  return $widgets = array(
    // Use the name of the widget as specified in the call to the WP_Widget constructor
    // as the array key.

    // The archives widget is a widget which is shipped with wordpress by default.
    // The arguments used by this widget, as all other default widgets, can be found
    // in wp-includes/default-widgets.php.

    'archives' => array(
      // Pass in the array options as an array
      'title' => 'Old Content',
      'dropdown' => 'on',
      // The 'on' value is arbitrarily chosen, the widget actually only checks for
      // a non-empty value on both of these options
      'count' => 'on',
    ),
 );

}

理想情况下,您可以在一个安装程序中调用 initialize_sidebars,该函数调用插件或主题激活,如下所示:主题激活:

add_action( 'after_switch_theme', 'my_activation_function' );
function my_activation_function() {
  initialize_sidebars();
}

插件激活:

register_activation_hook( __FILE__, 'my_activation_function' );
function my_activation_function() {
  initialize_sidebars();
}

总结这个功能集团的用法:

  1. 创建一个初始化挂钩到’alter_initialization_sidebars’ 过滤器的侧边栏的功能。
  2. 为刚刚添加的每个侧边栏创建一个附加到 「alter_initialization_widgets_ $ sidebarname」 过滤器的函数。将 $ sidebarname 替换为您在步骤 1 中创建的每个侧边栏的名称。

您也可以将此未注释的代码简单地复制到函数文件中,并立即开始创建过滤器函数:Code on pastie (without initialization filter functions)

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。