Click to initialize TinyMCE
The Term Action is a template to create or update a term on form submission.
Add actions on form submission
(Optional) Target this action using hooks.
The URL to redirect to. See "Cheatsheet" tab for all available template tags.
(Optional) Target this action using hooks.
Fill inputs with values
Setting name | Description |
Action type | Create or update a term |
Action name | Alias name allowing to use targeted hook |
Target | The term to update |
Source | The term to retrieve fields from |
Load values | Fill inputs with values |
Name | The term name |
Slug | The term slug |
Taxonomy | The term taxonomy |
Parent | The term parent |
Description | The term description |
Save ACF fields | Choose which ACF fields should be saved to this term |
Load ACF fields | Choose which ACF fields should have their values loaded |
By default, there is no validation for this Action. You can use the acf/validate_value
hook to validate each fields individually.
To validate the whole form, you can use the acfe/form/validation/term
hook and throw errors with the acfe_add_validation_error()
function. Usage example:
/*
* Term Validation
*
* @array $form Form settings
* @string $post_id Post ID (where the form is displayed)
* @string $action Action name
*/
action('acfe/form/validation/term', $form, $post_id, $action);
action('acfe/form/validation/term/form=my-form', $form, $post_id, $action);
action('acfe/form/validation/term/action=my-term', $form, $post_id, $action);
add_action('acfe/form/validation/term/form=my-form', 'my_form_validation', 10, 3);
function my_form_validation($form, $post_id, $action){
// Get field input value
$my_field = get_field('my_field');
if($my_field === 'Company'){
// Add validation error
acfe_add_validation_error('my_field', 'The value Company is not allowed');
}
}
The preparation phase is triggered after the form validation and before its submission. Returning false
will stop the Action and go to the next one. Usage example:
/*
* Term Prepare
*
* @bool $prepare True/false
* @array $form Form settings
* @string $post_id Post ID (where the form is displayed)
* @string $action Action name
*/
filter('acfe/form/prepare/term', $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/term/form=my-form', $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/term/action=my-post', $prepare, $form, $post_id, $action);
add_filter('acfe/form/prepare/term/form=my-form', 'my_form_prepare', 10, 4);
function my_form_prepare($prepare, $form, $post_id, $action){
// Do not create/update term if user isn't logged in
if(!is_user_logged_in()){
$prepare = false;
}
// return
return $prepare;
}
Alter the term ID where meta values are loaded from. Usage example:
/*
* Term Values Source
*
* @int $term_id Term ID Source
* @array $form Form settings
* @string $action Action name
*/
filter('acfe/form/load/term_id', $term_id, $form, $action);
filter('acfe/form/load/term_id/form=my-form', $term_id, $form, $action);
filter('acfe/form/load/term_id/action=my-term', $term_id, $form, $action);
add_filter('acfe/form/load/term_id/form=my-form', 'my_form_term_source', 10, 3);
function my_form_term_source($term_id, $form, $action){
// Get field input value
$my_field = get_field('my_field');
if($my_field === 'Company'){
// Force to load values from the term ID 15
$term_id = 15;
}
// return
return $term_id;
}
Alter the term arguments before database insert/update. Usage example:
/*
* Term Arguments
*
* @array $args Generated term arguments
* @string $type Action type: 'insert_term' or 'update_term'
* @array $form Form settings
* @string $action Action name
*/
filter('acfe/form/submit/term_args', $args, $type, $form, $action);
filter('acfe/form/submit/term_args/form=my-form', $args, $type, $form, $action);
filter('acfe/form/submit/term_args/action=my-term', $args, $type, $form, $action);
add_filter('acfe/form/submit/term_args/form=my-form', 'my_form_term_args', 10, 4);
function my_form_term_args($args, $type, $form, $action){
/*
* Get the form input value named 'my_field'
*/
$my_field = get_field('my_field');
if($my_field === 'Company'){
// Change Description
$args['description'] = 'My term description';
}
/*
* Get previous Post Action output
*/
$prev_post_action = acfe_form_get_action('post');
if(!empty($prev_post_action)){
if($prev_post_action['post_title'] === 'Company'){
// Change Description
$args['description'] = 'My term description';
}
}
// Stop Term save
// return false;
// return
return $args;
}
Trigger a custom action after the term was created/updated using the acfe/form/submit/term
hook. Usage example:
/*
* Term Submit
*
* @int $term_id Created/Updated term ID
* @string $type Action type: 'insert_term' or 'update_term'
* @array $args Generated term arguments
* @array $form Form settings
* @string $action Action name
*/
action('acfe/form/submit/term', $term_id, $type, $args, $form, $action);
action('acfe/form/submit/term/form=my-form', $term_id, $type, $args, $form, $action);
action('acfe/form/submit/term/action=my-term', $term_id, $type, $args, $form, $action);
add_action('acfe/form/submit/term/form=my-form', 'my_form_submit', 10, 5);
function my_form_submit($term_id, $type, $args, $form, $action){
/*
* Get the form input value named 'my_field'
*/
$my_field = get_field('my_field');
if($my_field === 'Company'){
// do_something();
}
/*
* Get previous Post Action output
*/
$prev_post_action = acfe_form_get_action('post');
if(!empty($prev_post_action)){
if($prev_post_action['post_title'] === 'Company'){
// do_something();
}
}
}
The Action Output let developers retrieve the action data in a future action within the Form UI or a PHP hook. For example, the Template Tag {action:term:name}
will output the latest Term Action “Name” setting.
If the action is named, the Template Tag will use it. Ie: {action:my-term:name}
. The equivalent function in PHP is acfe_form_get_action('term')
.
It is possible to change the output of a Term Action using the acfe/form/output/term
filter. Usage example:
/*
* Term Output
*
* @array $data Term information
* @array $term_id Created/Updated term ID
* @string $type Action type: 'insert_term' or 'update_term'
* @array $args Term arguments
* @array $form Form settings
* @string $action Action name
*/
filter('acfe/form/output/term', $data, $term_id, $type, $args, $form, $action);
filter('acfe/form/output/term/form=my-form', $data, $term_id, $type, $args, $form, $action);
filter('acfe/form/output/term/action=my-term', $data, $term_id, $type, $args, $form, $action);
add_filter('acfe/form/output/term/form=my-form', 'my_form_output', 10, 6);
function my_form_output($data, $term_id, $type, $args, $form, $action){
/*
* $data
*
* Array
* (
* [term_id] => 72
* [name] => My Term
* [slug] => my-term
* [term_group] => 0
* [term_taxonomy_id] => 72
* [taxonomy] => category
* [description] => My description
* [parent] => 0
* [count] => 0
* [filter] => raw
* [permalink] => /category/my-term
* [admin_url] => /wp-admin/term.php?tag_ID=72&taxonomy=category
* )
*/
// Get field input value
$my_field = get_field('my_field');
if($my_field === 'Company'){
// Set a custom key
$data['custom_key'] = 'custom_value';
}
// return
return $data;
}