Skip to content

fix(ai-content-planner): skip empty paragraph insertion when post type has a block template#23401

Open
vraja-pro wants to merge 1 commit into
trunkfrom
23398-bug-insertfirstparagraph-in-ai-content-planner-adds-an-empty-paragraph-on-new-posts-with-block-templates
Open

fix(ai-content-planner): skip empty paragraph insertion when post type has a block template#23401
vraja-pro wants to merge 1 commit into
trunkfrom
23398-bug-insertfirstparagraph-in-ai-content-planner-adds-an-empty-paragraph-on-new-posts-with-block-templates

Conversation

@vraja-pro

@vraja-pro vraja-pro commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

When a post type registers a block template, insertFirstParagraph would race against Gutenberg's template application: the effect fired while blocks was still [] (before the template blocks landed), inserting a stray core/paragraph at position 0.

The fix reads the template from select('core').getPostType(), which is populated from the REST API preload cache synchronously before React renders, unlike getSettings().template which is written by BlockEditorProvider inside a useEffect (one tick too late).

Context

Summary

This PR can be summarized in the following changelog entry:

  • Fixes a bug where the AI Content Planner inserted an empty paragraph block before the template blocks when a post type had a block template registered.

Relevant technical choices:

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

Setup: Save the following snippet in snippets plugin of functions.php:

<?php
add_action( 'init', function () {
	$post_type_object = get_post_type_object( 'post' );
	$post_type_object->template = [
		[
			'core/group',
			[
				'align'     => 'left',
				'className' => 'post-content',
				'metadata'  => [ 'name' => 'Post Content' ],
				'layout'    => [
					'type'           => 'constrained',
					'contentSize'    => '1140px',
					'wideSize'       => '1140px',
					'inherit'        => true,
					'justifyContent' => 'center',
				],
			],
		],
	];
}, 20 );

With a block template (the bug fix)

  • Create a new post.
  • If you have recently created posts, Confirm the AI Content Planner banner appears above the core/group block and there is no paragraph block under it.
  • Otherwise check that there is no new paragraph block.

Without a block template (regression)
5. Remove or disable the PHP snippet.
6. Create a new post.
7. Confirm the AI Content Planner banner still appears at the top of the empty canvas.

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

QA can test this PR by following these steps:

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

Other environments

  • This PR also affects Shopify. I have added a changelog entry starting with [shopify-seo], added test instructions for Shopify and attached the Shopify label to this PR.
  • This PR also affects Yoast SEO for Google Docs. I have added a changelog entry starting with [yoast-doc-extension], added test instructions for Yoast SEO for Google Docs and attached the Google Docs Add-on label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.

Quality assurance

  • I have tested this code to the best of my abilities.
  • During testing, I had activated all plugins that Yoast SEO provides integrations for.
  • I have added unit tests to verify the code works as intended.
  • If any part of the code is behind a feature flag, my test instructions also cover cases where the feature flag is switched off.
  • I have written this PR in accordance with my team's definition of done.
  • I have checked that the base branch is correctly set.
  • I have run grunt build:images and committed the results, if my PR introduces or edits images or SVGs.

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label.
  • I have added my hours to the WBSO document.

Fixes #23398

…e has a block template

When a post type registers a block template, insertFirstParagraph would
race against Gutenberg's template application: the effect fired while
blocks was still [] (before the template blocks landed), inserting a
stray core/paragraph at position 0.

The fix reads the template from select('core').getPostType(), which is
populated from the REST API preload cache synchronously before React
renders, unlike getSettings().template which is written by
BlockEditorProvider inside a useEffect (one tick too late).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vraja-pro vraja-pro added the changelog: bugfix Needs to be included in the 'Bugfixes' category in the changelog label Jun 24, 2026
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 0

Coverage increased (+0.02%) to 53.801%

Details

  • Coverage increased (+0.02%) from the base build.
  • Patch coverage: 4 of 4 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 70037
Covered Lines: 37377
Line Coverage: 53.37%
Relevant Branches: 18125
Covered Branches: 10055
Branch Coverage: 55.48%
Branches in Coverage %: Yes
Coverage Strength: 42968.79 hits per line

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: bugfix Needs to be included in the 'Bugfixes' category in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: insertFirstParagraph in AI Content Planner adds an empty paragraph on new posts with block templates

2 participants