<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wordpress &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:33:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Wordpress &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>WordPress: Creating Gutenberg-Block Compatible Posts Using Rest API</title>
		<link>https://myshittycode.com/2023/01/03/wordpress-creating-gutenberg-block-compatible-posts-using-rest-api/</link>
					<comments>https://myshittycode.com/2023/01/03/wordpress-creating-gutenberg-block-compatible-posts-using-rest-api/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 03 Jan 2023 17:27:35 +0000</pubDate>
				<category><![CDATA[Server]]></category>
		<category><![CDATA[Development Tools]]></category>
		<category><![CDATA[Gutenberg]]></category>
		<category><![CDATA[Rest]]></category>
		<category><![CDATA[Wordpress]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=1988</guid>

					<description><![CDATA[<p>This article shows how you can dynamically create new WordPress posts using REST API that is compatible with Gutenberg blocks. When done correctly, there is no need to manually convert the content from Classic Editor to Gutenberg Block Editor, or fix incorrectly converted blocks. This is a big time saver when you plan to create [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2023/01/03/wordpress-creating-gutenberg-block-compatible-posts-using-rest-api/">WordPress: Creating Gutenberg-Block Compatible Posts Using Rest API</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This article shows how you can dynamically create new WordPress posts using REST API that is compatible with Gutenberg blocks. When done correctly, there is no need to manually convert the content from Classic Editor to Gutenberg Block Editor, or fix incorrectly converted blocks. This is a big time saver when you plan to create many posts using REST API.</p>



<p>This article also assumes you know how to dynamically <a href="https://developer.wordpress.org/rest-api/reference/posts/#create-a-post" target="_blank" rel="noopener">create a WordPress post using REST API</a>.</p>



<p>For simplicity, the cURL command is used instead of a programming language like PHP or Python.</p>



<h2 class="wp-block-heading" id="problem">PROBLEM</h2>



<p>Let&#8217;s assume we want to create a post with one paragraph and 2 columns, each containing a paragraph too. When creating a similar structure directly using Gutenberg Block Editor, the generated source code like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;p&gt;hello&lt;/p&gt;
&lt;div class=&quot;is-layout-flex wp-block-columns&quot;&gt;
&lt;div class=&quot;is-layout-flow wp-block-column&quot;&gt;
&lt;p&gt;left&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;is-layout-flow wp-block-column&quot;&gt;
&lt;p&gt;right&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</pre></div>


<p>Using the same structure, we are going to create the same post using REST API instead:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
export auth=...
export wp_url=...

content=$(echo &#039;
&lt;p&gt;hello&lt;/p&gt;
&lt;div class=&quot;is-layout-flex wp-block-columns&quot;&gt;
&lt;div class=&quot;is-layout-flow wp-block-column&quot;&gt;
&lt;p&gt;left&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;is-layout-flow wp-block-column&quot;&gt;
&lt;p&gt;right&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&#039; | jq -Rsa .)

curl -H &quot;Content-Type: application/json&quot; \
  -H &quot;Authorization: Basic $auth&quot; \
  -X POST &quot;$wp_url/wp-json/wp/v2/posts&quot; \
  -d &quot;{\&quot;title\&quot;:\&quot;test\&quot;, \&quot;content\&quot;: $content}&quot;

</pre></div>


<p>When opening the newly created post in edit mode, the post uses Classic Editor, and not Gutenberg Block Editor. </p>



<figure class="wp-block-image aligncenter size-large is-resized"><img fetchpriority="high" decoding="async" src="https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_22_49_AM-1024x445.png?x45560" alt="" class="wp-image-1991" width="768" height="334" srcset="https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_22_49_AM-1024x445.png 1024w, https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_22_49_AM-300x130.png 300w, https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_22_49_AM-768x334.png 768w, https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_22_49_AM.png 1352w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>When attempting to convert the post to Gutenberg blocks&#8230;</p>



<figure class="wp-block-image aligncenter size-large is-resized"><img decoding="async" src="https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_23_02_AM-1024x362.png?x45560" alt="" class="wp-image-1992" width="768" height="272" srcset="https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_23_02_AM-1024x362.png 1024w, https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_23_02_AM-300x106.png 300w, https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_23_02_AM-768x271.png 768w, https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_23_02_AM.png 1348w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>Some blocks are converted successfully. However, most of the complex or nested blocks are converted to Custom HTML blocks instead. This is because it is not smart enough to know which Gutenberg blocks to use.</p>



<figure class="wp-block-image aligncenter size-large is-resized"><img decoding="async" src="https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.23.16-AM-1024x604.png?x45560" alt="" class="wp-image-1993" width="768" height="453" srcset="https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.23.16-AM-1024x604.png 1024w, https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.23.16-AM-300x177.png 300w, https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.23.16-AM-768x453.png 768w, https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.23.16-AM.png 1336w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>This becomes problematic and time-consuming when you have to manually convert each post to use Gutenberg Block Editor, and fix sections that failed to convert to the correct blocks.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading" id="solution">SOLUTION</h2>



<p>If you know you are going to create future new posts using known Gutenberg blocks (ex: paragraph, column, image, etc), the easiest approach is to manually create a temporary post using Gutenberg Block Editor first.</p>



<p>Using the same example, the paragraph and 2-column structure are manually created using Gutenberg Block Editor.</p>



<figure class="wp-block-image aligncenter size-large is-resized"><img loading="lazy" decoding="async" src="https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.26.09-AM-1024x359.png?x45560" alt="" class="wp-image-1994" width="768" height="269" srcset="https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.26.09-AM-1024x359.png 1024w, https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.26.09-AM-300x105.png 300w, https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.26.09-AM-768x270.png 768w, https://myshittycode.com/wp-content/uploads/2023/01/Screen-Shot-2023-01-03-at-10.26.09-AM.png 1362w" sizes="auto, (max-width: 768px) 100vw, 768px" /></figure>



<p>Now, click on the kebab on the top right, then select <strong>Code editor</strong>.</p>



<figure class="wp-block-image aligncenter size-full is-resized"><img loading="lazy" decoding="async" src="https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_26_28_AM.png?x45560" alt="" class="wp-image-1995" width="468" height="746" srcset="https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_26_28_AM.png 624w, https://myshittycode.com/wp-content/uploads/2023/01/Screen_Shot_2023-01-03_at_10_26_28_AM-188x300.png 188w" sizes="auto, (max-width: 468px) 100vw, 468px" /></figure>



<p>This will display the source code in block format, which looks like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;!-- wp:paragraph --&gt;
&lt;p&gt;hello&lt;/p&gt;
&lt;!-- /wp:paragraph --&gt;

&lt;!-- wp:columns --&gt;
&lt;div class=&quot;wp-block-columns&quot;&gt;&lt;!-- wp:column --&gt;
&lt;div class=&quot;wp-block-column&quot;&gt;&lt;!-- wp:paragraph --&gt;
&lt;p&gt;left&lt;/p&gt;
&lt;!-- /wp:paragraph --&gt;&lt;/div&gt;
&lt;!-- /wp:column --&gt;

&lt;!-- wp:column --&gt;
&lt;div class=&quot;wp-block-column&quot;&gt;&lt;!-- wp:paragraph --&gt;
&lt;p&gt;right&lt;/p&gt;
&lt;!-- /wp:paragraph --&gt;&lt;/div&gt;
&lt;!-- /wp:column --&gt;&lt;/div&gt;
&lt;!-- /wp:columns --&gt;
</pre></div>


<p>Now that we know what the format looks like, we can automate the post creations using REST API.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
content=$(echo &#039;
&lt;!-- wp:paragraph --&gt;
&lt;p&gt;hello&lt;/p&gt;
&lt;!-- /wp:paragraph --&gt;

&lt;!-- wp:columns --&gt;
&lt;div class=&quot;wp-block-columns&quot;&gt;&lt;!-- wp:column --&gt;
&lt;div class=&quot;wp-block-column&quot;&gt;&lt;!-- wp:paragraph --&gt;
&lt;p&gt;left&lt;/p&gt;
&lt;!-- /wp:paragraph --&gt;&lt;/div&gt;
&lt;!-- /wp:column --&gt;

&lt;!-- wp:column --&gt;
&lt;div class=&quot;wp-block-column&quot;&gt;&lt;!-- wp:paragraph --&gt;
&lt;p&gt;right&lt;/p&gt;
&lt;!-- /wp:paragraph --&gt;&lt;/div&gt;
&lt;!-- /wp:column --&gt;&lt;/div&gt;
&lt;!-- /wp:columns --&gt;
&#039; | jq -Rsa .)


curl -H &quot;Content-Type: application/json&quot; \
  -H &quot;Authorization: Basic $auth&quot; \
  -X POST &quot;$wp_url/wp-json/wp/v2/posts&quot; \
  -d &quot;{\&quot;title\&quot;:\&quot;test\&quot;, \&quot;content\&quot;: $content}&quot;
</pre></div>


<p>Finally, we can create methods/functions with our favorite programming language to generate these blocks, which will be fed to the REST API to create a new WordPress post that is 100% compatible with Gutenberg blocks. </p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2023/01/03/wordpress-creating-gutenberg-block-compatible-posts-using-rest-api/">WordPress: Creating Gutenberg-Block Compatible Posts Using Rest API</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2023/01/03/wordpress-creating-gutenberg-block-compatible-posts-using-rest-api/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1988</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-19 12:15:47 by W3 Total Cache
-->