Wordpress - Add screen options to custom admin pages

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.


You can do it, by using the proper filter inside the \WP_Screen class. Just make sure that you don't switch it on per default:

How to show or hide the tab

The following filter shows how to show or hide the tab. Actually, as there is a better filter, the following way is of more use if you need to force hide the tab when it already exists:

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

How to show the tab and add custom content

The following shows a settings tab containing an input field that holds the value amount that you could use in any way on your page (for e.g. limitting results of $wpdb query).

/**
 * @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 );