• Hi,

    I use a hook to exclude categories for specific users.
    It works great with classical editor, but with Gutenberg (block editor) it always shows all categories in the “Categories” menu at the right of the editor’s page.

    Why does “list_terms_exclusions” hook doesn’t work with gutenberg ?

    Thanks for your answers

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The hook still fires with the block editor. What code are you using to add exclusions?

    Thread Starter pwozniak89

    (@pwozniak89)

    Yes the hook fires, but categories management may be a little different as all categories still listed, not only 2 as my code wants…

    Here is my code :

    function hide_categories_for_specific_user( $exclusions, $args ){
    	if (($GLOBALS['pagenow'] === 'post.php' or $GLOBALS['pagenow'] === 'edit.php') and get_current_user_id() == 4) {
    		$allowed_id = array('55', '99');
    		$exterms = wp_parse_id_list( $allowed_id );
    		foreach ( $exterms as $exterm ) {
    			if ( empty($exclusions) ) {
    				$exclusions = " AND ( t.term_id = " . intval($exterm) . " ";
    			} else {
    				$exclusions .= " OR t.term_id = " . intval($exterm) . " ";
    			}
    		}
    		if ( !empty($exclusions) ) {
    			$exclusions .= ')';
    		}
    	}
    	return $exclusions;
    }
    
    add_filter( 'list_terms_exclusions', 'hide_categories_for_specific_user', 10, 2 );
    Moderator bcworkz

    (@bcworkz)

    Yeah, a little different. The categories are retrieved via the API so your ‘pagenow’ conditional prevents your code from being applied. You need an OR condition to check if REST_REQUEST constant is defined as true. Perhaps also check the referrer page to prevent other API requests from being restricted.

    Thread Starter pwozniak89

    (@pwozniak89)

    It works !

    Thank you very very very much.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Categories exlusion with gutenberg’ is closed to new replies.