| | 8 | |
| | 9 | EDIT : |
| | 10 | |
| | 11 | Solved! |
| | 12 | |
| | 13 | My problem occurs because I use dynamic screen option name when the {{{set_screen_option}}} filter is executed. |
| | 14 | |
| | 15 | I 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 | |
| | 17 | The solution is : |
| | 18 | 1. I changed all of my screen option name by adding "_page" at the end of the strings. |
| | 19 | 2. 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) |
| | 20 | 3. Inside the function I could call the {{{set_screen_option_{$option}}}} filter because by that time I knew the {{{{$option}}}} value. |
| | 21 | 4. I use anonymous function to return $value that I've got from {{{set-screen-option}}} filter. |
| | 22 | |
| | 23 | This is the snippet looks like : |
| | 24 | {{{ |
| | 25 | add_filter('set-screen-option', array($this, 'set_screen_option_filter'), 10, 3); // fired before admin_menu hook |
| | 26 | |
| | 27 | function 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 | }}} |