To solve this, my research led me to:
- Programmatically add widgets to sidebars
- How to use
control_callback
when creating a widget via functions.php or plugin?
The following is just a proof of concept based on the always excellent code of @Bainternet (in the Q about control_callback
).
The Dashboard Widget displays the title of a selected Text Widget it displays "Content of..." but should be "Title of..."
Its configuration screen displays a dropdown with all Text Widgets of all Sidebars:
Here's the Appearance > Widgets screen
Now, what's left to implement is an editing interface (text input and so on) and save the proper values inside the option widget_text
, using:
update_option('widget_text', $a_VERY_well_structured_array_otherwise_things_will_break);
.
<?php
/*
Plugin Name: Dashboard Widget to deal with Text Widgets
Plugin URI: http://stackoverflow.com/q/14898302/1287812
Description: based on Bainternet plugin https://wordpress.stackexchange.com/q/77830/12615
Version: 0.1
Author: brasofilo
Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo
*/
// Register widget
add_action( 'wp_dashboard_setup', 'add_dashboard_widget_wpse_77830' );
function add_dashboard_widget_wpse_77830()
{
wp_add_dashboard_widget(
'dashboard_widget_wpse_77830',
'Text Widgets',
'dashboard_widget_wpse_77830',
'dashboard_widget_wpse_77830_handle'
);
}
// Show widget
function dashboard_widget_wpse_77830()
{
//get saved data
if ( !$widget_options = get_option( 'my_dashboard_widget_options' ) )
$widget_options = array();
$saved_txt_widget = isset( $widget_options['txt_widget'] ) ? $widget_options['txt_widget'] : '';
echo "
<p><strong>Content of the Widget</strong></p>
<div class='txt_widget_class_wrap'>
<label style='background:#ccc;'> {$saved_txt_widget}</label>
</div>
";
}
// Configure and update widget
function dashboard_widget_wpse_77830_handle()
{
// Get saved data
if ( !$widget_options = get_option( 'my_dashboard_widget_options' ) )
$widget_options = array();
// Process update
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['my_dashboard_widget_options']) )
{
// Minor validation
$widget_options['txt_widget'] = wp_kses( $_POST['my_dashboard_widget_options']['txt_widget'], array() );
// Save update
update_option( 'my_dashboard_widget_options', $widget_options );
}
// Set defaults
if( !isset( $widget_options['txt_widget'] ) )
$widget_options['txt_widget'] = ''; //you can set the default
// Get Widget Text
$txt = get_option( 'widget_text' );
// Not necessary in the array
unset($txt['_multiwidget']);
// Start HTML
echo "
<p><strong>Available Text Widgets</strong></p>
<div class='txt_widget_class_wrap'>
<label>Title</label>
<select name='my_dashboard_widget_options[txt_widget]' id='txt_widget'>";
// Print options
foreach( $txt as $t )
{
printf(
'<option value="%s" %s>%s</option>',
$t['title'],
selected( $widget_options['txt_widget'], $t['title'], false ),
$t['title']
);
}
// End HTML
echo "
</select>
</div>
";
}