Creating Custom Meta Boxes on Plugin Option Page

Right in my main plugin file, wp-email-capture.php I am calling this:-

add_action( 'admin_init', 'wp_email_capture_add_custom_boxes');

This calls this function:-

Read More
function wp_email_capture_add_custom_boxes()
{
       add_meta_box("wp_email_capture_admin_supportbox", __('Support', 'WPEC'), "wp_email_capture_admin_supportbox", "wpemailcaptureoptions","side");
       //do_meta_boxes( 'wpemailcaptureoptions', 'normal' , NULL); - removed as per SE advice
}

And separately this function:-

function wp_email_capture_admin_supportbox()
{
        echo "<p>If you are having problems with this plugin, please read the Frequently Asked Questions, or alternatively submit a support request.</p>";
}

I open the WP Email Capture Options page (which has a slug of wpemailcaptureoptions) and I don’t see any box, or anything in the sidebar. It’s completely baffled me.

I suspect it is something on how it is called, I tried with add_action('add_meta_box',..., but then even the function wasn’t being called.

Ideas?

If you need anything else, please let me know 🙂

Edit

I have now moved the “do_meta_boxes” to the options page where I want the meta box displayed and still having no joy, here’s the function that manages the option page:-

function wp_email_capture_show_lists()
{
    $lists = wpecp_get_all_lists();
do_meta_boxes( 'wpemailcaptureoptions', 'normal' , NULL);
    echo '<div class="wrap">';
    echo '<img src="'.plugins_url('images/logolarge.png' , dirname(__FILE__)).'" align="right" style="padding-right: 5%" alt="WP Email Capture"> <h2>Lists</h2>';
?>  
<table class="widefat">
<thead>
<tr>
    <th><?php _e('List Name','WPEC'); ?></th>
    <th><?php _e('Edit','WPEC'); ?></th>      
    <th><?php _e('Delete','WPEC'); ?></th>
</tr>
</thead>
<tfoot>
<tr>
    <th><?php _e('List Name','WPEC'); ?></th>
    <th><?php _e('Edit','WPEC'); ?></th>      
    <th><?php _e('Delete','WPEC'); ?></th>
</tr>
</tfoot>
<tbody>

<?php

foreach ($lists as $list)
{

?>
    <tr valign="top">
    <th scope="row" style="width:400px"><?php echo $list->listid .". ". $list->listname; ?></th>

<?php 
    if ($list->listtype == 2)
    {

        $url = $_SERVER["REQUEST_URI"] . "&wpecr_listid=".$list->listid."&external=1";

    } else {

        $url = $_SERVER["REQUEST_URI"] . "&wpecr_listid=".$list->listid;
    }
 ?>

    <td><a href="<?php echo $url; ?>">Edit</a></td><td>

 <?php 
    if ($list->listid != get_option('wp_wpecp_default_list'))
    {

        $url = $_SERVER["REQUEST_URI"] . "&delete_listid=".$list->listid;

?>  
        <a href="<?php echo $url; ?>">Delete</a> 
<?php
    }
?>
    </td>
    </tr>
<?php
    }

    echo '</tbody></table>';
    echo "<p><a href='" . $_SERVER["REQUEST_URI"] ."&wpecr_listid=0' class='button-secondary'>Add New WP Email Capture List</a> <a href='" . $_SERVER["REQUEST_URI"] ."&wpecr_listid=0&external=1' class='button-secondary'>Add New External List</a></p>";
    echo '</div></div>';

} ?>

Related posts

Leave a Reply

2 comments

  1. The reason you’re having problems with the meta box is that your contexts don’t match up.

    When you call add_meta_box you specify the $context argument (the fifth one) as side. Yet when you call do_meta_boxes you do so with the context (second argument) normal. You need to change one or the other, but they should match up.

    To get the metaboxes to look right, you’ll also need to make sure you put them inside a container with the class metabox-holder.

    Your code would become:

    <?php
    function wp_email_capture_show_lists()
    {
        $lists = wpecp_get_all_lists();
        echo '<div class="wrap metabox-holder">';
        do_meta_boxes('wpemailcaptureoptions', 'side' , NULL);
    
        // snip snip
    
        echo '</div>'; // close the wrap
    }
    

    This question got me curious, so I wrote an example of how one could use meta boxes + the settings API on custom page. Might help you out:

    <?php
    
    WPSE57092::init();
    
    class WPSE57092
    {
        /**
         * The hook to add an action to add our meta boxes.
         *
         */
        const ADD_HOOK = 'wpse57092_add_boxes';
    
        /**
         * The page key for our meta boxes.  You could use this as the "group" for
         * the settings API as well or something similar.
         *
         */
        const PAGE = 'do_wpse57092_boxes';
    
        /**
         * The setting key.
         *
         */
        const SETTING = 'wpse57092_opts';
    
        public static function init()
        {
            add_action(
                'admin_menu',
                array(__CLASS__, 'page')
            );
    
            add_action(
                self::ADD_HOOK,
                array(__CLASS__, 'meta_box')
            );
    
            add_action(
                'admin_init',
                array(__CLASS__, 'settings')
            );
        }
    
        public static function settings()
        {
            register_setting(
                self::PAGE,
                self::SETTING,
                array(__CLASS__, 'validate')
            );
    
            add_settings_section(
                'default',
                __('A Settings Section', 'wpse57092'),
                '__return_false',
                self::PAGE
            );
    
            add_settings_field(
                'wpse57092-text',
                __('Some Field', 'wpse57092'),
                array(__CLASS__, 'field_cb'),
                self::PAGE,
                'default',
                array('label_for' => self::SETTING)
            );
        }
    
        public static function meta_box()
        {
            add_meta_box(
                'custom-meta-wpse57092',
                __('Just Another Meta Box', 'wpse57092'),
                array(__CLASS__, 'box_cb'),
                self::PAGE,
                'main',
                'high'
            );
        }
    
        public static function box_cb($setting)
        {
            // do_settings_fields doesn't do form tables for you.
            echo '<table class="form-table">'; 
            do_settings_fields(self::PAGE, 'default');
            echo '</table>';
        }
    
        public static function field_cb($args)
        {
            printf(
                '<input type="text" id="%1$s" name="%1$s" class="widefat" value="%2$s" />',
                esc_attr($args['label_for']),
                esc_attr(get_option($args['label_for']))
            );
            echo '<p class="description">';
            _e('Just some help text here', 'wpse57092');
            echo '</p>';
        }
    
        public static function page()
        {
            $p = add_options_page(
                __('WPSE 57092 Options', 'wpse57092'),
                __('WPSE 57092', 'wpse57092'),
                'manage_options',
                'wpse57092-options',
                array(__CLASS__, 'page_cb')
            );
        }
    
        public static function page_cb()
        {
            do_action(self::ADD_HOOK);
            ?>
            <div class="wrap metabox-holder">
                <?php screen_icon(); ?>
                <h2><?php _e('WPSE 57092 Options', 'wpse57092'); ?></h2>
                <p>&nbsp;</p>
                <form action="<?php echo admin_url('options.php'); ?>" method="post">
                    <?php
                    settings_fields(self::PAGE);
                    do_meta_boxes(self::PAGE, 'main', self::SETTING);
                    ?>
                </form>
            </div>
            <?php
        }
    
        public static function validate($dirty)
        {
            return esc_url_raw($dirty);
        }
    }