How can a widget which of which only one instance can be used?


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.

  • Easy seems good enough. – Mild Fuzz Sep 20 '10 at 17:37
  • another way might be to set the _multiwidget option to 0. Like $settings = get_option("widget_text"); $settings['_multiwidget'] = 0; update_option("widget_text", $settings); – onetrickpony Dec 19 '10 at 11:46

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'];
    'Single Instance Widget',
    array( 'classname' => 'mfields-test-single-instance-widget' )

you can create widgets that can activated once with the old method:

// Custom Widget
function widget_artdev_custom() { ?>


<?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.

  • register_sidebar_widget is deprecated since version 2.8! Use wp_register_sidebar_widget() instead. – mfields Mar 20 '11 at 14:09
  • you are correct @mfields, i have edit the answer! – Philip Mar 31 '11 at 19:27

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);
            update_option('wpse_1828_widget', $data);

    function deactivate()

    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>

        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'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.