Force Screen Options for every Websites in WPMU

Hi there heroes!

Could you provide some insights about the best way to customise Screen Options?

We would like to make some Screen Options "checked" by default. For example, on Navigation Menus Pages, we would like to check the "Product Categories" by default.

Further than customising, we would also like to hide "Screen Options" from all Administrators.

Could you give me some feedback about the best way to achieve this?

Thanks again for all the support!

  • Jude

    Howdy Urbano

    Hope all is well with you !

    Could you provide some insights about the best way to customise Screen Options?

    We would like to make some Screen Options "checked" by default. For example, on Navigation Menus Pages, we would like to check the "Product Categories" by default.

    Here you go, this is a good approach to set defaults, do note that one a member sets the option this filter wont work

    http://wordpress.stackexchange.com/a/26764/69926

    Further than customising, we would also like to hide "Screen Options" from all Administrators.

    This snippet should help here, take a look

    http://wp-snippets.com/hide-screen-options-tab/

    Cheers
    Jude

    • Urbano

      Thanks for your reply Jude!

      Here you go, this is a good approach to set defaults, do note that one a member sets the option this filter wont work

      http://wordpress.stackexchange.com/a/26764/69926

      I'm (very) confused. From what I've understood, he meant to hide some of the screen options. I'm (very) newb at PHP, could you help me figure out what lines of code do I need?

      So far I've understood we need to attach this action to the user registration:

      // add_action('user_register', 'set_user_metaboxes');
      add_action('admin_init', 'set_user_metaboxes');
      function set_user_metaboxes($user_id=NULL) {

      And I might need the oposite of hidden here?

      // Set the default hiddens if it has not been set yet
      if ( ! get_user_meta( $user_id, $meta_key['hidden'], true) ) {
      $meta_value = array('postcustom','trackbacksdiv','commentstatusdiv','commentsdiv','slugdiv','authordiv','revisionsdiv');
      update_user_meta( $user_id, $meta_key['hidden'], $meta_value );
      }
      }

      Apologize for my confusion on this, I'm really lost. Thanks for your help!

          • Jude

            Hey again Urbano

            1) You'd want to create a mu plugin for this

            2) All the options entered here will stay hidden, the rest will be visible

            3) I've further simplified the above code to work as intended for you

            function hide_meta_box_attributes( $hidden, $screen) {
            // Replace stuff here n copy/paste
                $hidden = array( 'product_categorydiv' , 'mp-related-products-metabox') ;
                return $hidden;
            
            }
            add_filter('hidden_meta_boxes', 'hide_meta_box_attributes', 10, 2);

            4) Finally, if you want to get rid of screen options for certain roles. You'd want to use the code below ( add remove roles as needed )

            if ( !is_admin() ) {
              function remove_screen_options(){ __return_false;}
              add_filter('screen_options_show_screen', 'remove_screen_options');
            }

            Hope that clarifies

            Cheers
            Jude

          • Urbano

            Hi there Jude, thank's for your precious help!

            That's a terrific script you got there, I'll give it a try.

            2) All the options entered here will stay hidden, the rest will be visible

            One question: Our goal is to show a default hidden option. Should we hide all except the ones we want to keep visible, even tho they are hidden by default?

            An example of our goal is the Menu Page, where "Product Categories" from WooCommerce are hidden by default, as you can see on the attached image.

            4) Finally, if you want to get rid of screen options for certain roles. You'd want to use the code below ( add remove roles as needed )

            This could be a very helpful solution, but we'd like to start testing hiding the screen options button for all users except super-admins. I believe there's an option for this on Branding Plugin. This is why we need to have some of the options visible by "default".

            Looking forward to hear from you so we can test this properly, seem's like we're almost there!

            Thanks again for your help and keep up the terrific work!

  • Jude

    Hi again Urbano

    One question: Our goal is to show a default hidden option. Should we hide all except the ones we want to keep visible, even tho they are hidden by default?

    Yup this will be the case. Anything not in the list will be visible.

    As for this

    This could be a very helpful solution, but we'd like to start testing hiding the screen options button for all users except super-admins. I believe there's an option for this on Branding Plugin. This is why we need to have some of the options visible by "default".

    This can be be of help too if you want to take a code approach

    function restrict_admin() {
    if ( ! is_super_admin() ) {
      function remove_screen_options(){ __return_false;}
      add_filter('screen_options_show_screen', 'remove_screen_options');
    }
    }
    add_action( 'admin_init', 'restrict_admin', 1 );

    Cheers
    Jude

    • Urbano

      Hi there Jude, thanks again for your help!

      I have successfully created the mu-plugin and I can see it's working. All screen options are turned on by default.

      2) All the options entered here will stay hidden, the rest will be visible

      How do I see the name of each option, so I can add to the code?

      Just to confirm, I should add these options like in the following:

      $hidden = array( 'product_categorydiv' , 'mp-related-products-metabox' , 'other-option3', 'other-option4') ;

      add_filter('hidden_meta_boxes', 'hide_meta_box_attributes', 10, 2);

      I'm also curious, what does that 10 and 2 stands for?

      Thanks again for your precious help, we almost got it! :slight_smile:

  • Jude

    Hi again Urbano

    How do I see the name of each option, so I can add to the code?

    Different plugins name the meta boxes differently. You'd want to inspect the code for a foolproof way of getting this done.

    See screenshot

    I'm also curious, what does that 10 and 2 stands for?

    The 10 stands for the priority and the 2 stands for the number of args passed to the callback function.

    See this for more details

    https://developer.wordpress.org/reference/functions/add_filter/
    https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

    Cheers
    Jude

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.