I want to add screen options to my plugin settings page, like the ones that are available in the Dashboard.
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.
You don’t need to invent a new screen option row. Just use proper metaboxes.
Currently, you are drawing pseudo-metaboxes:
You should do this:
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:
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).Here is a full, concise example based on the Meta Boxes on Custom Pages by Stephen Harris:
Same code as a gist
references
https://codex.wordpress.org/Dashboard_Widgets_API