将屏幕选项添加到自定义管理页面

时间:2013-05-11 作者:Sudar

我想将屏幕选项添加到我的插件设置页面,就像仪表板中可用的选项一样。

enter image description here

我试着用add_option 的方法WP_Screen 对象,并发现它只支持两个选项。per_pagelayout_columns.

有人知道要使用什么屏幕选项来获取仪表板页面中的选项吗?

Edit:

让我解释一下我想说什么。

我的Bulk Delete Plugin 每个部分都允许人们根据一些标准(如类别、标记、自定义分类法等)删除帖子。我想让用户选择他们想要使用的部分和他们想要隐藏的部分,类似于仪表板页面,在那里用户可以选择他们想要查看和隐藏的仪表板小部件。

现在,为了实现这一点,我想显示一个复选框列表(每个部分一个),并让用户选择要显示的复选框。

要显示复选框列表,我必须调用add_option 的方法WP_Screen 对象当我这么做的时候,我发现add_option 函数只支持这两种类型,其他类型则被忽略。

每个页面的布局列,但仅在仪表板页面中显示复选框。我想知道如何在自定义管理页面的屏幕选项部分复制类似的内容。

3 个回复
最合适的回答,由SO网友:fuxia 整理而成

您不需要创建新的屏幕选项行。只需使用适当的代谢箱。

当前,您正在绘制伪元盒:

<!-- Post status start-->
        <div class = "postbox">
            <div class = "handlediv"> <br> </div>
            <h3 class = "hndle"><span><?php _e("By Post Status", \'bulk-delete\'); ?></span></h3>
        <div class = "inside">
        <h4><?php _e("Select the posts which you want to delete", \'bulk-delete\'); ?></h4>
您应该这样做:

<div id="post-body-content">
    <!-- #post-body-content -->
</div>

<div id="postbox-container-1" class="postbox-container">
    <?php do_meta_boxes(\'\',\'side\',$object); ?>
</div>

<div id="postbox-container-2" class="postbox-container">
    <?php do_meta_boxes(\'\',\'normal\',$object); ?>
    <?php do_meta_boxes(\'\',\'advanced\',$object); ?>
</div>
然后将您自己的元数据库注册到add_meta_box().

阅读Meta Boxes on Custom Pages 从…起Stephen Harris 了解所有详细信息(demo on GitHub).<要点是:您将免费获得这些框的屏幕选项。

当WordPress有一天更改元盒的内部标记时,您的代码可能仍然可以工作,因为您已经使用了API。

SO网友:kaiser

您可以通过在\\WP_Screen 班只需确保不按默认值将其打开即可:

如何显示或隐藏选项卡下面的过滤器显示如何显示或隐藏选项卡。实际上,由于有一个更好的过滤器,如果需要在选项卡已经存在时强制隐藏该选项卡,则以下方法更有用:

add_filter( \'screen_options_show_screen\', function( $show, \\WP_Screen $screen )
{
    // Navigate to the screen of choice and uncomment the following line to find out the \'base\' val
    // var_dump( $screen );
    return \'your_screen_id\' !== $screen->base
        ? $show
        : true;
}, 10, 2 );
如何显示选项卡和添加自定义内容下面显示了一个包含保存值的输入字段的设置选项卡amount 您可以在页面上以任何方式使用(例如,限制$wpdb 查询)。

/**
 * @param string     $settings
 * @param \\WP_Screen $screen
 */
add_filter( \'screen_settings\', function( $settings, \\WP_Screen $screen )
{
    if ( \'your_screen_id\' !== $screen->base )
        return $settings;

    $amount = isset( $_GET[\'paged\'] ) 
        ? filter_var(
            absint( $_GET[\'paged\'] ),
            FILTER_SANITIZE_NUMBER_INT,
            FILTER_NULL_ON_FAILURE
        ) 
        : 1;

    return sprintf(
        \'<label for="amount">Amount:</label> \'
        .\'<input step="1" min="1" max="999" class="screen-per-page" name="amount" val="%d">\',
        .get_submit_button( \'Set\', \'secondary\', \'submit-amount\', false ),
        $amount
    );
}, 10, 2 );

SO网友:Ярослав Рахматуллин

下面是一个基于Meta Boxes on Custom Pages by Stephen Harris:

与a相同的代码gist

<?php

/**
 * Plugin Name:     LHF Volunteer Form
 * Description:     Manages a google-sheet full of names and emails
 * Plugin URI:      http://ladehammerfestivalen.no/volunteer
 * Author URI:      http://genja.org
 * Author:          [email protected]
 * Text Domain:     lhf-volunteer-form
 * Domain Path:     /languages
 * Version:         0.2.0
 * @package         Lhf_Volunteer_Form
 */

require_once  __DIR__ . \'/vendor/autoload.php\';

use Lhf\\Sheet\\RegistrationsSheet;

frivilligSystemMain();

function frivilligSystemMain() {
    try {
        $regSheet = \\Lhf\\Sheet\\RegistrationsSheet::createInWordPress();
    } catch (\\Exception $ex) {
        error_log(sprintf(\'%s:%d %s\', __FILE__, __LINE__, $ex->getMessage()));
    }
    add_action(\'init\', function() use ($regSheet) {
        if (is_admin()) {
            add_action( \'admin_menu\', function() use ($regSheet) {
                $screenId = DashboardView::screen_id;
                $pageId = add_dashboard_page( \'hammerater\', \'Hammerater\', \'endre_frivillig_skjema\', $screenId,
                    function () use ($regSheet) { DashboardView::renderVolunteerDashboard($regSheet); } );
                add_action("load-$pageId", function() use ($regSheet, $pageId, $screenId) {
                    wp_enqueue_script(\'postbox\');
                    add_screen_option(\'layout_columns\', array(\'max\' => 2, \'default\' => 2) );
                    do_action("add_meta_boxes_$screenId", null); // allow third parties to hook into this.
                    do_action(\'add_meta_boxes\', $screenId, null); // allow third parties to hook into this.
                });
                add_action("add_meta_boxes_$screenId", function () use ($regSheet) { DashboardView::registerMetaboxes($regSheet); });
            });
        }
    });
}

class DashboardView
{
    const screen_id = \'frivillig-liste\';

    private static function includeAdminHeader()
    {
        require_once( ABSPATH . \'wp-admin/admin.php\');
        require_once( ABSPATH . \'wp-admin/includes/dashboard.php\');
        require_once( ABSPATH . \'wp-admin/admin-header.php\');
        wp_dashboard_setup();
        wp_enqueue_script( \'dashboard\' );
        add_thickbox();
        do_action( \'add_meta_boxes\' );
        if ( wp_is_mobile() ) {
            wp_enqueue_script( \'jquery-touch-punch\' );
            //wp_dequeue_script(\'jquery-migrate\');
        }

        wp_enqueue_script( \'datatables\', \'//cdn.datatables.net/1.10.16/js/jquery.dataTables.js\');
        wp_enqueue_style( \'datatables\', \'//cdn.datatables.net/1.10.16/css/jquery.dataTables.css\');

        wp_enqueue_script( \'datatables-responsive\', \'//cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js\');
        wp_enqueue_style(  \'datatables-responsive\', \'//cdn.datatables.net/responsive/2.2.1/css/responsive.dataTables.min.css\');

        wp_enqueue_script( \'datatables-row-group\', \'//cdn.datatables.net/rowgroup/1.0.2/js/dataTables.rowGroup.min.js\');
        wp_enqueue_style(  \'datatables-row-group\', \'//cdn.datatables.net/rowgroup/1.0.2/css/rowGroup.dataTables.min.css\');
    }

    static function renderVolunteerDashboard(RegistrationsSheet $regSheet) {
        static::includeAdminHeader();
        wp_enqueue_script(\'lhf-sheets\');
        $workTypes = get_option( \'lhfsheets_form_work_types\' );
        ?>
        <div class="wrap">
            <form>
<?php
        wp_nonce_field( \'meta-box-order\', \'meta-box-order-nonce\', false );
        wp_nonce_field( \'closedpostboxes\', \'closedpostboxesnonce\', false );
?>
            <h1>Frivillige Hammerater</h1>
            <h2 class="nav-tab-wrapper">
                <a href="<?= admin_url( \'index.php?page=frivillig-liste&tab=liste\' ) ?>" >Liste</a>
                <a href="<?= admin_url( \'index.php?page=frivillig-liste&tab=preferanser\' ) ?>">Arbeidsposter</a>
                <a href="<?= admin_url( \'index.php?page=frivillig-liste&tab=info\' ) ?>">Frivilliginfo</a>
            </h2>
            <?php
            $screen      = get_current_screen();
            $columns     = absint( $screen->get_columns() );
            $columns_css = \'\';
            if ( $columns ) {
                $columns_css = " columns-$columns";
            }
            ?>

            <?php if ( $_GET[\'tab\'] == \'liste\' || ! $_GET[\'tab\'] ) { ?>
                <div id="dashboard-widgets" class="metabox-holder<?php echo $columns_css; ?>">
                    <div id="postbox-container-1" class="postbox-container">
                        <?php do_meta_boxes( $screen->id, \'main_list\', \'\' ); ?>
                    </div>
                </div>
            <?php } ?>

            <?php if ( $_GET[\'tab\'] == \'preferanser\' || ! $_GET[\'tab\'] ) { ?>
                <div id="dashboard-widgets" class="metabox-holder<?php echo $columns_css; ?>">
                    <div id="preferences-sortables" class="postbox-container">
                        <?php do_meta_boxes( $screen->id, \'preferences\', \'\'); ?>
                    </div>
                    <div id="preferences_left-sortables" class="postbox-container">
                        <?php do_meta_boxes( $screen->id, \'preferences_right\', \'\'); ?>
                    </div>
                </div>
            <?php } ?>
            <?php if ( $_GET[\'tab\'] == \'info\' ) { ?>
                        <h3>Annen info</h3>
            <?php } ?>
            </form>
        </div>
        <?php
    }

    static function renderMainList($records, $status = \'registered/served/contacted\') {
        /** @var Frivillig $e */
        ?>
        <div class="main">
          <table id="frivillige-hammerater-<?= $status ?>" style="display:none" data-status="<?= $status ?>">
            <?php foreach ($records as $e) { ?>
              <tr> ...  </tr>
            <?php } ?>
            </tbody>
          </table>
        </div>
        <?php
    }

    public static function registerMetaboxes( RegistrationsSheet $regSheet ) {
        if ($_GET[\'tab\'] == \'info\') { return; }
        $all = $regSheet->getVolunteerRecords();
        if ($_GET[\'tab\'] == \'liste\' || $_GET[\'tab\'] === null) {
            foreach (Frivillig::states() as $state) {
                add_meta_box(
                    "volunteers-search-all",
                    __(\'Verktøy\') ,
                    function () use ($state) { DashboardView::renderGlobalSearchMetaBox(); },
                    \'dashboard_page_frivillig-liste\',
                    \'main_list\'
                );

                $peopleWithState = [];
                foreach ($all as $f) { if ($f->status === $state) { $peopleWithState[] = $f; } }

                add_meta_box(
                    "volunteers-$state",
                    DashboardView::$stateName[$state],
                    function () use ($peopleWithState, $state) { DashboardView::renderMainList($peopleWithState, $state); },
                    \'dashboard_page_frivillig-liste\',
                    \'main_list\'
                );
            }
        }

        if ($_GET[\'tab\'] == \'preferanser\') {
            $workTypes = get_option(\'lhfsheets_form_work_types\');
            foreach ($workTypes as $workType) {
                $workers = [];
                foreach ($all as $frivillig) {
                    $interests = preg_split(\'/,\\s+/\', $frivillig->interests);
                    if (in_array($workType[\'slug\'], $interests)) {
                        $workers[] = $frivillig;
                    }
                }
                add_meta_box(
                    "volunteer-prefers-{$workType[\'slug\']}",
                    $workType[\'description\'],
                    function () use ($workers, $workType) { DashboardView::renderPreferences($workers, $workType); },
                    \'dashboard_page_frivillig-liste\',
                    \'preferences\'
                );
            }
        }
    }

    public static function renderPreferences($workers, $workType) { ?>
        <ul>
            <?php foreach ($workers as $e) { ?>
            <li data-id="<?= $e->id ?>"> ...  </li>
            <?php } ?>
        </ul>
        <?php
    }

    private static function renderGlobalSearchMetaBox() {
        ?> Søk: <input type="text" onkeydown="window.populateSearchFields(event)" placeholder="<?= __(\'i alle tabellene\') ?>  "> <?php
    }
}
参考文献

https://codex.wordpress.org/Dashboard_Widgets_API

结束

相关推荐

Selecting a post in Dashboard

如何在管理仪表板中设置a<;选择(>);指定职位类型的所有职位列表?我需要这个来保存帖子IDset_option() 呼叫如何做到这一点?