Add meta box to WordPress options page

How can I add a (draggable) meta box to an options page for a plugin I created? Is this even possible? Because if I look in the docs I see that I can only add it to a ‘post’, ‘page’, ‘link’, or ‘custom_post_type’.

Related posts

Leave a Reply

1 comment

  1. Yes it’s possible. The code in your previous question was correct but it misses something important or you haven’t added that code to the question.

    Here is a demo plugin that can help you get it working.

    This Plugin demonstrates how you can build your own plugin pages using the WordPress provided draggable metaboxes, requires WordPress 2.7 version, supports WordPress 2.8 changed boxing layout engine

    The basic code from the demo plugin is the following. Note that in the full exemple the side meta boxes are not working nor the two columns layout as it was written for WordPress 2.8 and current version is almost 5.0.

    // Source code by Frank Bueltge at gist.github.com/bueltge/757903
    class howto_metabox_plugin {
        function howto_metabox_plugin() {
            add_action('admin_menu', array($this, 'on_admin_menu')); 
            add_action('admin_post_save_howto_metaboxes_general', array($this, 'on_save_changes'));
        }
    
        function on_admin_menu() {
            $this->pagehook = add_options_page('Howto Metabox Page Title', "HowTo Metaboxes", 'manage_options', 'howto_metaboxes', array($this, 'on_show_page'));
            add_action('load-'.$this->pagehook, array($this, 'on_load_page'));
        }
    
        function on_load_page() {
            wp_enqueue_script('common');
            wp_enqueue_script('wp-lists');
            wp_enqueue_script('postbox');
            add_meta_box('howto-metaboxes-contentbox-2', 'Contentbox 2 Title', array($this, 'on_contentbox_2_content'), $this->pagehook, 'normal', 'core');
            add_meta_box('howto-metaboxes-contentbox-additional-1', 'Contentbox Additional 1 Title', array($this, 'on_contentbox_additional_1_content'), $this->pagehook, 'additional', 'core');
        }
    
        function on_show_page() {
            //define some data can be given to each metabox during rendering
            $data = array('My Data 1', 'My Data 2', 'Available Data 1');
            ?>
            <div id="howto-metaboxes-general" class="wrap">
            <?php screen_icon('options-general'); ?>
            <h2>Metabox Showcase Plugin Page</h2>
            <form action="admin-post.php" method="post">
                <?php wp_nonce_field('howto-metaboxes-general'); ?>
                <input type="hidden" name="action" value="save_howto_metaboxes_general" />
    
                <div id="poststuff" class="metabox-holder">
                    <div id="side-info-column" class="inner-sidebar">
                        <?php do_meta_boxes($this->pagehook, 'side', $data); ?>
                    </div>
                    <div id="post-body" class="has-sidebar">
                        <div id="post-body-content" class="has-sidebar-content">
                            <?php do_meta_boxes($this->pagehook, 'normal', $data); ?>
                            <?php do_meta_boxes($this->pagehook, 'additional', $data); ?>
                            <p>
                                <input type="submit" value="Save Changes" class="button-primary" name="Submit"/>    
                            </p>
                        </div>
                    </div>
                    <br class="clear"/>
    
                </div>  
            </form>
            </div>
            <script type="text/javascript">
                //<![CDATA[
                jQuery(document).ready( function($) {
                    // close postboxes that should be closed
                    $('.if-js-closed').removeClass('if-js-closed').addClass('closed');
                    // postboxes setup
                    postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
                });
                //]]>
            </script>
    
            <?php
        }
    
        function on_save_changes() {
            if ( !current_user_can('manage_options') )
                wp_die( __('Cheatin’ uh?') );         
    
            check_admin_referer('howto-metaboxes-general');     
            //process here your on $_POST validation and / or option saving 
            //lets redirect the post request into get request (you may add additional params at the url, if you need to show save results
            wp_redirect($_POST['_wp_http_referer']);        
        }
        function on_contentbox_2_content($data) {
            sort($data);
            ?>
            <p>The given parameter at <b>reverse sorted</b> order are: <em><?php echo implode(' | ', array_reverse($data)); ?></em></p>
            <?php
        }
        function on_contentbox_additional_1_content($data) {
            ?>
            <p>This and the 2nd <em>additional</em> box will be addressed by an other group identifier to render it by calling with this dedicated name.</p>
            <p>You can have as much as needed box groups.</p>
            <?php
        }
    }
    $my_howto_metabox_plugin = new howto_metabox_plugin();