问题
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);
回答1:
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.
来源:https://stackoverflow.com/questions/28939372/acf-front-end-form-to-create-term