Dashboard like meta boxes in my plugin – how to save their position and open/closed state?

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.

Read More

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>

Related posts

Leave a Reply

2 comments

  1. 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).

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