Quick and Dirty Widget Testing

Testing widgets with your WordPress theme would be so much faster if you could enable all widgets at once, instead of dragging them one by one.

Here are two small functions to help with widget testing. The first takes all “Inactive Widgets” and adds them to the first registered sidebar in the theme. The second removes all widgets, leaving the widget area empty.

To use, add the code into your theme’s functions.php and uncomment the add_action() calls to trigger the functions. Happy testing!

<?php

/**
 * Quick and dirty inactive widget loading
 * Loads all inactive widgets into first registered sidebar
 *  
 * @global array $wp_registered_sidebars
 */

// To use: uncomment the add_filter call below, then refresh your admin Widgets page
// add_action( 'in_widget_form', 'enable_inactive_widgets' );

function enable_inactive_widgets() {

	// get first registered sidebar
	global $wp_registered_sidebars;
	$first_sidebar_id = array_shift( array_keys( $wp_registered_sidebars ) );

	// get widgets
	$widgets = get_option( 'sidebars_widgets' );

	// if inactive widgets exist, add them to the first sidebar
	if ( isset( $widgets['wp_inactive_widgets'] ) && '' != $widgets['wp_inactive_widgets'] ) {
		$inactive_widgets = array(
			$first_sidebar_id => $widgets['wp_inactive_widgets']
		);
		update_option( 'sidebars_widgets', $inactive_widgets );
	}
}

/**
 * Quick and dirty widget removal
 * This will remove both active and inactive widgets
 *  
 */

// To use: uncomment the add_action call below, then refresh your admin Widgets page
// add_action( 'in_widget_form', 'remove_all_widgets' );

function remove_all_widgets() {
	update_option( 'sidebars_widgets', null );
}

?>

5 thoughts on “Quick and Dirty Widget Testing

  1. Hi, Ian,

    First, a general “Thanks and I’ve been receiving the newsletter for quite a while now” to you. My own experimentation has subsided, but will be rekindled soon.

    Second, should line 11 be an add_filter() call?

    Cheers,

    Daniel

  2. Pingback: Notable Tech Posts – 2010.05.09 | The Life of Lew Ayotte

  3. Found this awesome snippet and it’s definitely something I’ll be using. Could this be modified to load specific widgets in different sidebars when they user hasn’t customized any yet?

    Thanks again for the short tutorial. Valuable info!

    • @James — You could use this same script to load all inactive widgets to a specific sidebar (by specifying the ID of the sidebar instead of using the first one), but not to load specific widgets to different sidebars.

  4. Pingback: You Should Follow ThemeShaper — simpledream is Lance B. Willett

Comments are closed.