• HOME
  • NEWS
  • PORTFOLIO
  • LAB
  • ABOUT
  • CONTACT

wordpress filter set-screen-option not working

tags : backendscreen optionsset-screen-optionsnippetwordpress categories : programmation

by Trân on January 19, 2020


Notice: A non well formed numeric value encountered in /home/tigerbal/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/tigerbal/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/tigerbal/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/tigerbal/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/tigerbal/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/tigerbal/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

I recently tried to use screen options for a wordpress backend plugin. I wanted to use a per page variable to be able to easily manage a number of users to request.
I first init the screen options :

1
2
3
4
5
6
7
8
add_action('my_plugin_slug', array( $this, 'screen_set_option'));
 
function screen_set_option() {
    add_screen_option('per_page', array(
    'default'=>10,
    'label'=>__('Users per page', 'my_domain'),
    'option'=>'users_per_page'));
}

But then I couldn’t find a way to make the filter set-screen-option to work. This filter is used to filter a screen option value before it is set (https://developer.wordpress.org/reference/hooks/set-screen-option/).
I tried this code with no success :

1
2
3
4
5
6
7
8
9
10
add_filter('set-screen-option', array($this, 'set_screen_option'), 10, 3);
 
function set_screen_option($status, $option, $value) {
      if ( 'search_member_per_page' == $option ) {
          $user = get_current_user_id();
          update_user_meta($user, 'users_per_page', $value);
          return $value;
     }
     return $value;
}

So I choose to save the screen option value after it was posted :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// when rendering the page :
 
$user=get_current_user_id();
$screen=get_current_screen();
 
if ( isset( $_POST['wp_screen_options'] ) &&is_array( $_POST['wp_screen_options'] ) ) {
     $option=$_POST['wp_screen_options']['option'];
     if ($option=='users_per_page') {
        $users_per_page=$_POST['wp_screen_options']['value'];
         update_user_meta($user, users_per_page', $users_per_page);
          apply_filters( 'set-screen-option', false, $option, $users_per_page );
     }
} else {
    $users_per_page=get_user_meta($user, 'users_per_page', true);
 
    if ( empty ( $users_per_page) ||$users_per_page<1 ) {
       $users_per_page=$screen->get_option( 'per_page', 'default' );
    } else {
       apply_filters( 'set-screen-option', false, 'users_per_page', $users_per_page);
    }
}

Notice that I store the screen option value in the user meta table to be able to easily retrieved it.

 

woocommerce, overwrite auto scroll (scroll to notice function) » « woocommerce membership and subscription plugin snippet

One thought on “wordpress filter set-screen-option not working”

  1. Nirjhar Lo says:
    June 13, 2020 at 10:48 am

    I spent 3 to 4 hours, regarding this.
    But update_user_meta function was missing in my code.

    Thanks a lot for this post.

    Reply

Leave a Reply Cancel reply

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

 
Tags / Mots clefs

backend • screen options • set-screen-option • snippet • wordpress
 

© fruit du dragon - freelance creative coder

Categories

  • général
  • inspiration
  • lab
  • programmation
  • sites
  • work

Tags

android architecture archives canvas cellules-studio cornelius css culturel demonstrateur design eshop flash fremok html html/css/js hugo IFTTT incandescence Instagram institutionnel ios ipad jeux js krpano luxe media design mobile musée nomoon paper fold photographie requins-marteaux responsive sbam snippet tablette toute-attente tran nn transport typographie ui ultranoir woocommerce wordpress

Stats


Notice: Undefined variable: extra in /home/tigerbal/www/wp-content/plugins/pulsemaps/pm-widget.php on line 47

Archives

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org