ACF前端表格以创建术语 [英] ACF front end form to create term

查看:79
本文介绍了ACF前端表格以创建术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ACF前端表单功能来创建带有用于创建分类术语(在本例中为组)的自定义字段的表单。我已经看过如何在帖子中实现此功能,并且在整个网站上都做到了,但是我无法实现此功能以插入新的术语。我认为我在正确的轨道上,但是仍然存在问题,未添加条款

 函数my_pre_save_post($ post_id)
{
//检查这是否是新帖子
if($ post_id!='new')
{
return $ post_id;
}

//创建一个新组

//插入组
$ post_id = wp_insert_term(
$ _POST ['acf [_post_title]'],//术语
'groups',//分类法
数组(
'description'=> $ _POST ['acf [_post_content]'],

);

//更新$ _POST ['return']
$ _POST ['return'] = add_query_arg(array('post_id'=> $ post_id),$ _POST ['return' ]);

//返回新ID
return $ post_id;
}

add_filter(‘acf / pre_save_post’,‘my_pre_save_post’);
$ args = array(
'post_id'=>'new',
'field_groups'=> array(6055),
'post_title'=>是,
'post_content'=> true,
);
acf_form($ args);


解决方案

也许为时已晚,但我想回答帮助其他有此问题的人。在WP Codex中,我们可以找到基于wp核心行为的不错的解决方案。



首先,我们必须创建ACF字段组并将其设置为在分类法编辑屏幕上显示。然后向其中添加自定义字段。您必须至少创建一个自定义字段-将来的术语标题必须是必填字段。其他字段由您决定。



在呈现表单之前,需要检查一件事。我们需要知道ACF字段组ID。您可以在组编辑页面的url中找到它,例如:

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

在这种情况下,组ID为340。不想使用硬编码的ID(如果您的组不时变化),则可以使用组名(在此示例中为组名ID Technic CPT)获得它:

 全局$ wpdb; 
$ group_ID = $ wpdb-> get_var(从$ wpdb->帖子中选择ID,其中post_title ='Technical CPT');

因此,现在我们准备呈现表单:

  acf_form_head(); 
$ acf_form_args = array(
'id'=>'technic_add_form',
'post_id'=>'new_post',
'form'=>是,
'submit_value'=>'发布',
'field_groups'=> gt数组($ group_ID),
'new_post'=>数组(
'post_type'=> ;'technic',
'post_status'=>'publish'

);
acf_form($ acf_form_args);

请注意参数 post_type!它必须是您的分类法的名称(子类)。在此示例中,我为自定义分类法技术创建术语。



现在,我们必须防止将此数据另存为帖子,而是创建一个术语。通过wp_post_insert()函数创建新的帖子,其中包含许多有用的过滤器和操作。其中之一是非常有用的过滤器- wp_insert_post_data 。在创建发布数据数组之后但在插入数据库之前将触发它。

  add_filter('wp_insert_post_data','acf_taxonomy_handler','99',2); 
函数acf_taxonomy_handler($ data,$ postarr){
if($ data [‘post_type’] ==‘technic’):
全局$ wpdb;
$ group_ID = $ wpdb-> get_var(从$ wpdb->帖子中选择ID,其中post_title ='Technical CPT');
$ acf_fields = acf_get_fields_by_id($ group_ID);
foreach($ acf_fields as $ acf_field)$$ acf_field ['name'] = trim(esc_attr(strip_tags($ _POST ['acf'] [$ acf_field ['key']])));
$ term = wp_insert_term($ technic_name,'technic');
if($ term):
foreach($ acf_fields as $ acf_field):
update_field($ acf_field ['name'],$ {$ acf_field ['name']},'technic_ '。$ term ['term_id']);
endforeach;
endif;
的回报;
else:
返回$ data;
endif;
}

现在让我们解释一下此功能。



首先,我们检查post_type来找到我们的分类法名称。



然后,我们从链接的acf字段组中获取数组自定义字段并清理其值。 / p>

下一步是创建新术语。我们使用自定义字段之一作为新术语的名称。



然后,如果一切正常,我们可以更新所有自定义字段的值并停止执行wp_insert_post()。 / p>

如果我们得到其他post_type,则只返回$ data。



因此,创建了该术语:)如果您不仅要在前端添加术语,还要编辑和保存它们,则必须再编写一个函数,用于另一个过滤器, acf / pre_save_post 。代码实际上是相同的,但是如果您需要,我可以将其发布。


I want to use ACF frontend form function to create a form with custom fields that is used to create a taxonomy term, in this case a group. I have seen how to implement this with a post and have done so throughout the site but i am unable to implement this to insert a new term. I think i am on the right track but still am having issues with it not adding terms

        function my_pre_save_post( $post_id )
            {
                // check if this is to be a new post
                if( $post_id != 'new' )
                {
                    return $post_id;
                }

                // Create a new group

                // insert the group
                $post_id = wp_insert_term(
                  $_POST['acf[_post_title]'], // the term
                  'groups', // the taxonomy
                  array(
                    'description'=> $_POST['acf[_post_content]'],
                  )
                );

                // update $_POST['return']
                $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );

                // return the new ID
                return $post_id;
            }

            add_filter('acf/pre_save_post' , 'my_pre_save_post' );
        $args = array(
            'post_id' => 'new',
            'field_groups' => array( 6055 ),
            'post_title' => true,
            'post_content' => true,
        );
        acf_form($args);

解决方案

Maybe it's too late, but i want to answer to help others, who has this problem. In WP Codex we can find a nice solution based on wp core behaviour.

First of all, we must create ACF Field group and set it to be displayed on taxonomy edit screen. Then add custom fieds to it. You must create at least one custom field - for future term title, it must be required. Any other fields are on your discretion.

There is one thing to check before rendering the form. We need to know ACF field group ID. You can find it 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 id Technic CPT):

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

So, now we are ready to render our form:

acf_form_head();
$acf_form_args = array(
  'id' => 'technic_add_form',
  'post_id' => 'new_post',
  'form' => true,
  'submit_value' => 'Publish',
  'field_groups' => array($group_ID),
  'new_post' => array(
    'post_type' => 'technic',
    'post_status' => 'publish'
  )
);
acf_form( $acf_form_args );

Please note parameter "post_type"! It must be name (slug) of your taxonomy. In this example i create terms for custom taxonomy "technic".

Now we must prevent saving this data as post and create a term instead. New posts are creating by wp_post_insert() function, containing many useful filters and actions. One of them is very useful filter - 'wp_insert_post_data'. It fires after making post data array, but before inserting to database. Just what we need, right?

add_filter( 'wp_insert_post_data', 'acf_taxonomy_handler', '99', 2 );
function acf_taxonomy_handler( $data, $postarr ) {
 if ( $data[ 'post_type' ] == 'technic' ) :
  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 );
  foreach ( $acf_fields as $acf_field ) $$acf_field[ 'name' ] = trim( esc_attr( strip_tags( $_POST[ 'acf' ][ $acf_field[ 'key' ] ] ) ) );
  $term = wp_insert_term( $technic_name, 'technic' );
  if ( $term ) :
   foreach ( $acf_fields as $acf_field ) :
    update_field( $acf_field[ 'name' ], ${$acf_field[ 'name' ]}, 'technic_' . $term[ 'term_id' ] );
   endforeach;
  endif;
  return;
 else :
  return $data;
 endif;
}

Now let's explain this function.

First of all we check post_type to find our taxonomy name.

Then we get array custom fields from our linked acf field group and sanitize their values.

Next step is to create a new term. We use one of our custom fields as name of new term.

Then, if all is ok, we can update all custom fields values and stop executing wp_insert_post().

And if we get some other post_type, we just return $data.

So, the term is created :) If you want not only adding terms on frontend but also editing and saving them, you must write one more function, for another filter, 'acf/pre_save_post'. Code is practically the same, but if you need, i can post it.

这篇关于ACF前端表格以创建术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆