将元框添加到 WordPress 选项页面 [英] Add meta box to WordPress options page

查看:24
本文介绍了将元框添加到 WordPress 选项页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将(可拖动的)元框添加到我创建的插件的选项页面?这甚至可能吗?因为如果我查看文档,我会发现我只能将其添加到帖子"、页面"、链接"或custom_post_type"中.

解决方案

是的,这是可能的.您上一个问题中的代码是正确的,但它遗漏了一些重要的内容,或者您​​尚未将该代码添加到问题中.

这是一个演示插件,可以帮助您使其正常工作.

<块引用>

此插件演示了如何使用 WordPress 提供的可拖动元框构建自己的插件页面,需要 WordPress 2.7 版本,支持 WordPress 2.8 更改装箱布局引擎

演示插件的基本代码如下.请注意,在完整示例中,侧面元框不起作用,两列布局也不起作用,因为它是为 WordPress 2.8 编写的,当前版本几乎是 5.0.

//源代码由 Frank Bueltge 在 gist.github.com/bueltge/757903类 howto_metabox_plugin {函数 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'));}函数 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'));}函数 on_load_page() {wp_enqueue_script('common');wp_enqueue_script('wp-lists');wp_enqueue_script('邮箱');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');}函数 on_show_page() {//定义一些数据可以在渲染期间提供给每个metabox$data = array('我的数据 1', '我的数据 2', '可用数据 1');?><div id="howto-metaboxes-general" class="wrap"><?php screen_icon('options-general');?><h2>Metabox Showcase 插件页面</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 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>

<br class="clear"/>

</表单>

<script type="text/javascript">//<![CDATA[jQuery(文档).ready(函数($){//关闭应该关闭的邮箱$('.if-js-closed').removeClass('if-js-closed').addClass('closed');//邮箱设置postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');});//]]><?php}函数 on_save_changes() {如果 ( !current_user_can('manage_options') )wp_die( __('Cheatin&#8217; uh?') );check_admin_referer('howto-metaboxes-general');//在此处理您的 $_POST 验证和/或选项保存//让我们将 post 请求重定向到 get 请求(如果需要显示保存结果,您可以在 url 中添加额外的参数wp_redirect($_POST['_wp_http_referer']);}函数 on_contentbox_2_content($data) {排序($数据);?><p>给定参数在<b>反向排序</b>顺序是:<em><?php echo implode(' | ', array_reverse($data));?></em></p><?php}函数 on_contentbox_additional_1_content($data) {?><p>这个和第二个<em>附加</em>框将由其他组标识符寻址以通过使用此专用名称调用来呈现它.</p><p>您可以根据需要拥有尽可能多的框组.</p><?php}}$my_howto_metabox_plugin = new howto_metabox_plugin();

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

解决方案

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&#8217; 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();

这篇关于将元框添加到 WordPress 选项页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆