Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Add Screen Options Tab to Your WordPress Plugin

Posted on April 4, 2012 by Pippin in Action and Filter Hooks, Intermediate, Tutorials, WordPress Admin / Dashboard, Writing Plugins 16 Comments
Home» Tutorials » Action and Filter Hooks » Add Screen Options Tab to Your WordPress Plugin
Screen Options
Tweet
Love It - 4

You have probably noticed the Screen Options tab in the top right corner of many WordPress admin pages. This tab brings down a configuration panel that you can generally use to control what elements are displayed on the current amin page. One of the really great things about the Screen Options tab is that which ever settings you select are saved to your user meta options, meaning they remain as are until you personally change them. While not very flexible, you can add your plugin’s own option to the Screen Options tab.

This tutorial is based on one written by Chris Marslender. I’m going to cover pretty much the same things Chris did, but I’m going to go just a little bit further and show you a demonstration of how I’ve used this in my Restrict Content Pro plugin.

The Screen Options APi works very similar to the add_help_tab() API, which I have covered several times before, but is a bit more limited. At this time the documentation on the function is very, very sparse. From exploring the core source, I’ve concluded that the add_screen_option() function does not have the ability to add custom option types, but rather allows you to enable certain predefined option types. For this tutorial, we’re going to look at adding a “per page” option to our plugin. Once complete, I will show you how I implemented this into Restrict Content Pro.

We are going to assume that you have already setup your plugin’s settings page, but if you haven’t, then check out some of these tutorials.

In order to register our screen option, we have to use the add_action(“load-$your-settings-page”, “screen_options_callback”); hook. This action is added just after we register our settings page, so yours might look something like this:

1
2
3
4
5
6
7
8
9
10
function pippin_sample_settings_menu() {
 
	global $pippin_sample_page;
 
	// add settings page
	$pippin_sample_page = add_menu_page(__('Sample Settings', 'pippin'), __('Settings', 'pippin'), 'manage_options', 'sample-settings-slug', 'sample_settings_display_page');
 
	add_action("load-$pippin_sample_page", "pippin_sample_screen_options");
}
add_action('admin_menu', 'pippin_sample_settings_menu');

We now setup our function that registers the screen option and name it pippin_sample_screen_options. Note that this is the same name as used for the callback in the add_action() above. If it is not exact, it will not work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function pippin_sample_screen_options() {
 
	global $pippin_sample_page;
 
	$screen = get_current_screen();
 
	// get out of here if we are not on our settings page
	if(!is_object($screen) || $screen->id != $pippin_sample_page)
		return;
 
	$args = array(
		'label' => __('Members per page', 'pippin'),
		'default' => 10,
		'option' => 'pippin_per_page'
	);
	add_screen_option( 'per_page', $args );
}

This will result in an option that looks like this:

The add_screen_option() function takes to parameters:

  1. $options – the option we are adding
  2. $args – the additional parameters for our option

There are only a few available choices for the first parameter, and I have not been able to find a list of them, so we will stick with “per_page” for now. This “per_page” determines the kind of HTML we are going to output for our screen option, which, in this case, is a small text input.

The $args array allows us to define the label of the input, the default value, and the ID of our option.

Now we need to add another function that will save the value we enter when clicking “Apply”. This is done with a simple filter.

1
2
3
4
function pippin_set_screen_option($status, $option, $value) {
	if ( 'pippin_per_page' == $option ) return $value;
}
add_filter('set-screen-option', 'pippin_set_screen_option', 10, 3);

The function takes three parameters:

  1. $status – I’m not sure what this is yet
  2. $option – the ID of our option, as defined by the ‘option’ key in our $args array above
  3. $value – the value we have entered in our “per page” input field

Try it out. When you click “Apply”, the page will reload and your option will be saved.

There is just one more thing to do: retrieve the stored option and use it to control the number of items displayed on the page for our plugin’s settings page. This is pretty simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
// get the current user ID
$user = get_current_user_id();
// get the current admin screen
$screen = get_current_screen();
// retrieve the "per_page" option
$screen_option = $screen->get_option('per_page', 'option');
// retrieve the value of the option stored for the current user
$per_page = get_user_meta($user, $screen_option, true);
if ( empty ( $per_page) || $per_page < 1 ) {
	// get the default value if none is set
	$per_page = $screen->get_option( 'per_page', 'default' );
}
// now use $per_page to set the number of items displayed

This will set the $per_page variable equal to whatever value is stored in your user’s meta option, and you can now use this in your plugin to control how many elements are displayed on the page. In the case of Restrict Content Pro, I’m using this to control the number of members displayed per page on the Member’s admin page.

By default, my Member’s page looks like this, with 10 members per page (there are only 7 in the system in this example):

Once the setting has been changed, it looks like this:

The implementation of the $per_page option will differ with every plugin, but in my case, I pass it as a parameter to the function that displays the registered members, like so:

1
$members = rcp_get_members($status, $subscription_id, $offset, $per_page, $order);

The $members variable is then looped through to display the details of each member.

If you have not made use of the screen options API, I’d highly recommend that you check it out.

Tweet Follow @pippinsplugins
add_filter, add_screen_option, Chris Marslender, get_current_screen

16 comments on “Add Screen Options Tab to Your WordPress Plugin”

  1. designerken says:
    May 17, 2012 at 3:25 pm

    I can get the options tab to appear and the default 10 is showing, but when I put in another number and apply the screen refreshes the number goes back to the default 10.

    I looked at my MYSQL and I see the pippin_per_page row in the user_meta table for the user but there is no value for it. When I manually place a value in the MYSQL then refresh the page the options tab shows the new value in the input field, but when I type in a new value and apply it the field goes back to the default 10 and the MYSQL field is blank again.

    Where am I going wrong with saving the value?

    Reply
    • Pippin says:
      May 17, 2012 at 4:57 pm

      Can you show me the code via a pastebin or snippi.com?

  2. designerken says:
    May 17, 2012 at 5:43 pm

    http://pastebin.com/Fu8L3a2s

    I have been following your plug-in tutorial so I have this split up over several files with some of them in an includes folder. I have changed some of the functions for the paste to be public. The pastebin shows three of those files. Let me know if you need to see more info.

    Reply
    • Pippin says:
      May 17, 2012 at 7:07 pm

      Found the problem :)

      Your global $test_my_page name is not consistent. First it is $test_my_page, then it is $my_test_page.

      You also need to make sure you are adding the screen option for your test page. See the updated version: http://pastebin.com/0k6ezGF6

  3. designerken says:
    May 18, 2012 at 10:15 am

    The global variable was a typo when changing the variable in pastebin to protect the innocent :) (my bad).

    I will go through and add the switch() into the code. Thanks for the tip. It was not mentioned in the code above or in Chris Marslender tutorial. The Check below is even different from the pastebin so I’ll have to go thoufh and see what is happening here..
    if(!is_object($screen) || $screen->id != $pippin_sample_page)
    return;

    Reply
    • Pippin says:
      May 18, 2012 at 11:21 am

      Whoops, I forgot about the check if($screen->id == $page_name).

  4. designerken says:
    May 18, 2012 at 2:06 pm

    Where does that go?
    if($screen->id == $page_name)

    Reply
    • Pippin says:
      May 18, 2012 at 6:06 pm

      In the test_screen_options() function.

  5. Archana Solanki says:
    September 13, 2012 at 1:57 am

    Hi Pippin,
    Thanks a lot for this wonderful tutorial :)
    I wanted to ask you that how can i add multiple options in screen options???
    Can you guide me in order to achieve this please?

    Reply
    • Pippin says:
      September 13, 2012 at 1:46 pm

      I think there is a limitation in the API. I couldn’t make multiple options work.

  6. Mikkel Breum says:
    December 7, 2012 at 7:15 am

    Hi Pippin, Thank you for the tutorial.

    add_screen_option() is indeed lacking documentation, and the source code is not that well documented either.

    I want to add an option to the dashboard screen, that should consist of a multi-select – or if that’s not possible – a group of checkboxes. Each option/checkbox should represent a custom post type.

    I’ll be using the options for a plugin that adds the selected custom post types to the Right Now dashboard.

    The one thing I can’t work out, is what slug to use for the $option parameter. ‘per_page’ is obviously not the one. I want to add my own ‘custom’ option type, consisting of an input element (multiple select box or group of checkboxes) and store the selected values in an array.

    Do you have any suggestions on this, or can you tell me right now, if this is not possible based on your knowledge with the API?

    Reply
  7. Mikkel Breum says:
    December 7, 2012 at 7:21 am

    oh, I just found your wrapper script, will try that out right away.
    http://w-shadow.com/blog/2010/06/29/adding-stuff-to-wordpress-screen-options/

    Reply
    • Pippin says:
      December 7, 2012 at 10:37 am

      Nice, let me know how well that works for you.

  8. Lowell says:
    January 13, 2013 at 4:01 pm

    Hey Pippin,

    I have a copy of ecct I bought from code canyon a while back. At first I thought it wasn’t the right choice for me because I’m not a php expert so I have difficulty getting the fields to display properly on the frontend. I ended up spending over $100 on another set of tools that provide a similar function.

    But I had trouble with it, after coming back and watching some of your tutorials I realized that ecct is the perfect tool for the job, it has just the right amount of functionality and I definitely recommend it and the level of documentation and support you provide.

    Quick question, when you add your action, what is the purpose of the ‘pippin’ ?

    $pippin_sample_page = add_menu_page(__('Sample Settings', 'pippin')

    A theme I have does that everywhere in their functions file and I was curious to the meaning of it…..

    ex. ( ‘sample settings’, “johnson_theme’)

    Thanks so much.

    Lowell

    Reply
    • Pippin says:
      January 14, 2013 at 11:41 am

      The “pippin” part is the text domain and is used for internationalization (making it possible to translate the text). Have a read through my tutorial on the subject for a more thorough explanation.

  9. Joseph Sileo says:
    February 7, 2013 at 9:06 pm

    You are awesome! Thanks for saving me a ton of time.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Plugin Development 101 – An Intro to Filters
  • Introduction to the gettext Filter in WordPress
  • Let’s Talk Extensible Code
  • Playing Nice with the “the_content” Filter
  • Adding Custom Fields to the Easy Digital Downloads Checkout

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (1)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @Rarst A whole lot of this: http://t.co/V81OP87Orl
    May 20, 2013
  • @Rarst lol, that&#039;s about half of the cars where I live
    May 20, 2013
  • @markmcwilliams happy birthday!
    May 20, 2013

Topics

Sugar Event Calendar the_content Rémi Corson get_user_meta wp_enqueue_script add_options_page attachments contextual help register_setting meta box featured add_shortcode hook login forms authors short codes attachment Related posts plugin do_action mail chimp image recent posts comments apply_filters post types bbpress short code taxonomies custom post type gallery Ajax images Stripe taxonomy jquery widgets users add_filter add_action easy content types widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins