ACF前端表格更新期限

时间:2016-05-28 09:09:34

标签: advanced-custom-fields term

我想使用ACF前端表单函数来创建包含自定义字段的表单

我在创建新术语@Alhana时看到了这个问题 ACF front end form to create term  但我想用旧数据生成表单

2 个答案:

答案 0 :(得分:0)

嗯,我没有看到这个问题,但如果它仍然存在,那么这是一个解决方案。 首先,确保您拥有与您的分类相关联的ACF组。您将需要此组的ID,可以在组编辑页面的URL中找到它,例如:

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

在这种情况下,群组ID 340 。如果您不想使用硬编码ID(如果您的群组不时更改),您可以使用群组名称(在此示例中群组名称为技术CPT )获取:

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

然后,您需要您正在更新的术语ID 。我认为,从WP的基础知识开始写它并不是必要的。:)你将以这样的结尾结束:

$term_id = 405;

最后,你需要你的分类标准。在此示例中,它是技术。所以,让我们呈现我们的形式!

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 ); 

现在,您的字词的自定义字段将以此形式显示。但是要在编辑后保存术语数据,您需要添加更多代码。 ACF表单假设您正在保存帖子数据,我们会添加一些逻辑来检测保存期限数据。

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;
}

请注意:验证$ _POST数据可能会更复杂。例如,如果您的分类法字段中存在ACF库或关系,则可能必须验证值数组。在我的例子中,我只有常见的文本字段。

希望有所帮助!

答案 1 :(得分:0)

来自Alhana的答案为我带来了一个变化。如果将术语对象作为post_id的值发送,则可以使用:

$term_obj = get_term($term_id);
$acf_form_args = array(
            'post_id' => $term_obj,
            'post_title' => false,
            'submit_value' => 'Update Term',
            'field_groups' => array($group_ID),
    );