Limit the number of inactive widgets

Tested under v3.2.1: $sidebars = wp_get_sidebars_widgets(); if(count($sidebars[‘wp_inactive_widgets’]) > 10){ $new_inactive = array_slice($sidebars[‘wp_inactive_widgets’],-10,10); // remove the dead widget options $dead_inactive = array_slice($sidebars[‘wp_inactive_widgets’],0,count($sidebars[‘wp_inactive_widgets’])-10); foreach($dead_inactive as $dead){ $pos = strpos($dead,’-‘); $widget_name = substr($dead,0,$pos); $widget_number = substr($dead,$pos+1); $option = get_option(‘widget_’.$widget_name); unset($option[$widget_number]); update_option(‘widget_’.$widget_name,$option); } // save our new widget setup $sidebars[‘wp_inactive_widgets’] = $new_inactive; wp_set_sidebars_widgets($sidebars); } The above code limits the … Read more

What is the difference between wp_register_sidebar_widget and register_widget?

wp_register_sidebar_widget() is part of the old widgets API. Sidebar widgets used to be built procedurally … in a non-reusable fashion (i.e. you could only ever have one of each). register_widget() was introduced with the new Widgets API and takes an object/class as an input rather than actual widget parameters. WordPress can instantiate as many copies … Read more

Adding a div to wrap widget content after the widget title

In addition to Toscho’s answer here’s what you need for a robust solution: // if no title then add widget content wrapper to before widget add_filter( ‘dynamic_sidebar_params’, ‘check_sidebar_params’ ); function check_sidebar_params( $params ) { global $wp_registered_widgets; $settings_getter = $wp_registered_widgets[ $params[0][‘widget_id’] ][‘callback’][0]; $settings = $settings_getter->get_settings(); $settings = $settings[ $params[1][‘number’] ]; if ( $params[0][ ‘after_widget’ ] == … Read more

Add class to before_widget from within a custom widget

Aha, so the $before_widget variable is a string representing div element: <div class=”widget my” id=”my-widget-1″> . So I checked the $before_widget for the “class” sub-string and added my $widget_width value to it. The code is from my custom widget file: function widget( $args, $instance ) { extract( $args ); … //other code $widget_width = !empty($instance[‘widget_width’]) … Read more

How to store widget fields data as an array?

You have to collect multiple fields under the same name like this … name=”collect[1]” name=”collect[2]” … and adjust your widget logic to this. Here is a very simple demo widget: <?php # -*- coding: utf-8 -*- /* Plugin Name: Store Options as array */ add_action( ‘widgets_init’, array ( ‘T5_Array_Options_Widget’, ‘register’ ) ); class T5_Array_Options_Widget extends … Read more

Add a custom walker to a menu created in a widget

If you look at implementation of WP_Nav_Menu_Widget class you will see the following code: function widget($args, $instance) { // Get menu $nav_menu = ! empty( $instance[‘nav_menu’] ) ? wp_get_nav_menu_object( $instance[‘nav_menu’] ) : false; if ( !$nav_menu ) return; $instance[‘title’] = apply_filters( ‘widget_title’, empty( $instance[‘title’] ) ? ” : $instance[‘title’], $instance, $this->id_base ); echo $args[‘before_widget’]; if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)