Prevent second widget activation

Is there a way to “remove the widget from available widgets” after you’ve activated it once on some area?
The idea is to allow user to activate only one instance of it.

Related posts

Leave a Reply

3 comments

  1. You can create a one-time-use widget with the following example found here: wp_register_sidebar_widget

    The following code will create a widget called “Your Widget” which will become available in the WordPress Administrative Panels. The widget can then be dragged to an available sidebar for display.

    Note that this widget can only be used once in exactly 1 of the
    sidebars. For recursive widgets (widgets you can add to multiple times
    and add to multiple sidebars) please see the Register Widget function.

    function your_widget_display($args) {
       extract($args);
       echo $before_widget;
       echo $before_title . 'My Unique Widget' . $after_title;
       echo $after_widget;
       // print some HTML for the widget to display here
       echo "Your Widget Test";
    }
    
    wp_register_sidebar_widget(
        'your_widget_1',        // your unique widget id
        'Your Widget',          // widget name
        'your_widget_display',  // callback function
        array(                  // options
            'description' => 'Description of what your widget does'
        )
    );
    
  2. You can use JQuery to remove it from “avilable widgets” panel if it has been used once.

      $( document ).ready( function() {
         $( '#widgets-right input[value="widget-id_base"]').each( function () {
           $( '#widgets-left .widget:has(input[value="widget-id_base"])' ).css( 'display', 'none' );
         } ); 
      } );
    
  3. That is not an really good Idea. Because Themes wich I develope are more complex, so that you have different Sidebars through the different Views.

    For Example:
    Standard-Sidebar: Displays next to the Content
    Shop-Sidebar: Displays next to the Shop-Overview

    So and now I want to place your Widget into both Sidebars, because there is no view, where both sidebars are visible at the same time.

    What you search for, is discard the Placement of your widget into an sidebar, where your Widget is still existing, right?