Home » Wordpress » How to auto activate WordPress widgets? (New Widgets_API WP_Widget class)

How to auto activate WordPress widgets? (New Widgets_API WP_Widget class)

Posted by: admin November 30, 2017 Leave a comment

Questions:

I gave up on this finally.

I am developing something on WordPress 2.8.4. I am impressed by the ease of new widgets API which allows you to use extends WP_Widget and create widgets that have multiple instances easily. But I am facing a problem.

How can I auto-activate the widget on theme activation? I’ve tried to use:

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

But with no success. the problem is that New wordpress API creates widget ids and adds a unique id to the end of ID automatically. So I just can’t get the hang of it. I’ve tried the above solution but the actual widget ID by viewing source code always displays uc_tagcloud-2 or 3 or 4..etc etc.. a new instance every time i add the widget.

I would appreciate any thought, I’ve deeply thought about it and searched the internet for hours now. So this is my last chance.

I basically do not want users to drag and enable them manually.

Below is a sample widget I’ve developed. It works fine if i drag and put it in relevant sidebar. But I do not know how .. my question is : how can I activate it automatically into specific sidebar without draggin it manually (WP_Widget new Widgets API)

This is code of widget:

<?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');
}
?>

It’s very simple, the new API i love it. But I just can’t get how to auto activate an instance of the widget on specific sidebar. Any help?

Answers:

There’s a blog post mirror as old as this Question that may have a solution, but it is designed to WordPress MU (now Multisite) and should be updated/adapted accordingly. It is a mix of mu-plugin and extra code in the theme’s functions.php.

But based in this Q&A in WordPress Answers and this other blog post no mirror yet, the following works for WordPress 3.3 or superior. It runs on theme activation and is tested with WP 3.4.2 and applied on TwentyEleven’s 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 );
}