Support » Fixing WordPress » How do I include some custom Gutenberg blocks in a custom theme?

  • I’m developing a WordPress theme with a very focused use case. I’m trying to help some local non-profits improve their web presence. I want to make the process as straightforward and with as little extra installation steps as possible. As a result I’m forgoing using ACF or any exterior plugins as much as possible.

    I have some custom Gutenberg blocks as a plugin. I want to include them in my theme. This is not working. How can I get it to register the gutengberg blogs from the theme?

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    I just followed the guide at https://www.advancedcustomfields.com/resources/blocks/

    As this relates to a commercial plugin, please use their official support channel. We feel they are best equipped to support their products.

    https://www.advancedcustomfields.com/support/

    Commercial products are not supported in these forums.

    @sterndata Sorry, I’m not asking about how to include ACF in my theme. I’m asking how to include Gutenberg blocks in my theme.

    I made a plugin that registers custom gutenberg blocks.

    I want to include it in my theme so that when the theme is install the blocks also become available without also having to install a separate plugin.

    I am aware why this is not the recommended strategy by WordPress as it carries with the risk of theme lockin and making a website break content when the theme is changed.

    But that’s not a concern in my use case and I am trying to make an install process more seamless. That’s my priority.

    Thanks.

    I have tried adding the following code to my function.php and it should work but it does not seem to be.

    I know the js and css files are all fine because the only thing I’m doing is copying the working plugin folder into my theme folder and then trying to enqueue the scripts from there.

    add_action( 'enqueue_block_editor_assets', 'block05editor_scripts' );
    
    function block05editor_scripts() {
    	wp_enqueue_script(
    		'block05editor_scripts',
    		get_stylesheet_directory() . '/Jackalope-Gutenberg/05-media-block/block.build.js',
    		array( 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-components' ),
    		''
    	);
    
    	wp_enqueue_style(
    		'block05editor_styles',
    		get_stylesheet_directory()  . '/Jackalope-Gutenberg/05-media-block/editor.css',
    		array(),
    		''
    	);
    }
    
    //- ADD BLOCK ASSETS FOR FRONTEND
    add_action( 'enqueue_block_assets', 'block05frontend_styles' );
    
    function block05frontend_styles() {
    	wp_enqueue_style(
    		'block05frontend_styles',
    		get_stylesheet_directory()  . '/Jackalope-Gutenberg/05-media-block/style.css',
    		array(),
    		''
    	);
    }
    • This reply was modified 4 months, 2 weeks ago by Jackalope Co..
    digitallecom

    (@digitallecom)

    @thedonquixotic – Greetings, did you manage to get this working. There is ABSOLUTELY no information on the interwebs about best practice for this, which is shocking really!!

    Am working on this myself so will of course post once / if I can get it working, but would of course appreciate any guidance in the interim!

    @ipstenu – I have long followed your posts, and know you to be very well connected within the WP community, so would be very grateful if yo had any ideas?

    Jackalope Co.

    (@thedonquixotic)

    @digitallecom Yes, I did manage to get this working. Check the pull request made by a guy who helped me on the github page for the project and you should be able to see the working code.

    mikehart100

    (@mikehart100)

    Hi there, I have the same problem setting up gutenberg register block in theme not a plugin.

    Could you tell me and everyone how you managed to get this working?

    Much appreciated

    mikehart100

    (@mikehart100)

    my problem was that there is a conflict with laravel mix

    @thedonquixotic Where exactly can I see the working code of .. Check the pull request made by a guy who helped me on the github page?

    I was not able to find it at GitHub. Thank you!

    • This reply was modified 1 month, 2 weeks ago by janhorna.

    @janhorna here’s a link to the commit: https://github.com/jcklpe/common-wp-theme/commit/3c87bbae4f606d9fec61a0d00e1c3e8096974418

    I actually ended up getting rid of that addition because they set it up such that it would have to load each individual block directly in the functions.php file, and once you try abstracting it to a single “block loader php” file to load a set of blocks at a time, it breaks and the behavior returns to not working. Since I don’t want to pollute my functions.php file with a bunch of individually declared blocks I opted for just using a plugin as per wordpress suggested practice. It’s annoying to not understand why it won’t work but it’s actually not really worth the struggle.

    @thedonquixotic Thank you. I also don’t understand why you can not include the custom blocks directly in your theme and avoid the dependancy on separate plugins.

    I don’t either but that’s how they’ve got it set up.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.