Skip to content

Fix: Return result from wp_register_block_template function#66102

Merged
ntsekouras merged 1 commit into
WordPress:trunkfrom
AnmolVerma404:fix/no_return_from_function_wp_unregister_block_template
Oct 16, 2024
Merged

Fix: Return result from wp_register_block_template function#66102
ntsekouras merged 1 commit into
WordPress:trunkfrom
AnmolVerma404:fix/no_return_from_function_wp_unregister_block_template

Conversation

@AnmolVerma404

@AnmolVerma404 AnmolVerma404 commented Oct 14, 2024

Copy link
Copy Markdown
Contributor

What?

Why?

  • The function wp_register_block_template is deprecated, and the legacy code that makes use of the return result might break, as no result is being returned. That's why it is necessary to return the result here.

How?

  • It simply returns the results from the newly renamed function register_block_template.

Testing Instructions

[Reffering example from #65958 PR]

  1. Add this code to an existing plugin or in a code snippet using the Code Snippets plugin:. If you use wp_register_block_template and utilize the return result, it will work as expected.
add_action(
	'init',
	function () {
		$template = wp_register_block_template(
			'gutenberg//plugin-template',
			array(
				'title'       => 'Plugin Template',
				'description' => 'A template registered by a plugin.',
				'content'     => '<!-- wp:template-part {"slug":"header","tagName":"header"} /--><!-- wp:group {"tagName":"main","layout":{"inherit":true}} --><main class="wp-block-group"><!-- wp:paragraph --><p>This is a plugin-registered template.</p><!-- /wp:paragraph --></main><!-- /wp:group -->',
			)
		);
		print_r( $template );
		add_action(
			'category_template_hierarchy',
			function () {
				return array( 'plugin-template' );
			}
		);
	}
);
  1. The print_r( $template ) statement should be visible on any page of the site. It should return a WP_Block_Template or WP_Error object.
if ( $template instanceof WP_Error ) {
    // Handle WP_Error object
}

if ( $template instanceof WP_Block_Template ) {
    // Handle WP_Block_Template object
} 
  1. Go to a post category in the frontend (ie: /category/test-category/) and verify the Plugin Template contents are rendered.
  2. Go to Appearance > Editor > Templates and verify the Plugin Template appears with the correct title and description.
  3. Make some edits to the template. Verify they are applied in the frontend.
  4. Revert the edits and verify edits are reverted in the frontend as well.
  5. Edit the previous code snippet adding these lines:
		unregister_block_template( 'gutenberg//plugin-template' );
  1. Verify the template no longer appears under Appearance > Editor > Templates and going to a post category in the frontend doesn't render it either.

@github-actions

github-actions Bot commented Oct 14, 2024

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: AnmolVerma404 <anmolverma404@git.wordpress.org>
Co-authored-by: Aljullu <aljullu@git.wordpress.org>
Co-authored-by: kevin940726 <kevin940726@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Oct 14, 2024
@github-actions

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @AnmolVerma404! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@Aljullu Aljullu added No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Type] Bug An existing feature does not function as intended labels Oct 15, 2024

@Aljullu Aljullu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this, @AnmolVerma404! Code changes looks good and it's testing well. I would only make a change to the testing step code snippet to something like this:

add_action(
	'init',
	function () {
		$template = wp_register_block_template(
			'gutenberg//plugin-template',
			array(
				'title'       => 'Plugin Template',
				'description' => 'A template registered by a plugin.',
				'content'     => '<!-- wp:template-part {"slug":"header","tagName":"header"} /--><!-- wp:group {"tagName":"main","layout":{"inherit":true}} --><main class="wp-block-group"><!-- wp:paragraph --><p>This is a plugin-registered template.</p><!-- /wp:paragraph --></main><!-- /wp:group -->',
			)
		);
		print_r( $template );
		add_action(
			'category_template_hierarchy',
			function () {
				return array( 'plugin-template' );
			}
		);
	}
);

And then add a step to verify the template object is correctly printed on the screen.

Besides that, LGTM. 🙂

@AnmolVerma404

Copy link
Copy Markdown
Contributor Author

@Aljullu , I've added the changes to the testing code snippet in the PR description ✅

@Aljullu

Aljullu commented Oct 15, 2024

Copy link
Copy Markdown
Contributor

Thanks for updating the testing steps, @AnmolVerma404!

@kevin940726 I see you added the "Backport to WP 6.7" label, but this specific code only exists in Gutenberg, not in WP core, so my understanding is that no porting is needed, right? Please let me know if I'm missing something.

@kevin940726

Copy link
Copy Markdown
Member

Hmm yeah! Sorry for holding this. Any idea why the original PR was backported though? I'll remove the label for now to unblock this.

@Aljullu

Aljullu commented Oct 16, 2024

Copy link
Copy Markdown
Contributor

No worries, @kevin940726! The original PR was renaming a couple of functions to remove the wp_ prefix. In Gutenberg we kept the old functions as deprecated while in WordPress core we simply renamed the functions (it was decided here: #61577 (comment)).

Given that this PR only modifies the deprecated functions, it only needs to be in Gutenberg and can't be ported to WP core.

Hope it makes sense! I will go ahead and merge it for now.

@ntsekouras ntsekouras left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@ntsekouras ntsekouras merged commit e015455 into WordPress:trunk Oct 16, 2024
@github-actions github-actions Bot added this to the Gutenberg 19.5 milestone Oct 16, 2024
karthick-murugan pushed a commit to karthick-murugan/gutenberg that referenced this pull request Nov 13, 2024
…ate function (WordPress#66102)

Co-authored-by: AnmolVerma404 <anmolverma404@git.wordpress.org>
Co-authored-by: Aljullu <aljullu@git.wordpress.org>
Co-authored-by: kevin940726 <kevin940726@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants