• Resolved GreatBlakes

    (@greatblakes)


    I’ve followed several tutorials explaining how to get started with server-side blocks and all of them use render_callback inside of the register_block_type() function. I have been able to get it to appear in the editor, but when I view the post on the front-end the block doesn’t appear.

    I know it has nothing to do with the code *inside* the function because I’m simply returning a hard-coded <p> tag immediately.

    I thought maybe it was because I was trying it inside of an object-oriented class (trait to be specific), so I tried doing it procedurally and it worked. Now I need to get it to work with OOP, but I can’t tell if it’s where the function is called or how I’m calling it…

    Here’s are my two functions. gutenberg_latest_posts_block is being hooked at init (not shown). Again, this is working in the editor, but not the front-end.

    
    public function gutenberg_latest_posts_block(){
    	//Editor Script
    	wp_register_script(
    		'nebula-latest-posts-block',
    		get_template_directory_uri() . '/libs/Gutenberg/blocks/latest/latest.js',
    		array('wp-blocks', 'wp-element')
    	);
    
    	register_block_type('nebula/latest-posts', array(
    		'editor_script' => 'nebula-latest-posts-block',
    		'render_callback' => 'nebula_get_latest_post', //Not working. Also tried array($this, 'nebula_get_latest_post')
    	));
    }
    
    //Function for the front-end
    //Tried moving this outside of the class, but no luck there either.
    public function nebula_get_latest_post($attributes){
    	echo '<h1>testing</h1>';
    	return 'this would be posts! yay!'; //this never appears
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • Having exactly the same problem; I’m getting no output at all on the front end.

    It turned out that my problem was declaring the name of the block differently in the JS to the PHP (typo).

    The JS would successfully render on the edit screen; but when Gutenberg attempted to parse the content block, its attempt to match the block name in order to perform the correct callback silently failed.

    @piersb how to make sure that both names are the same? I think I’m facing the same issue.

    I pass editor_script to the register_block_type function, with the name of my js file, just registered. Actually, pretty much like @greatblakes code.. Where else should I be using it? Is there any way to debug the block callback initialization?

    I don’t know, why this topic is already resolved, but there is no answer to @greatblakes’s question.

    I came to the following solution:
    Because we’re in OOP, means inside a php class, the class needs to be addressed too, if we want to access the function. So we put the function into the “register_block_type” like this:

    register_block_type('nebula/latest-posts',[
      'render_callback' => [$this, 'nebula_get_latest_post']
    ]

    At least this way worked for me to get the front end output defined in the function (here “nebula_get_latest_post”).

    • This reply was modified 5 years, 9 months ago by ditshej.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Calling “render_callback” inside “register_block_type” in OOP’ is closed to new replies.