Add screen options to custom admin pages

I want to add screen options to my plugin settings page, like the ones that are available in the Dashboard.

enter image description here

I tried using the add_option method of the WP_Screen object and found that it supports only two options. per_page and layout_columns.

Does anyone know what screen option to use to get options like the one in Dashboard page?

Edit:

Let me explain what I am trying to a little bit.

I have different sections in my Bulk Delete Plugin and each section let’s people delete posts based on some criteria (like category, tags, custom taxonomy etc). I want to let users choose which sections they want to use and which sections that they want to hide, similar to the Dashboard page, where users can choose which dashboard widgets they want to see and which ones to hide.

Now, to implement this, I want to show a list of checkboxes (one for each section) and let user choose which one to show.

To show the list of checkboxes, I had to call the add_option method of the WP_Screen object. When I was doing that, I figured out that currently add_option function only supports these two types and the others are just ignored.

  • per_page
  • layout_columns

But, only in the dashboard page checkboxes are shown. I want to know how to replicate a similar thing in the screen options section of my custom admin page as well.

3

You don’t need to invent a new screen option row. Just use proper metaboxes.

Currently, you are drawing pseudo-metaboxes:

<!-- 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>

You should do this:

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

Then register your own metaboxes with add_meta_box().

Read Meta Boxes on Custom Pages from Stephen Harris for all the details (demo on GitHub).
The main point is: You will get the screen options for these boxes for free.

And when WordPress changes the inner markup for metaboxes one day, your code will probably still work, because you have used the API.

Leave a Comment