JavaScript for WordPress Forums Gutenberg Development Fatal error due to the register_block_type

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #39941
    mikemcalister
    Participant

    I’m also getting a fatal error due to the register_block_type. I’m not using the course files, I fired up my own plugin using CGB.

    Fatal error: Uncaught Error: Call to undefined function register_block_type()

    The error is referring to this snippet:

    register_block_type( 'example/example-testimonial-list', [
        'render_callback' => 'atomic_testimonial_list_render',
    ] );

    I’ve tried several different approaches but not sure why the function is returning as undefined. Anyone come up with a fix for this yet?

    #39944
    Zac Gordon
    Keymaster

    Hey @mikemcalister

    My guess is that this is because something like this is missing at the top of your code:

    
    const { registerBlockType } = wp.blocks;
    
    

    Is that it?

    #39949
    mikemcalister
    Participant

    @zgordon Thanks for the quick reply, Zac! I do have that in my block.js, but not in the php file where the register_block_type function is. Similarly to your course files, I have the function that renders the posts and the register_block_type call in the main php file.

    #39956
    Zac Gordon
    Keymaster

    Ooops! Sorry! I missed that you were talking on the PHP side for a dynamic block!

    Okay, that is weird. Can’t remember if I have come across it before..

    A couple shots in the dark here (some of them are real stretches, but hopefully something helps):

    – When you install the course example plugin (or another plugin with a dynamic block) does it give you the same error?
    – Does ‘example/example-testimonial-list’ exist as an actual block?
    – Is there possibly an error before that line?
    – The Gutenberg plugin is installed, right?
    – This function actually exists, right? atomic_testimonial_list_render

    Just some thoughts on starting points.. Please let me know what you find!

    #39964
    Zac Gordon
    Keymaster

    @mikemcalister Just pinging you here again since I moved this to it’s own thread 🙂

    #39972
    mikemcalister
    Participant

    Thanks for the checklist! I don’t get the error when working with the course plugin. I did get it when trying to rename the course plugin, similar to the other user. And now I’m having the error in my own plugin. Everything else looks good: block exists, Gutenberg installed, render function exists.

    Here’s a zip of the plugin I’m working on for reference. It should throw the error on activation. To fix the error, you can comment out register_block_type in src/custom-post-types/cpt.php, where I have the render function as well. I don’t expect you to review this plugin or spend a bunch of time hunting down a bug, but might be helpful if it turns out to be a common issue.

    #39980
    Zac Gordon
    Keymaster

    Hmmm this is weird. I am getting the same error as you. Played around a bit with it but not seeing a quick solution.

    I would suggest try posting up here https://github.com/WordPress/gutenberg/issues and please let us know what you find!!!

    #39982
    mikemcalister
    Participant

    Thanks for the quick look! I will definitely report back anything I find.

    #39984
    Zac Gordon
    Keymaster

    Also asked in #editor-js core channel for you 🙂

    #40008
    mikemcalister
    Participant

    Thanks for pinging the #editor-js channel! Someone came through with a suggestion to hook your block loading to the init action, as seen below. This has removed the register_block_type error!

    function load_your_blocks() {
    	/**
    	 * Initialize the blocks
    	 */
    	require_once plugin_dir_path( __FILE__ ) . 'src/init.php';
    }
    add_action( 'init', 'load_your_blocks' );
    #40037
    Zac Gordon
    Keymaster

    Ohhhh that is interesting. Makes sense and sounds like they might change the priority so loads all the time or at least resolve the issue when it loads into Core.

    Glad we got this sorted!!! Weird it only happens if your plugin name begins with something before G ;p

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