[Resolved] Restrict GenerateBlocks by user role

Home Forums Support [Resolved] Restrict GenerateBlocks by user role

Home Forums Support Restrict GenerateBlocks by user role

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1384516
    PHILIP

    On a site with many authors, I’d like to force blog posts to only use Gutenberg blocks.

    Is there a way to restrict GenerateBlocks by user role, or to pages only?

    Thanks
    Philip.

    #1384926
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Just to confirm, you don’t want any of the blocks to show up/be usable? What if someone without the necessary role opens a page which has GenerateBlocks on it already?

    #1384970
    PHILIP

    Hello Tom
    In the same way: More Options > Tools > Block Manager can disable GenerateBlocks. I want to do that so authors can’t turn it back on again.

    If someone with author role writes a POST, I want to hide GenerateBlocks so they are limited to writing their post using Gutenberg blocks.

    If someone with administrator role writes a PAGE, they can use GenerateBlocks.

    So GenerateBlocks will only be used on pages written by users with administrator role.

    GenerateBlocks is incredibly powerful and it lets every author design every post unequally. But on this particular site it’s important that all posts focus on the content, not the design.

    I’m not sure if this is a realistic goal. If it is, it would be useful.

    PS. I’ve been trying out 1.11.0 The combination of GenerateBlocks with hooks and conditional tags really takes GeneratePress into a different league.

    Thanks
    Philip.

    #1385074
    Tom
    Lead Developer
    Lead Developer

    Not 100% sure this will work, but worth a shot:

    add_filter( 'allowed_block_types', function( $allowed_blocks ) {
        global $post;
    
        if ( current_user_can( 'manage_options' ) && 'page' === $post->post_type ) {
            return $allowed_blocks;
        }
    
        return array_diff(
            $allowed_blocks,
            array(
                'generateblocks/container',
                'generateblocks/button-container',
                'generateblocks/button',
                'generateblocks/grid',
                'generateblocks/headline',
            )
        );
    } );

    Awesome to hear you’re enjoying 1.11! ๐Ÿ™‚

    #1385324
    PHILIP

    Tried on a local host, adding the code to the end of functions dot php. And tried using your recommend code snippets plugin…
    https://docs.generatepress.com/article/adding-php/

    But I couldn’t get it to work.

    If this is a big job, consider it a possible feature request, not an immediate and pressing need.

    I can get close to what I need via the top right more options button, block manager option.

    PS. I’m not liking 1.11.0, I’m loving it. You mentioned there’s more to come, I can’t wait to see what.

    #1385997
    Tom
    Lead Developer
    Lead Developer

    I’ll play with this more – I agree that it could be a good feature.

    Thank you! ๐Ÿ™‚

    #1386013
    PHILIP

    Before Gutenberg I had a “No more than five fancy pages” rule, which served me well.

    I’ll mark the topic as resolved. If you come up with a solution in the short term, I’m sure you can reopen the topic to post more code.

    Thanks
    Philip.

    #1486380
    Simon

    Hi Tom, Philip

    I was looking for the same thing. I am using the excellent GenerateBlocks, along with the standard WP blocks in the block editor, to write pages, but I wanted to offer a simple selection of blocks for inexperienced authors writing posts.

    Misha Rudrastyh has written a useful guide: https://rudrastyh.com/gutenberg/remove-default-blocks.html on removing default blocks. It includes the codes for all the WP blocks, and enables you to choose which ones to display.

    Adapting Misha’s code, the code snippet below works to offer just 5 simple blocks to authors writing posts, while keeping all the blocks available for writing pages.

    (The ability of the WP image block to align an image to float alongside a text block, convinced me that our 1200px max-width posts didn’t need a columns block too.)

    Hope it is useful to others.
    Simon

    add_filter( 'allowed_block_types', 'misha_allowed_block_types', 10, 2 );
     
    function misha_allowed_block_types( $allowed_blocks, $post ) {
    
    	if( $post->post_type === 'post' ) {
    	$allowed_blocks = array(
    		'core/image',
    		'core/paragraph',
    		'core/heading',
    		'core/list',
    		'core/quote'
    	);
    	}
    	return $allowed_blocks;
    } 
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.