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 am trying to mimic the dashboard meta boxes UI in my WP plugin - the small metaboxes. I have already styled them and also the drag & drop feature works as I loaded the following script and style:

wp_enqueue_style('dashboard');
wp_enqueue_script('dashboard');

However I also would like to save my custom "postboxes" position (order) and also the open/close state.

Any ideas how could this be achieved?

Thanks.

UPDATE: This is the structure of my postbox:

<div class="wrap">
    <h2><?php echo get_admin_page_title(); ?></h2>
    <div class="postbox-container" style="width: 100%">
        <div class="metabox-holder">
            <div class="meta-box-sortables">
                <div class="postbox" id="first">
                    <div class="handlediv" title="Click to toggle"><br /></div>
                    <h3 class="hndle"><span><?php echo get_admin_page_title(); ?></span></h3>
                    <div class="inside">
                        <p>first</p>
                    </div>
                </div>
                <div class="postbox" id="second">
                    <div class="handlediv" title="Click to toggle"><br /></div>
                    <h3 class="hndle"><span><?php echo get_admin_page_title(); ?></span></h3>
                    <div class="inside">
                        <p>second</p>
                    </div>
                </div>

            </div>
        </div>
    </div>

    <form style="display:none" method="get" action="">
        <?php wp_nonce_field('closedpostboxes', 'closedpostboxesnonce', false ); ?>
        <?php wp_nonce_field('meta-box-order', 'meta-box-order-nonce', false ); ?>
    </form>
</div>
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

When ordering or closing metaboxes, those actions require nonces, add the following to your code and see if that resolves the problem.

<?php wp_nonce_field('closedpostboxes', 'closedpostboxesnonce', false ); ?>
<?php wp_nonce_field('meta-box-order', 'meta-box-order-nonce', false ); ?>

Additional:

You should add metaboxes to your page using add_meta_box and output them using do_meta_boxes passing in the hook for your plugin page as the first arg. This should at least ensure you have registered and output them in the same way WordPress does, and providing you've also output the appropriate nonces, should work.

If something still isn't working, view source on your problem page(s) and verify the JS files are being output in the correct order(easier still, compare JS inclusions in your page with a core WordPress page that uses metaboxes, make sure your JS includes(enqueues) appear in the same order).

share|improve this answer
    
I tried to add that, but nothing changed. Probably I am missing something else too.I also added wp_enqueue_script('postbox'); as this is probably the ajax script that is solving this saving. I am going to paste there my structure of these boxes below. –  depi Mar 25 '11 at 11:44
    
You need to call do_meta_boxes as Scribu pointed out. –  t31os Mar 25 '11 at 12:32
    
Great I got it working now, I was missing the postboxes.add_postbox_toggles( 'mypagename' ); JS code which I find in scribu's plugin, thank you! –  depi Mar 26 '11 at 12:32
    
Ah yes, totally forgot about adding the post box toggles JS(been ages since i used meta boxes in a plugin - i really should do it again just as a refresher). –  t31os Mar 26 '11 at 12:43
    
No problem :) However one weird thing is if I use input radio button in my metabox and drag it then it losts it's checked focus. When I replace it to checkbox or any other field it works well. –  depi Mar 26 '11 at 14:08
show 2 more comments

It probably won't work if you write the HTML by hand.

Better to use do_metaboxes().

I've got it all figured out in my scbBoxesPage class:

http://plugins.trac.wordpress.org/browser/scb-framework/trunk/scb/BoxesPage.php?rev=339808

Example usage in the Front-end Editor plugin:

http://plugins.trac.wordpress.org/browser/front-end-editor/trunk/admin.php?rev=362968

I should probably write a proper example plugin that uses all those classes.

share|improve this answer
add comment

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.