• Resolved Morshed Alam

    (@sumon1068)


    Hello,
    I run a multi author blog. I want authors not to be able to publish posts under some categories(Suppose ‘selected’, ‘editorial’). So I want to hide them in post editor.

    I tried this code:

    add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
    function yoursite_list_terms_exclusions( $exclusions, $args ) {
      global $pagenow;
      if (in_array($pagenow,array('post.php','post-new.php')) && 
         !current_user_can('manage_options')) {
        $exclusions = " {$exclusions} AND t.slug NOT IN ('selected','editorial')";
      }
      return $exclusions;
    }

    But it is not working. I used this code too:

    function hide_categories_for_specific_user( $exclusions, $args ){
    
    if (!current_user_can('manage_options') ) {
    
       // IDs of terms to be excluded
       $exclude_array = array("5","6"); // CHANGE THIS TO IDs OF YOUR TERMS
    
       // Generation of exclusion SQL code
       $exterms = wp_parse_id_list( $exclude_array );
       foreach ( $exterms as $exterm ) {
               if ( empty($exclusions) )
                       $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
               else
                       $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
       }
    
       // Closing bracket
       if ( !empty($exclusions) )
           $exclusions .= ')';
    
       // Return our SQL statement
       return $exclusions;
    
      }
    }
    
     // Finally hook up our filter
     add_filter( 'list_terms_exclusions', 'hide_categories_for_specific_user', 10, 2 );

    It works in the post editor but it breaks category widget in the front-end for non-admin users.

    I used several plugins for that. “Restrict Categories, Author category” etc. But no luck. I am currently hiding them using css :nth-of-type. But works for add new post but it breaks when authors edit their post.

    Can anyone help me?

Viewing 14 replies - 1 through 14 (of 14 total)
  • Hi there!

    You can use the Ultimate Category Excluder WordPress plugin.

    It has an option to hide categories based on user roles.

    You can learn more about the plugin here: https://bit.ly/3d9OkEg ??

    I believe the plug-in mentioned by @bizanimesh doesn’t filter categories on the WordPress Dashboard. It only works on the front-end. Please correct me if I’m wrong though.

    I was looking into the code provided by @sumon1068 on your original post for a few hours now. And from what I found, it and the other plug-ins that you mentioned doesn’t work on Gutenberg.

    For some reason, it starts working if I remove the if condition and have it as follows:

    add_filter( 'list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2 );
    function yoursite_list_terms_exclusions( $exclusions, $args ) {
            return $exclusions . " AND t.slug NOT IN ( 'selected', 'editorial' )";
    }

    But this removes the categories from the front-end as well. So I haven’t really found a solution for Gutenberg yet.

    Just wanted to post my findings.

    Edit: Adding an if statement anywhere on the code before changing the $exclusions causes it to stop working. Even if I add the if statement in another function and add it to admin_init and add the filter from there.

    • This reply was modified 4 years, 8 months ago by Thimal Wickremage. Reason: Added more information
    Moderator bcworkz

    (@bcworkz)

    In the code that works in the editor, revise the last part like so:

    // Closing bracket
       if ( !empty($exclusions) )
           $exclusions .= ')';
      }
       // Return the SQL statement
       return $exclusions;
    }

    (moved the if () statement closing curly brace so something is always returned)

    Thread Starter Morshed Alam

    (@sumon1068)

    @bcworkz ,

    Thank you for the reply. Despite revising last part, I am not able yet to get what I want.

    Yeah, it removes those two categories from the post editor, but it removes posts of those two categories from the front-end as well. I don’t want to remove posts from the front end.

    Can you help me with that?

    Hi @sumon1068

    I finally got this working, but it’s not an ideal solution. To remove the categories only from the editor after making the revision bcworkz suggested, you can change the first if statement to this:

    if ( defined( 'REST_REQUEST' ) && REST_REQUEST && !current_user_can( 'manage_options' ) ) {

    This is not an ideal solution because we’re hiding the categories on all REST requests without checking whether it’s on the WordPress Dashboard or not. But as long as your theme isn’t using REST requests to display categories on the front-end, this should work. And maybe someone will come up with a better solution.

    Also, methods like is_admin() or WP_Screen::is_block_editor to check whether we’re on the Dashboard doesn’t work here because of the way REST requests work.

    Thread Starter Morshed Alam

    (@sumon1068)

    @thimalw

    That’s great! Thank you very much. It works and as I stopped REST API access for non-logged in requests, it’s fine for me. You said maybe someone will come up with a better solution, so I would like to keep this thread open for a few days for others who are using REST API.

    By the way, I asked this question in WordPress StackExchange and found no solution there.

    You are awesome.

    Thanks again. Stay safe.

    Thread Starter Morshed Alam

    (@sumon1068)

    Addition:

    To cover up Quick Edit from All Posts I have modified the if statement to this:

    if ( ((defined( 'REST_REQUEST' ) && REST_REQUEST) or $GLOBALS['pagenow'] === 'edit.php' ) && !current_user_can( 'manage_options' ) ) {

    Moderator bcworkz

    (@bcworkz)

    As stated previously, we cannot know if an API request is from admin or not within the context of the request itself — i.e. is_admin() doesn’t work. Here’s a thought, though unverified: Perhaps we could check the server’s referrer value to determine if the request is from admin or not.

    Thread Starter Morshed Alam

    (@sumon1068)

    Solved.

    Thanks @sumon1068, I got this link after searching a lot, I did all things to hide categories in admin none of the plugin are working now for this function and finally.
    I made some modifications for those who want to use id instead of slug

    function hide_categories_for_specific_user( $exclusions, $args ){
    if ( ((defined( ‘REST_REQUEST’ ) && REST_REQUEST) or $GLOBALS[‘pagenow’] === ‘edit.php’ ) && !current_user_can( ‘manage_options’ ) ) {
    // IDs of terms to be excluded
    $exclude_array = array(“12″,”16″,”17”); // CHANGE THIS TO IDs OF YOUR TERMS
    // Generation of exclusion SQL code
    $exterms = wp_parse_id_list( $exclude_array );
    foreach ( $exterms as $exterm ) {
    if ( empty($exclusions) )
    $exclusions = ‘ AND ( t.term_id <> ‘ . intval($exterm) . ‘ ‘;
    else
    $exclusions .= ‘ AND t.term_id <> ‘ . intval($exterm) . ‘ ‘;
    }
    // Closing bracket
    if ( !empty($exclusions) )
    $exclusions .= ‘)’;
    // Return our SQL statement
    }
    return $exclusions;
    }

    // Finally hook up our filter
    add_filter( ‘list_terms_exclusions’, ‘hide_categories_for_specific_user’, 10, 2 );`

    Hi,

    I’m a real newbie in WordPress programming and you solution seems great for what I’m looking for (restrict post categories, when writing a post, by user or role).
    But where do I have to insert this code to make it works ?

    Thread Starter Morshed Alam

    (@sumon1068)

    Insert it to your theme,s function.php file.

    Hi,
    Thanks a lot.
    It works… But it only remove categories in dropdown menu from edit.php page.
    I would like to hide some categories in post.php. So I’ve tried to change $GLOBALS[‘pagenow’] === ‘edit.php’ to $GLOBALS[‘pagenow’] === ‘post.php’ but it always shows all categories in Gutenberg.
    With classical editor it works great…

    Rrrr…
    What is the difference between Gutenberg and Classical editor about terms management ?

    The Gutenberg (block editor) call via a script (api.fetch.min.js) this request :
    wp-json/wp/v2/categories?per_page=100&orderby=name&order=asc&_fields=id%2Cname%2Cparent&_locale=user
    to retrieve all available categories.

    I tried to run this command with “include” parameter as said in rest-api and so, I ran this request :
    wp-json/wp/v2/categories?per_page=100&orderby=name&order=asc&_fields=id%2Cname%2Cparent&_locale=user&include=55,99
    and the json output only shows the 2 categories I want to see !

    But how to add this include parameter to a hook ?

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Hide Some Categories in Post Editor’ is closed to new replies.