Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress function to get raw widget data for all of the widgets in a given sidebar
<?php
function rh_get_widget_data_for_all_sidebars() {
global $wp_registered_sidebars;
$output = array();
foreach ( $wp_registered_sidebars as $sidebar ) {
if ( empty( $sidebar['name'] ) ) {
continue;
}
$sidebar_name = $sidebar['name'];
$output[ $sidebar_name ] = rh_get_widget_data_for( $sidebar_name );
}
return $output;
}
<?php
function rh_get_widget_data_for( $sidebar_name ) {
global $wp_registered_sidebars, $wp_registered_widgets;
// Holds the final data to return
$output = array();
// Loop over all of the registered sidebars looking for the one with the same name as $sidebar_name
$sidebar_id = false;
foreach ( $wp_registered_sidebars as $sidebar ) {
if ( $sidebar['name'] == $sidebar_name ) {
// We now have the Sidebar ID, we can stop our loop and continue.
$sidebar_id = $sidebar['id'];
break;
}
}
if ( ! $sidebar_id ) {
// There is no sidebar registered with the name provided.
return $output;
}
// A nested array in the format $sidebar_id => array( 'widget_id-1', 'widget_id-2' ... );
$sidebars_widgets = wp_get_sidebars_widgets();
$widget_ids = $sidebars_widgets[ $sidebar_id ];
if ( ! $widget_ids ) {
// Without proper widget_ids we can't continue.
return array();
}
// Loop over each widget_id so we can fetch the data out of the wp_options table.
foreach ( $widget_ids as $id ) {
// The name of the option in the database is the name of the widget class.
$option_name = $wp_registered_widgets[ $id ]['callback'][0]->option_name;
// Widget data is stored as an associative array. To get the right data we need to get the right key which is stored in $wp_registered_widgets
$key = $wp_registered_widgets[ $id ]['params'][0]['number'];
$widget_data = get_option( $option_name );
// Add the widget data on to the end of the output array.
$output[] = (object) $widget_data[ $key ];
}
return $output;
}
@joq3

This comment has been minimized.

Copy link

@joq3 joq3 commented May 14, 2018

Hi, found this function, and I use this to detect changes made in widgets, if a new widget is added or one is removed, or if a setting inside the widget has changed. And it works great for this. But would it be possible to rewrite this to include all sidebars instead of the specified one? Or maybe make it possible to list more than one sidebar in this function?

@kingkool68

This comment has been minimized.

Copy link
Owner Author

@kingkool68 kingkool68 commented May 14, 2018

@joq3 I updated the gist to include a function to get widget data for all of the registered sidebars. It returns an array keyed by the name of the sidebar.

@wp-kitten

This comment has been minimized.

Copy link

@wp-kitten wp-kitten commented Aug 16, 2018

you rock! 👍

@wp-kitten

This comment has been minimized.

Copy link

@wp-kitten wp-kitten commented Aug 16, 2018

One typo though, in rh_get_widget_data_for():

$sibebar_id = false;

should be
$sidebar_id = false;

@afmarchetti

This comment has been minimized.

Copy link

@afmarchetti afmarchetti commented Sep 11, 2018

Cool! unfortunately the page with the widgets hasn't the customizer (with the widgets) available, this is because the widget are display with the function.. there is a way to work around this?

Many thanks man!
;-)

@EdisonSM

This comment has been minimized.

Copy link

@EdisonSM EdisonSM commented Jun 18, 2019

Great functions! Saved me some time in a tight schedule. Thanks for sharing.

@kingkool68

This comment has been minimized.

Copy link
Owner Author

@kingkool68 kingkool68 commented Jun 18, 2019

@EdisonSM Thanks for leaving a comment!

@RomanStone

This comment has been minimized.

Copy link

@RomanStone RomanStone commented Sep 19, 2019

+1, Working

@mehmet-demir

This comment has been minimized.

Copy link

@mehmet-demir mehmet-demir commented Feb 17, 2020

Could someone explain how to use it pls? I am getting an empty array :(

@kingkool68

This comment has been minimized.

Copy link
Owner Author

@kingkool68 kingkool68 commented Feb 19, 2020

@mehmet-demir When you register a sidebar, you give it a name like Footer. See https://developer.wordpress.org/reference/functions/register_sidebar/

To get all of the raw widget data for the Footer sidebar, do rh_get_widget_data_for( 'Footer' ); which will give you an array of widget data.

@jamesfacts

This comment has been minimized.

Copy link

@jamesfacts jamesfacts commented Sep 24, 2020

Thanks so much for this! I do see a tiny typo: https://gist.github.com/kingkool68/3418186#file-rh-get-widget-data-for-php-L9

Probably should be $sidebar_id = false;

@kingkool68

This comment has been minimized.

Copy link
Owner Author

@kingkool68 kingkool68 commented Sep 24, 2020

@jamesfacts You're welcome. Thanks for spotting the typo. You rock!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment