How can a widget which of which only one instance can be used?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Simple way would be to set some global variable on first widget run and check for it. Output nothing or informational message if it is already set.
Proper way would probably be to work with interface and remove widget from available when you add it to sidebar, but that is way out of my league.
Method 2
I put together the following code based on @Philip’s answer. Seems to work for me. Any suggestions are welcome!
function mfields_test_single_instance_widget( $args ) {
$args = wp_parse_args( (array) $args, array(
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
print $args['before_title'] . esc_html( $args['widget_name'] ) . $args['after_title'];
print $args['before_widget'] . '<p>THERE CAN BE ONLY ONE!!!</p>' . $args['after_widget'];
}
wp_register_sidebar_widget(
'mfields-test-single-instance-widget',
'Single Instance Widget',
'mfields_test_single_instance_widget',
array( 'classname' => 'mfields-test-single-instance-widget' )
);
Method 3
you can create widgets that can activated once with the old method:
<?php
// Custom Widget
function widget_artdev_custom() { ?>
// YOUR CODE-FUNCTIONS HERE
<?php }
if ( function_exists('register_sidebar_widget') )
register_sidebar_widget(__('Custom Widget','artdev'), 'widget_artdev_custom');
?>
have in mind that this code can be used for >2.8 and older versions of wordpress.
Method 4
Previously, I had provided a link to a tutorial, the following is a full working example. @mfields example works just fine, mine simply has the options fields present.
/*
* Single Instance Widget with Options
*/
add_action("widgets_init", array('Single_instance_widget', 'register'));
register_activation_hook(__FILE__, array('Single_instance_widget', 'activate'));
register_deactivation_hook(__FILE__, array('Single_instance_widget', 'deactivate'));
class Single_instance_widget
{
function activate()
{
$data = array(
'option1' => 'Default value',
'option2' => 55
);
if (!get_option('wpse_1828_widget'))
{
add_option('wpse_1828_widget', $data);
}
else
{
update_option('wpse_1828_widget', $data);
}
}
function deactivate()
{
delete_option('wpse_1828_widget');
}
function control()
{
$data = get_option('wpse_1828_widget');
echo <<<HTML
<p><label>Option 1<input name="wpse_1828_widget_option1"
type="text" value="{$data['option1']}"/></label></p>
<p><label>Option 2<input name="wpse_1828_widget_option2"
type="text" value="{$data['option2']}"/></label></p>
HTML;
if (isset($_POST['wpse_1828_widget_option1']))
{
$data['option1'] = attribute_escape($_POST['wpse_1828_widget_option1']);
$data['option2'] = attribute_escape($_POST['wpse_1828_widget_option2']);
update_option('wpse_1828_widget', $data);
}
}
function widget($args)
{
echo $args['before_widget'];
echo $args['before_title'] . 'Your widget title' . $args['after_title'];
echo 'I am your widget';
echo $args['after_widget'];
}
function register()
{
wp_register_sidebar_widget('wpse_1828_widget_id', 'Single Instance Widget', array('Single_instance_widget', 'widget'));
wp_register_widget_control('wpse_1828_widget_id', 'Single Instance Widget', array('Single_instance_widget', 'control'));
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0