Skip to content

Instantly share code, notes, and snippets.

@cameronjonesweb
Last active November 28, 2025 04:33
Show Gist options
  • Select an option

  • Save cameronjonesweb/ed7ba31063b4d50e131dc5c69d6e92eb to your computer and use it in GitHub Desktop.

Select an option

Save cameronjonesweb/ed7ba31063b4d50e131dc5c69d6e92eb to your computer and use it in GitHub Desktop.
Automatically migrate ACF flexible content layouts to blocks. View the assocated slides from Web Directions Enqueue 2025 here: https://speakerdeck.com/cameronjonesweb/how-we-automated-migrating-thousands-of-blog-posts-from-acf-flexible-content-to-the-block-editor
<?php
/**
* Plugin Name: Convert Flex Content to Blocks
* Author: Cameron Jones
* Author URI: https://shortiedesigns.com
*/
function cameronjonesweb_convert_flex_content_to_blocks( $post_id, $user_id = null ) {
if ( ! is_null( $user_id ) ) {
wp_set_current_user( $user_id );
}
$content = '';
$flex_content = get_field( 'flexible_content', $post_id );
if ( ! empty( $flex_content ) ) {
foreach ( $flex_content as $fc ) {
if ( 'content_block' === $fc['acf_fc_layout'] ) {
if ( ! empty( $fc['class'] ) ) {
// Has a class, make it a group block.
$content .= sprintf(
'<!-- wp:group {"className":"%2$s","layout":{"type":"constrained"}} --><div class="wp-block-group %2$s"><!-- wp:freeform -->%1$s<!-- /wp:freeform --></div><!-- /wp:group -->',
$fc['class'],
$fc['content']
);
} else {
$content .= sprintf(
'<!-- wp:freeform -->%1$s<!-- /wp:freeform -->',
$fc['content']
);
}
} elseif ( 'horizontal_line' === $fc['acf_fc_layout'] ) {
$content .= '<!-- wp:separator --><hr class="wp-block-separator"/><!-- /wp:separator -->';
} elseif ( 'half_and_half' === $fc['acf_fc_layout'] ) {
$content .= sprintf(
'<!-- wp:columns {"className":"%3$s"} --><div class="wp-block-columns %3$s"><!-- wp:column --><div class="wp-block-column"><!-- wp:freeform -->%1$s<!-- /wp:freeform --></div><!-- /wp:column --><!-- wp:column --><div class="wp-block-column"><!-- wp:freeform -->%2$s<!-- /wp:freeform --></div><!-- /wp:column --></div><!-- /wp:columns -->',
$fc['block_1'],
$fc['block_2'],
$fc['class'],
);
} elseif ( 'two_thirds_one_third' === $fc['acf_fc_layout'] ) {
$content .= sprintf(
'<!-- wp:cover {"url":"%5$s","id":%4$s,"dimRatio":0,"isDark":false,"className":"%3$s"} --><div class="wp-block-cover is-light %3$s"><span aria-hidden="true" class="wp-block-cover__background has-background-dim-0 has-background-dim"></span><img class="wp-block-cover__image-background wp-image-%4$s" alt="" src="%5$s" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:columns --><div class="wp-block-columns"><!-- wp:column {"width":"66.66%%"} --><div class="wp-block-column" style="flex-basis:66.66%%"><!-- wp:freeform -->%1$s<!-- /wp:freeform --></div><!-- /wp:column --><!-- wp:column {"width":"33.33%%"} --><div class="wp-block-column" style="flex-basis:33.33%%"><!-- wp:freeform -->%2$s<!-- /wp:freeform --></div><!-- /wp:column --></div><!-- /wp:columns --></div></div><!-- /wp:cover -->',
$fc['block_1'],
$fc['block_2'],
$fc['class'],
$fc['background_image']['ID'],
$fc['background_image']['url']
);
}
}
}
wp_update_post(
array(
'ID' => $post_id,
'post_content' => $content,
'post_modified' => get_post_modified_time( 'U', false, $post_id ),
'post_modified_gmt' => get_post_modified_time( 'U', true, $post_id ),
)
);
}
add_action(
'convert_flex_content_to_blocks',
function( $page = 0, $user_id = null ) {
$posts_per_page = 10;
$posts = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'fields' => 'ids',
'paged' => $page,
)
);
if ( ! empty( $posts->have_posts() ) ) {
while ( $posts->have_posts() ) {
$posts->the_post();
cameronjonesweb_convert_flex_content_to_blocks( get_the_ID(), $user_id );
}
wp_reset_postdata();
if ( ( $page * $posts_per_page ) < $posts->found_posts ) {
wp_schedule_single_event( time() + 10, 'convert_flex_content_to_blocks', array( $page + 1, $user_id ) );
}
}
exit();
},
10,
2
);
add_action(
'admin_menu',
function() {
add_management_page(
'Blog Migration',
'Blog Migration',
'manage_options',
'blog-migration',
function() {
if ( isset ( $_POST['cameronjonesweb_migrate_blog_posts'] ) && ! empty( $_POST['cameronjonesweb_migrate_blog_posts'] ) ) {
if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['cameronjonesweb_migrate_blog_posts'] ) ), 'cameronjonesweb_migrate_blog_posts' ) ) {
wp_schedule_single_event( time(), 'convert_flex_content_to_blocks', array( 0, get_current_user_id() ) );
echo '<div id="message" class="updated"><p>Blog Migration commenced.</p></div>';
} else {
echo '<div id="message" class="error"><p>Invalid nonce.</p></div>';
}
}
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<h3>Bulk update content</h3>
<form method="post">
<table class="form-table">
<tbody>
<tr>
<td>
<?php wp_nonce_field( 'cameronjonesweb_migrate_blog_posts', 'cameronjonesweb_migrate_blog_posts' ); ?>
<input type="submit" name="submit" value="Convert Blog Post Flexible Content" class="button button-primary">
</td>
</tr>
</tbody>
</table>
</form>
</div>
<?php
}
);
}
);
add_filter(
'post_row_actions',
function ( $actions, $post ) {
if ( 'post' === get_post_type( $post ) ) {
$actions['convert_flex_content_to_blocks'] = sprintf(
'<a href="%1$s">Convert to blocks</a>',
esc_url(
sprintf(
admin_url( 'edit.php?post_id=%1$s&_wpnonce=%2$s&action=convert_flex_content_to_blocks' ),
get_the_ID( $post ),
wp_create_nonce( 'convert_flex_content_to_blocks' )
)
)
);
}
return $actions;
},
10,
2
);
add_action(
'load-edit.php',
function() {
if ( isset( $_GET['action'] ) && 'convert_flex_content_to_blocks' === $_GET['action'] ) {
if ( wp_verify_nonce( wp_unslash( sanitize_text_field( $_GET['_wpnonce'] ) ), 'convert_flex_content_to_blocks' ) ) {
$post_id = wp_unslash( sanitize_text_field( $_GET['post_id'] ) );
cameronjonesweb_convert_flex_content_to_blocks( $post_id );
}
wp_redirect( wp_get_referer() );
die();
}
}
);
{
"key": "group_55c840f190833",
"title": "Flexible Content Post",
"fields": [
{
"key": "field_55c8410e72d46",
"label": "Flexible content",
"name": "flexible_content",
"aria-label": "",
"type": "flexible_content",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"button_label": "Add Row",
"min": "",
"max": "",
"layouts": [
{
"key": "55c8411ed2160",
"name": "heading_block",
"label": "Heading block",
"display": "row",
"sub_fields": [
{
"key": "field_55c8413572d47",
"label": "Background image",
"name": "background_image",
"aria-label": "",
"type": "image",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"return_format": "array",
"preview_size": "thumbnail",
"library": "all",
"min_width": "",
"min_height": "",
"min_size": "",
"max_width": "",
"max_height": "",
"max_size": "",
"mime_types": ""
},
{
"key": "field_55c8415872d48",
"label": "Title",
"name": "title",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_55d16ae844903",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55c841f09cea8",
"name": "three_thirds",
"label": "Three thirds",
"display": "row",
"sub_fields": [
{
"key": "field_55c842019cea9",
"label": "Block 1",
"name": "block_1",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c842139ceaa",
"label": "Block 2",
"name": "block_2",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c842199ceab",
"label": "Block 3",
"name": "block_3",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55d16af544904",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55c84330e6362",
"name": "one_third_two_thirds",
"label": "One third two thirds",
"display": "row",
"sub_fields": [
{
"key": "field_55c84330e6363",
"label": "Block 1",
"name": "block_1",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c84330e6364",
"label": "Block 2",
"name": "block_2",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c954cfeb4eb",
"label": "Background image",
"name": "background_image",
"aria-label": "",
"type": "image",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"return_format": "array",
"preview_size": "thumbnail",
"library": "all",
"min_width": "",
"min_height": "",
"min_size": "",
"max_width": "",
"max_height": "",
"max_size": "",
"mime_types": ""
},
{
"key": "field_55d16afb44905",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55c845d36e08b",
"name": "two_thirds_one_third",
"label": "Two thirds one third",
"display": "row",
"sub_fields": [
{
"key": "field_55c845d36e08c",
"label": "Block 1",
"name": "block_1",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c845d36e08d",
"label": "Block 2",
"name": "block_2",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c9536a562a2",
"label": "Background image",
"name": "background_image",
"aria-label": "",
"type": "image",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"return_format": "array",
"preview_size": "thumbnail",
"library": "all",
"min_width": "",
"min_height": "",
"min_size": "",
"max_width": "",
"max_height": "",
"max_size": "",
"mime_types": ""
},
{
"key": "field_55d16b0144906",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55c845f26e08e",
"name": "half_and_half",
"label": "Half and half",
"display": "row",
"sub_fields": [
{
"key": "field_55c845f26e08f",
"label": "Block 1",
"name": "block_1",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c845f26e090",
"label": "Block 2",
"name": "block_2",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55d16b0744907",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55c84384e6366",
"name": "outline_block",
"label": "Outline block",
"display": "row",
"sub_fields": [
{
"key": "field_55c84397e6367",
"label": "Content",
"name": "content",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c843b0e6368",
"label": "Outline color",
"name": "outline_color",
"aria-label": "",
"type": "text",
"instructions": "Hex color, for example #f9f9f9",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_55d16b0d44908",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55c844112abda",
"name": "content_block",
"label": "Content block",
"display": "row",
"sub_fields": [
{
"key": "field_55c844172abdb",
"label": "Content",
"name": "content",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55d16b1344909",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55c8455dc9a85",
"name": "content_block_with_background",
"label": "Content block with background",
"display": "row",
"sub_fields": [
{
"key": "field_55c84572c9a86",
"label": "Content",
"name": "content",
"aria-label": "",
"type": "wysiwyg",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1,
"delay": 0
},
{
"key": "field_55c84595c9a87",
"label": "Content class",
"name": "content_class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_55c8459cc9a88",
"label": "Background image",
"name": "background_image",
"aria-label": "",
"type": "image",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"return_format": "array",
"preview_size": "thumbnail",
"library": "all",
"min_width": "",
"min_height": "",
"min_size": "",
"max_width": "",
"max_height": "",
"max_size": "",
"mime_types": ""
},
{
"key": "field_55d16b1b4490a",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55c948480c7ae",
"name": "call_to_action",
"label": "Call to action",
"display": "row",
"sub_fields": [
{
"key": "field_55c948530c7af",
"label": "Button text",
"name": "button_text",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_55c948b60c7b0",
"label": "Button URL",
"name": "button_url",
"aria-label": "",
"type": "page_link",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"post_type": [],
"taxonomy": [],
"allow_null": 0,
"multiple": 0,
"allow_archives": 1
},
{
"key": "field_55d53e43e2c75",
"label": "Custom URL",
"name": "custom_url",
"aria-label": "",
"type": "text",
"instructions": "Use this field to override Button URL field",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_55d16b214490b",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
},
{
"key": "55d177de053e1",
"name": "horizontal_line",
"label": "Horizontal line",
"display": "row",
"sub_fields": [],
"min": "",
"max": ""
},
{
"key": "56a06d15e0946",
"name": "pricing_table",
"label": "Pricing table",
"display": "row",
"sub_fields": [
{
"key": "field_56a06d27e0947",
"label": "Set",
"name": "set",
"aria-label": "",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"collapsed": "",
"min": 1,
"max": 3,
"layout": "block",
"button_label": "Add Set",
"rows_per_page": 20,
"sub_fields": [
{
"key": "field_56a06d4be0948",
"label": "Price row",
"name": "price_row",
"aria-label": "",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"collapsed": "",
"min": 0,
"max": 0,
"layout": "table",
"button_label": "Add Row",
"rows_per_page": 20,
"parent_repeater": "field_56a06d27e0947",
"sub_fields": [
{
"key": "field_56a06d78e0949",
"label": "Content",
"name": "content",
"aria-label": "",
"type": "textarea",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"maxlength": "",
"rows": "",
"new_lines": "wpautop",
"readonly": 0,
"disabled": 0,
"parent_repeater": "field_56a06d4be0948"
},
{
"key": "field_56a06d8ee094a",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0,
"parent_repeater": "field_56a06d4be0948"
}
]
}
]
},
{
"key": "field_56a1c6615c1ac",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
],
"min": "",
"max": ""
}
]
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "post"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": [
"the_content"
],
"active": true,
"description": "",
"show_in_rest": 0,
"modified": 1762953151
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment