Skip to content

Instantly share code, notes, and snippets.

@davidallenlewis
Created September 29, 2020 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidallenlewis/935d2e1a62b9c825401fc655657ec044 to your computer and use it in GitHub Desktop.
Save davidallenlewis/935d2e1a62b9c825401fc655657ec044 to your computer and use it in GitHub Desktop.
Save ACF metadata to post_content on save
// *******************************
// Future-Proof content by saving ACF content to post_content
// -------------------------------
function save_acf_rich_text_to_post_content( $post_id ) {
if ( get_post_type( $post_id ) == ( 'post' || 'page' ) ) {
// Get ACF flex content
if( have_rows('content_blocks') ) {
$post_content = '';
while ( have_rows('content_blocks') ) {
the_row();
if( get_row_layout() == 'banner' ) {
$post_content.= wp_get_attachment_image( get_sub_field('image_large') , 'banner_large' );
$post_content.= wp_get_attachment_image( get_sub_field('image_small') , 'banner_small' );
$post_content.= '<h4>' . get_sub_field('prefix') . '</h4>';
$post_content.= '<h2>' . get_sub_field('title') . '</h2>';
}
if( get_row_layout() == 'form' ) {
$form = get_sub_field('form');
$post_content.= '[ninja_form id=' . $form['id'] . ']';
}
if( get_row_layout() == 'header' ) {
$post_content.= '<h4>' . get_sub_field('prefix') . '</h4>';
$post_content.= '<h2>' . get_sub_field('title') . '</h2>';
$post_content.= '<p>' . get_sub_field('intro') . '</p>';
}
if( get_row_layout() == 'image' ) {
$post_content.= wp_get_attachment_image( get_sub_field('image') , 'medium' );
$post_content.= '<p>' . get_sub_field('description') . '</p>';
}
if( get_row_layout() == 'richtext' ) {
$post_content.= get_sub_field('content');
}
if( get_row_layout() == 'gallery' ) {
$gallery_images = get_sub_field('gallery');
$gallery_ids = '';
foreach ($gallery_images as $gallery_image) {
$gallery_ids.= $gallery_image['id'] . ',';
}
$post_content.= '[gallery ids="' . $gallery_ids . '"]';
}
if( get_row_layout() == 'icons' ) {
$icons = get_sub_field('icons');
foreach ($icons as $icon) {
$post_content.= '<h6>' . $icon['label'] . '</h6>';
$post_content.= '<p>' . $icon['excerpt'] . '</p>';
}
}
}
}
// Save ACF WYSIWYG content to post_content
$my_post = array(
'ID' => $post_id,
'post_content' => $post_content,
);
wp_update_post($my_post);
}
if ( get_post_type( $post_id ) == ( 'attraction' || 'package' || 'program' || 'group' ) ) {
// Get ACF flex content
if( get_field('details') ) {
$post_content.= get_field('details');
}
// Save ACF WYSIWYG content to post_content
$my_post = array(
'ID' => $post_id,
'post_content' => $post_content,
);
wp_update_post($my_post);
}
}
add_action('acf/save_post', 'save_acf_rich_text_to_post_content', 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment