Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a plugin that allows the user to create "page/post types", customize the type style, then tag a page/post with that type. All of this works. My problem is this plugin also needs to allow them to attach widgets to said type.

I have no way of knowing how many "types" there are at any time.

The plugin works like this:

  • User creates types (cat, dog, mouse)
  • User specifies things like background colors, link colors, etc. for a type
  • User specifies widgets for that type

When the page/post is viewed the page detects if it has a type. If it does it pulls in all the relavent data to that type.

Everything works PERFECT.

I know I have to register a sidebar but I can't registe for each type since I have no idea how many there are and the user can create and delete at their whim.

My plan:

  • Register a sidebar called "custom sidebar"
  • Create a page that mimics the "Widgets" page
  • Allow the user to add widgets there on the "custom sidebar"
  • The plugin stores data for the sidebar based on the "type"'s id

I can't really use another plugin since the sidebar function has to meld right into my plugin. I'm not even sure this will work or even where to start. Any push in the right direction would be great!

share|improve this question
6  
Your 'types' sound like page templates, only your version is customizable by the user, is that correct? Reinventing the wheel (the widgets page) does not seem to be the right way to go. Maybe you could register a sidebar when the page type is created? It might help if you showed some code for that. –  vancoder Apr 19 '13 at 17:23

2 Answers 2

  1. Create a sidebar $type . '_sidebar'
  2. Register the sidebar $type . '_sidebar' (e.g dog_sidebar)
  3. Pass the type as GET argument to the widget.php (create some menu entries or something else where you link to e.g. wp-admin/widgets.php?sidebar_type=dog_sidebar)
  4. Filter out every sidebar that should not be displayed. You can use this function:

      global $wp_registered_sidebars;
    
      $type =filter_input( INPUT_GET, 'sidebar_type', FILTER_SANITIZE_STRIPPED );
    
      if ( empty( $type ) )
        return;
    
      if ( isset( $wp_registered_sidebars[ $type ] ) ) {
        $keep = array( $type => $wp_registered_sidebars[ $type ] );
        $wp_registered_sidebars = array_intersect_key( $keep, $wp_registered_sidebars );
      }
    
    }
    
    add_action( 'sidebar_admin_setup', 'filter_sidebars', 1, 0 );
    

For example in Twenty Twelve there are three registered sidebars (the sidebar IDs): sidebar-1, sidebar-2 and sidebar-3. If you now go to your-blogdoma.in/wp-admin/widgets.php?sidebar_type=sidebar-3, there is only the third sidebar left (with ID sidebar-3).

If you have registered a sidebar dog_sidebar and visit your-blogdoma.in/wp-admin/widgets.php?sidebar_type=dog_sidebar, the only visible sidebar is the 'Dog Sidebar' (with ID dog_sidebar).

The plugin stores data for the sidebar based on the "type"'s id

There is no need to do this. Every sidebar stores it's own data. But maybe I misunderstood what you attempting with "stores data for the sidebar".

share|improve this answer
up vote 0 down vote accepted

I ended up just creating a sidebar and any item that was placed there, a dropdown box was added to assign it a special location.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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