Instantly share code, notes, and snippets.

Embed
What would you like to do?
Inject non-acf fields during the rendering of acf-form
<?php
add_filter('acf/prepare_field', 'acf_prepare_field__inject_form_fields');
function acf_prepare_field__inject_form_fields( $field ) {
if ( ! is_admin() ) {
// look for the field AFTER where we want to inject something
// I used a switch to make it easier to react to multiple insertion points
switch ($field['key']) {
case 'field_123a456b78c90':
acf_form_inject__excerpt();
break;
default:
break;
}
}
return $field;
}
// render our custom form field and display any existing data in case we are editing an existing post
function acf_form_inject__excerpt() {
?>
<div class="acf-field acf-field-textarea" data-name="the_post_excerpt" data-type="textarea" data-key="the_post_excerpt">
<div class="acf-label">
<label for="acf-the_post_excerpt">Excerpt</label>
<p class="description">Excerpts are optional hand-crafted summaries of your content that can be used in your theme.</p>
</div>
<div class="acf-input">
<textarea id="the_post_excerpt" class="" name="jps[the_post_excerpt]" placeholder="" rows="8"><?php echo get_the_excerpt(); ?></textarea> </div>
</div>
<?php
}
?>
<?php
add_filter('acf/pre_save_post' , 'acf_pre_save_post__update_excerpt' );
function acf_pre_save_post__update_excerpt( $post_id ) {
// bail early if editing in admin
if( is_admin() ) {
return $post_id;
}
// if our non-acf excerpt field has data
if ( isset($_POST['jps']) && isset($_POST['jps']['the_post_excerpt']) ) {
$the_post = get_post($post_id);
$the_post->post_excerpt = $_POST['jps']['the_post_excerpt'];
wp_update_post( $the_post );
}
return $post_id;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment