Skip to content

Instantly share code, notes, and snippets.

@mrfoxtalbot
Created May 30, 2023 11:35
Show Gist options
  • Save mrfoxtalbot/805a5cec0327997c926ebddf25a9b607 to your computer and use it in GitHub Desktop.
Save mrfoxtalbot/805a5cec0327997c926ebddf25a9b607 to your computer and use it in GitHub Desktop.
Prepopulate CPTs with a reusable block and convert it to a regular block automatically
function convert_reusable_block_to_regular_block($data, $postarr) {
// Check if it's a new custom post type
if ($data['post_type'] === 'your_custom_post_type' && $data['post_status'] === 'auto-draft') {
$reusable_block_post = get_page_by_title('your_reusable_block_name', OBJECT, 'wp_block');
// Check if the reusable block exists
if ($reusable_block_post !== null) {
$reusable_block_content = $reusable_block_post->post_content;
// Convert the reusable block into a regular block
$data['post_content'] = $reusable_block_content;
$data['post_status'] = 'draft';
// Remove the reusable block reference
$data['post_name'] = '';
// Update the post title if needed
// $data['post_title'] = 'Your New Post Title';
}
}
return $data;
}
add_filter('wp_insert_post_data', 'convert_reusable_block_to_regular_block', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment