Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Return result from wp_register_block_template function #66102

Conversation

AnmolVerma404
Copy link
Contributor

@AnmolVerma404 AnmolVerma404 commented Oct 14, 2024

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.

Copy link

github-actions bot commented Oct 14, 2024

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
Copy link

👋 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
Copy link
Contributor

@Aljullu Aljullu left a comment

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. 🙂

@kevin940726 kevin940726 added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Oct 15, 2024
@AnmolVerma404
Copy link
Contributor Author

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

@Aljullu
Copy link
Contributor

Aljullu commented Oct 15, 2024

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
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.

@kevin940726 kevin940726 removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Oct 16, 2024
@Aljullu
Copy link
Contributor

Aljullu commented Oct 16, 2024

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.

Copy link
Contributor

@ntsekouras ntsekouras left a comment

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
76 of 79 checks passed
@github-actions github-actions bot added this to the Gutenberg 19.5 milestone Oct 16, 2024
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