在插件选项页上创建自定义元框

时间:2012-07-01 作者:Rhys Wynne

就在我的主插件文件wp email capture中。php我称之为:-

add_action( \'admin_init\', \'wp_email_capture_add_custom_boxes\');
这将调用此函数:-

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
}
另外,此功能:-

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>";
}
我打开了WP Email Capture Options页面(其中有大量wpemailcaptureoptions),但在侧边栏中没有看到任何框或任何内容。这让我完全困惑。

我怀疑这是怎么叫的,我试过了add_action(\'add_meta_box\',..., 但是,即使是函数也没有被调用。

想法?

如果您还需要什么,请告诉我:)

Edit

现在,我已经将“do\\u meta\\u box”移动到了选项页面,在那里我希望显示meta box,但仍然没有任何乐趣,下面是管理选项页面的功能:-

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>\';

} ?>

2 个回复
SO网友:Bainternet

你需要打电话do_meta_boxes() 在插件选项页中,而不是在admin_init

SO网友:chrisguitarguy

元框出现问题的原因是上下文不匹配。

当你打电话的时候add_meta_box 您可以指定$context 论点(第五个)作为一方。然而当你打电话的时候do_meta_boxes 您可以在上下文中执行此操作(第二个参数)normal. 你需要改变其中一个,但它们应该匹配。

要使元盒看起来正确,还需要确保将它们放在类所在的容器中metabox-holder.

您的代码将变成:

<?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
}
这个问题让我好奇,所以我wrote an example 如何在自定义页面上使用元框+设置API。可能会帮助您:

<?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);
    }
}

结束

相关推荐

更改默认自定义字段Metabox名称

我发现元框的标题不能通过cctm plugin. 它将“自定义字段”显示为默认值,这很恼人。Image所以我决定改变它的功能。php,下面是我的代码:add_filter(\'add_meta_boxes\', \'change_meta_box_titles\'); function change_meta_box_titles() { $wp_meta_boxes[\'my_post_type\'][\'normal\'][\'core\'][\'cctm_default\'][\'t