【问题标题】:How to auto activate WordPress widgets? (New Widgets_API WP_Widget class)如何自动激活 WordPress 小部件? (新的 Widgets_API WP_Widget 类)
【发布时间】:2010-11-24 02:24:18
【问题描述】:

我终于放弃了。

我正在 WordPress 2.8.4 上开发一些东西。新的小部件 API 的易用性给我留下了深刻的印象,它允许您使用扩展 WP_Widget 并轻松创建具有多个实例的小部件。但是我遇到了一个问题。

如何在主题激活时自动激活小部件?我试过用:

add_action('sidebars_widgets', array('sidebar-1', array('uc_tagcloud')));

但没有成功。问题是 New wordpress API 创建小部件 ID 并自动将唯一 ID 添加到 ID 末尾。所以我只是无法掌握它。我已经尝试了上述解决方案,但是通过查看源代码的实际小部件 ID 总是显示 uc_tagcloud-2 或 3 或 4..etc 等。每次添加小部件时都会出现一个新实例。

如果有任何想法,我将不胜感激,我已经深思熟虑并在互联网上搜索了几个小时。所以这是我最后的机会了。

我基本上不希望用户手动拖动和启用它们。

以下是我开发的示例小部件。如果我将其拖动并放在相关的侧边栏中,它工作正常。但我不知道怎么......我的问题是:如何在不手动拖动的情况下将它自动激活到特定的侧边栏(WP_Widget new Widgets API)

这是小部件的代码:

<?php
/**********************************************************************
Widget: Tag Cloud List
**********************************************************************/
class uc_tagcloud extends WP_Widget {

 // Constructor
 function uc_tagcloud() {
  $widget_ops = array('description' => __('A list of your blog tags for your sidebar','ucms'));
  $this->WP_Widget('uc_tagcloud', __('ultimaCMS - Tag Cloud','ucms'), $widget_ops);
 }

 // Display Widget
 function widget($args, $instance) {
  extract($args);
  $title = esc_attr($instance['title']);
  $num = intval($instance['num']);
  echo $before_widget.$before_title.$title.$after_title;

  // Display widget content
  ?>
  <?php wp_tag_cloud('smallest=9&largest=22&number='.$num); ?>
  <?php

  echo $after_widget;
 }

 // When Widget Control Form Is Posted
 function update($new_instance, $old_instance) {
  if (!isset($new_instance['submit'])) {
   return false;
  }
  $instance = $old_instance;
  $instance['title'] = strip_tags($new_instance['title']);
  $instance['num'] = intval($new_instance['num']);
  return $instance;
 }

 // DIsplay Widget Control Form
 function form($instance) {
  global $wpdb;
  $instance = wp_parse_args((array) $instance, array('title' => __('Tag Cloud','ucms'), 'num' => 100));
  $title = esc_attr($instance['title']);
  $num = intval($instance['num']);
?>

<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','ucms'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('num'); ?>"><?php _e('Number of tags:','ucms'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('num'); ?>" name="<?php echo $this->get_field_name('num'); ?>" type="text" value="<?php echo $num; ?>" />
<br /><small>Enter 0 to display all tags</small>
</p>

<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />

<?php
 }
}

### Initiate widget
add_action('widgets_init', 'uc_tagcloud_init');
function uc_tagcloud_init() {
 register_widget('uc_tagcloud');
}
?>

它非常简单,我喜欢它的新 API。但我只是不知道如何在特定侧边栏上自动激活小部件的实例。有什么帮助吗?

【问题讨论】:

  • 我认为这是不可能的。在 Wordpress MU WP 中强制网络管理员激活插件,在 WP 上似乎是一样的。事实上,我更愿意在放入我的网站之前激活和配置我的小部件。

标签: php wordpress class widget


【解决方案1】:

有一个 blog post mirror 可能有一个解决方案,但它是为 WordPress MU(现在是多站点)设计的,应该相应地更新/调整。它是mu-plugin 和主题functions.php 中的额外代码的组合。

但基于 WordPress Answers 中的 this Q&A 和其他 blog post 还没有镜像,以下适用于 WordPress 3.3 或更高版本。它在主题激活时运行,并使用 WP 3.4.2 进行了测试,并应用于 TwentyEleven 的functions.php

add_action( 'after_switch_theme', 'so_1353147_activate_theme', 10 , 2 );

function so_1353147_activate_theme( $oldname, $oldtheme = false ) 
{
    $sidebar_id = 'sidebar-5';
    $sidebars_widgets = get_option( 'sidebars_widgets' );
    $id = count( $sidebars_widgets ) + 1;
    $sidebars_widgets[$sidebar_id] = array( "text-" . $id );

    $ops = get_option( 'widget_text' );
    $ops[$id] = array(
        'title' => 'Automatic Widget',
        'text' => 'Lorem ipsum lorem', 
    );
    update_option( 'widget_text', $ops ); 
    update_option( 'sidebars_widgets', $sidebars_widgets );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2023-01-24
    • 1970-01-01
    • 2015-09-06
    • 2019-05-21
    • 2019-11-01
    相关资源
    最近更新 更多