Admin page metaboxes

Render collapsable and moveable metaboxes on an admin page or custom post type.

add_action( 'admin_menu', function() {
 
    $page = add_submenu_page(
        'edit.php',
        'My Page',
        'My Page',
        'edit',
        'ccom_sample_page',
        function() {
            ?>
             <div class="wrap">
                <?php screen_icon(); ?>
                <h1>My Page</h1>
                <form name="my_form" method="post">  
                    <input type="hidden" name="action" value="some-action">
                    <?php
                    wp_nonce_field( 'some-action-nonce' );
                    wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
                    wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
                    $columns = get_current_screen()->get_columns() == 1 ? 1 : 2;
                    ?>
                    <div id="poststuff">
                        <div id="post-body" class="metabox-holder columns-<?php echo $columns; ?>">
                            <div id="postbox-container-1" class="postbox-container">
                                <?php do_meta_boxes( '', 'side', null ); ?>
                            </div>
                            <div id="postbox-container-2" class="postbox-container">
                                <?php do_meta_boxes( '', 'normal', null );  ?>
                                <?php do_meta_boxes( '', 'advanced', null ); ?>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <?php
    } );
 
	add_action( 'load-' . $page, function() {
		do_action( 'add_meta_boxes_' . $page, null );
		do_action( 'add_meta_boxes', $page, null );
		add_screen_option( 'layout_columns', [ 'max' => 2, 'default' => 2 ] );
		wp_enqueue_script( 'postbox' );
	} );
 
	add_action( 'admin_footer-' . $page, function() {
		?>
		<script type="text/javascript">
		postboxes.add_postbox_toggles( pagenow );
		</script>
		<?php
	} );

} );

add_action(
	'add_meta_boxes',
	function() {

		add_meta_box(
			'sample-box',
			'Sample Box',
			function() {

				echo 'Sample Box Contents';

			},
			'posts_page_ ccom_sample_page', 
			'normal', 'high'
		);

		add_meta_box(
			'sample-sidebar',
			'Sample Sidebar',
			function() {

				echo 'Sample Sidebar Content';

			},
			null, 'side', 'high'
		);

	}

);

Instructions for Admin page metaboxes

  1. Log into a staging or locally hosted clone of your site.
  2. Install and activate Code Snippets plugin.
  3. WP Admin > Snippets > Add New
  4. Copy and paste the code from the section above.
  5. Check to ensure formatting came over properly.
  6. Customize the code as desired.
  7. Add a meaningful title.
  8. Select whether to run on front-end or back-end (or both).
  9. Click “Save and Activate”.
  10. Test your site to ensure it works.
  11. Disable if any problems, or recover.
  12. Repeat for live environment.

Need help modifying Admin page metaboxes?

Contact me. I can help with fitting projects or refer to my partner.

License

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.

Disclaimer of warranty