Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #50432, comment 2


Ignore:
Timestamp:
06/24/2020 05:01:32 AM (6 years ago)
Author:
cyberplayer
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #50432, comment 2

    initial v1  
    66
    77CMIIW
     8
     9EDIT :
     10
     11Solved!
     12
     13My problem occurs because I use dynamic screen option name when the {{{set_screen_option}}} filter is executed.
     14
     15I can't directly use {{{set_screen_option_{$option}}}} filter because I never knew what my {{{{$option}}}} is. It was dynamically generated when my page is loaded. I need to get the $option first before I can use the new filter.
     16
     17The solution is :
     181. I changed all of my screen option name by adding "_page" at the end of the strings.
     192. That way my function can get in to {{{set-screen-option}}} filter hook that only applied to options ending with '_page' (since 5.4.2)
     203. Inside the function I could call the {{{set_screen_option_{$option}}}} filter because by that time I knew the {{{{$option}}}} value.
     214. I use anonymous function to return $value that I've got from {{{set-screen-option}}} filter.
     22
     23This is the snippet looks like :
     24{{{
     25add_filter('set-screen-option', array($this, 'set_screen_option_filter'), 10, 3); // fired before admin_menu hook
     26
     27function set_screen_option_filter($status, $option, $value) {
     28
     29        if($this->get_current_page_table() === false) return $status; // $status = false (default from WP)
     30               
     31        $current_active_page = $this->get_current_active_page(); // method dipanggil lagi, karena admin_menu belum dieksekusi
     32        $args_option = $current_active_page['page_slug']."_".$current_active_page['page_type']."_screen_option_page";
     33        $args_option = str_replace("-", "_", $args_option);
     34               
     35        if($args_option === $option) {
     36                add_filter("set_screen_option_{$option}", function($status, $option, $value2) use($value) { return $value; }, 10, 3);
     37                return $value;
     38        }
     39                return $status;
     40} // end of function
     41
     42}}}