[wp-trac] [WordPress Trac] #24786: filter set-screen-option not triggering

WordPress Trac noreply at wordpress.org
Thu Apr 16 15:45:57 UTC 2015


#24786: filter set-screen-option not triggering
----------------------------+----------------------
 Reporter:  robbcaldwell    |       Owner:
     Type:  defect (bug)    |      Status:  closed
 Priority:  normal          |   Milestone:
Component:  Administration  |     Version:  3.5.2
 Severity:  normal          |  Resolution:  invalid
 Keywords:                  |     Focuses:
----------------------------+----------------------

Comment (by tychay):

 To expand on this (for future people finding this in search).

 If {{{add_screen_option()}}} works then it's too late in your code for
 {{{add_filter('set-screen-option')}}} to ever be triggered.

 As you noted, the filter for {{{set-screen-option}}} is defined in
 misc.php where it is triggered in {{{set_screen_options()}}} which is
 called in the admin bootstrap ({{{admin.php}}} almost 100 lines
 '''before''' {{{set_current_screen()}}} which is needed for
 {{{add_screen_option()}}} to work (since it needs the
 {{{$current_screen}}}).

 Put another way, add the filter very early in your code (notably before
 `admin_init`) so it will actually be triggered. Then have it try to
 {{{get_current_screen()}}} and see that it is null. That's how early
 {{{set-screen-option}}} gets triggered.

 …

 It is common for you to
 {{{add_screen_option()}}}/{{{screen::add_option()}}} after you know what
 the screen identifier is so it doesn't trigger on everything…

 {{{
 #!php
         public function create_admin_menu()
         {
                 $this->_options_suffix = add_options_page( 'page title',
 'menu title', 'manage_options', 'SLUG', array( $this, 'show_settings_page'
 ) );
                 if ( $this->_options_suffix ) {
                         add_action( 'load-'.$this->_options_suffix,
 array($this, 'loading_settings_page') );
                 }
         }
         public function loading_settings_page() {
                 $screen    = get_current_screen();
                 $screen->add_option(
                         'per_page', // built-in type
                         array(
                                 'label'   => $optinfo['display'], // Label
 to use in screen_options
                                 'default' => $optinfo['default'], //
 default # when empty
                                 'option'  => $optinfo['name'],    // db
 option name
                         )
                 );
         }
 }}}

 but you’ll need to add the {{{set-screen-option}}} very early: {{{wp-
 loaded}}} and {{{init}}} is fine but before {{{admin_init}}}. In the
 example above, I call the function that adds both…

 {{{
 #!php
         public function run_admin() {
                 add_action( 'admin_menu', array( $this,
 'create_admin_menu' ) );
                 add_filter( 'set-screen-option', array( $this,
 'filter_screen_option'), 10, 3 );
                 //add_action( 'admin_init', array( $this, 'admin_init') );
         }
 }}}

 in a hook on{{{ plugins_loaded}}}.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/24786#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list