ACF front end form to update term

坚强是说给别人听的谎言 提交于 2019-12-24 05:54:56

问题


I want to use ACF frontend form function to create a form with custom fields

I see this issue for create new term, @Alhana ACF front end form to create term but I want to generate the form with old data


回答1:


Well, i didn't see that question, but if it's still actual, here's a solution. First of all, make sure you have ACF group, linked to your taxonomy. You will need ID of this group, it can be found in url on group edit page, for example:

http://site.ru/wp-admin/post.php?post=340&action=edit

In this case group ID is 340. If you don't want to use hardcoded ID (if your groups are changing from time to time), you can get it, using group name (in this example group name is Technic CPT):

global $wpdb;
$group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );

Then, you'll need ID of term you're updating. I think, it's not nesessary to write about getting it since it's WP basics :) You'll end with something like this:

$term_id = 405;

And finally, you'll need your taxonomy's slug. In this example it's technic. So, let's render our form!

acf_form_head();
$acf_form_args = array(
  'id' => 'technic_edit_form',
  'post_id' => 'technic_'.$term_id,
  'form' => true,
  'submit_value' => 'Update technic',
  'field_groups' => array($group_ID),
  'updated_message' => 'Technic is updated!';
);
acf_form( $acf_form_args ); 

Now your term's custom fields will be shown in this form. But to save term data after editing you'll need to add some more code. ACF form assumes that you're saving post data, we'll add some logic to detect saving data for term.

add_filter( 'acf/pre_save_post', 'acf_handle_form_save', 10, 1 );
function acf_handle_form_save( $post_id ) {
  // Function accepts id of object we're saving.
  // All WordPress IDs are unique so we can use this to check which object it is now.
  // We'll try to get term by id.
  // We'll get term id with added taxonomy slug, for example 'technic_405'.
  // For checking term existence we must cut out this slug. 
  $cut_post_id = str_replace( 'technic_', '', $post_id );
  $test_tax_term = get_term_by( 'id', $cut_post_id, 'technic' );
  // If $test_tax_term is true - we are saving taxonomy term.
  // So let's change form behaviour to saving term instead of post.
  if ( $test_tax_term ) :
    // Get array of fields, attached to our taxonomy
    global $wpdb;
    $group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );
    $acf_fields = acf_get_fields_by_id( $group_ID );
    // Then sanitize fields from $_POST
    // All acf fields will be in $_POST['acf']
    foreach ( $acf_fields as $acf_field ) :
      $$acf_field[ 'name' ] = trim( esc_attr( strip_tags( $_POST[ 'acf' ][ $acf_field[ 'key' ] ] ) ) );
    endforeach;
    // We need to have some fields in our group, which are just duplicates of standard term fields: name, slug, description.
    // In this example it's only one field - term name, called 'technic_name'.
    $name = 'technic_name';
    // Update base term info, in this example - only name.
    $term = wp_update_term( $cut_post_id, 'technic', array( 'name' => $$name ) );
    // If all is correct, update custom fields:
    if ( !is_wp_error( $term ) ) :
      foreach ( $acf_fields as $acf_field ) :
        update_field( $acf_field[ 'name' ], $$acf_field[ 'name' ], 'technic_' . $cut_post_id );
      endforeach;
    endif;
  else :
    // Here is saving usual post data. Do what you need for saving it or just skip this point
  endif;
  return $post_id;
}

Please note: validation of $_POST data may be more complex. For example, you may have to validate array of values if there are ACF galleries or relationships among your taxonomy fields. In my example i have only common text fields.

Hope that helps!



来源:https://stackoverflow.com/questions/37497204/acf-front-end-form-to-update-term

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!