the wordpress admin already uses some kind of toggle on the menu and on metaboxes.. w/ "Click to Toggle" buttons that remember the open versus closed state. Does anybody know what code is responible for this? I'd like to implement it into sections of my custom metaboxs, and it doesn't seem like I should need to add a script to the admin area (b/c it is already slow) when WP has this functionality by default.
3 Answers
Checkout the HTML for a WP meta-box :
<div id="my_div" class="postbox ">
<div class="handlediv" title="Click to toggle."><br></div>
<h3 class="hndle"><span>title</span></h3>
<div class="inside">
...
</div>
</div>
If you follow that structure (and CSS classes) in your custom meta-box, the toggle will work.
-
awesome. was clearly over-complicating. borrow their markup and it works automagically. thanks! Commented Jun 28, 2011 at 15:42
-
do i have to give everything a unique ID? i tried going w/o an id, and it stops working when i have multiple toggles Commented Jun 28, 2011 at 15:54
-
also- doesn't seem to "remember" the open/closed status Commented Jun 28, 2011 at 16:01
-
That's bundled together with the current user data (else nothing will be remembered), which you can inspect with my (hardly outdated) "Current User Deamon" Plugin. That's the fields:
// User Meta Arrays:
$metaboxhidden_dashboard = $current_user->metaboxhidden_dashboard;
$capabilities = $current_user->{ $wpdb->prefix . 'capabilities' };
$closedpostboxes_page = $current_user->closedpostboxes_page;
$metaboxhidden_page = $current_user->metaboxhidden_page;
$autosave_draft_ids = $current_user->{ $wpdb->prefix . 'autosave_draft_ids' };
$closedpostboxes_post = $current_user->closedpostboxes_post;
$metaboxhidden_post = $current_user->metaboxhidden_post;
$metaboxorder_dashboard = $current_user->{ $wpdb->prefix . 'metaboxorder_dashboard' };
-
so if by default it will only remember these particular data i have to use a toggle plugin if i want toggle to work inside my metaboxes? Commented Jun 28, 2011 at 16:04
-
You can use plain simple jQuery toggle function as JQuery already is loaded in admin. Take a look at the core code if you want your status to be remembered.– kaiserCommented Jun 28, 2011 at 16:18
-
i ended up using plain toggle plus cookies. i'll have to look more closely at core to see if i can eliminate loading another plugin. Commented Jul 6, 2011 at 15:24
Try this mark-up with nothing else:
<div class="postbox">
<div title="Click to toggle" class="handlediv"><br></div>
<h3 class="hndle">HEADLINE/META BOX TITLE</h3>
<div class="inside">YOUR CONTENT</div>
</div><!-- // postbox -->
-
works great while you are on the page. but it doesn't 'remember' the open/closed status from your last visit the way the default metaboxes do. i think b/c as @kaiser says in his response, it only remembers certain meta data Commented Jul 9, 2011 at 3:19
-
The best and easiest way still is using
add_meta_box()
... or take a look into core where the closed boxes get set and add it to your script.– kaiserCommented Jul 10, 2011 at 16:21