Filters a screen option value before it is set.
Description
The filter can also be used to modify non-standard [items]_per_page
settings. See the parent function for a full list of standard options.
Returning false from the filter will skip saving the current option.
See also
Parameters
$screen_option
mixed- The value to save instead of the option value.
Default false (to skip saving the current option). $option
string- The option name.
$value
int- The option value.
Source
$screen_option = apply_filters( 'set-screen-option', $screen_option, $option, $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
This cannot be called too late in the WordPress action stack or it will not fire at the right time. You cannot put it inside a function running during the ‘admin_menu’ hook, for example. For the Store Locator Plus plugin (you can find up-to-date working code buried in there) I find adding the filter inside the WordPress ‘init’ methods works best as this is called early in the WordPress action stack.
The “sister” add_screen_option() call, however, tends to work better later in the call stack such as within the ‘admin_menu’ hook.
The general premise: save the options goes before (WP ‘init’ hook) setting up the options (WP ‘admin_menu’ hook).
To make it really work and easy to understand.
This code will NOT work.
If you enter 200 as item per page, it will still be 200 not 100 because you call to this filter too early.
This code will work.
Just add priority to more than 10.
Tested in WordPress 5.1