How to automatically activate WordPress widgets? (New Widgets_API WP_Widget Class)

I finally refused.

I am developing something in WordPress 2.8.4. I am impressed with the ease of the new widget API, which allows you to use WP_Widget extensions and create widgets that are easily accessible with multiple instances. But I ran into a problem.

How can I automatically activate a widget when activating a theme? I tried to use:

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

But without success. the problem is that the New wordpress API creates widget identifiers and automatically adds a unique identifier to the end of the identifier. So I just can’t figure it out. I tried the above solution, but the actual widget id, while looking at the source code, always displays uc_tagcloud-2 or 3 or 4..etc etc. A new instance each time a widget is added.

I would be grateful for any thought, I thought deeply about it and searched the Internet for hours. So this is my last chance.

Basically, I don't want users to drag and drop them manually.

, . , . , .. : , (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 . , . ?

+3
1

mirror , , , WordPress MU ( Multisite) /. mu-plugin functions.php.

Q & A WordPress 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 );
}
+1

Source: https://habr.com/ru/post/1716414/


All Articles