Another solution is to use acf/rest/format_value_for_rest/type=image
filter
function custom_acf_rest_api_image_format($value, $post_id, $field) {
// Get the full image object from the ID using wp_get_attachment_image_src()
$image = wp_get_attachment_image_src($value, 'full');
// Return an array with the image URL and other metadata
return array(
'url' => $image[0],
'width' => $image[1],
'height' => $image[2],
'alt' => get_post_meta($value, '_wp_attachment_image_alt', true)
);
}
// Hook the filter function to the acf/rest/format_value_for_rest/type=image filter
add_filter('acf/rest/format_value_for_rest/type=image', 'custom_acf_rest_api_image_format', 10, 3);
Another solution could be to define a conditional logic.
if field1 “value is equal to” true && field1 “value is equal to” false
Greate work,
I have to add this after render field to get work with relation field:
<script>
(function($) {
acf.do_action('append', $('#post'));
})(jQuery);
</script>
Some error on my response, this is a correct code:
add_action( 'rest_insert_project', 'prefix_update_files_field', 10 , 3 );
function prefix_update_files_field($post, $request, $true){
global $wp_rest_additional_fields;
$my_post_type = 'project';
$additional_fields = $wp_rest_additional_fields[$my_post_type];
foreach ( $additional_fields as $field_name => $field_options ) {
if ( ! $field_options['update_callback'] ) {
continue;
}
// Don't run the update callbacks if the data wasn't passed in the request.
if ( ! isset($_FILES[ $field_name ] ) ) {
continue;
}
$result = call_user_func( $field_options['update_callback'], $_FILES[ $field_name ], $object, $field_name, $request, $my_post_type );
if ( is_wp_error( $result ) ) {
return $result;
}
}
}
I found a solution but i’m sure there is a better way.
I add an action on rest_insert_<post_type>
hook
add_action( 'rest_insert_project', 'prefix_update_files_field', 10 , 3 );
function update_files_field($post, $request, $true){
global $wp_rest_additional_fields;
$my_post_type = 'project';
$additional_fields = $wp_rest_additional_fields[$my_post_type];
foreach ( $additional_fields as $field_name => $field_options ) {
if ( ! $field_options['update_callback'] ) {
continue;
}
// Don't run the update callbacks if the data wasn't passed in the request.
if ( ! isset($_FILES[ $field_name ] ) ) {
continue;
}
$result = call_user_func( $field_options['update_callback'], $request[ $field_name ], $object, $field_name, $request, $my_post_type );
if ( is_wp_error( $result ) ) {
return $result;
}
}
}
I found a solution, but I’m sure we can do it cleaner.
In My theme:
## First I add somes helpers
function zk_acf_get_field_groups_by_tax($taxonomy){
$groups = acf_get_field_groups();
$filtered_groups = [];
foreach($groups as $key => $group):
foreach($group['location'] as $location):
foreach($location as $location_item){
if(
$location_item['param'] == 'taxonomy' &&
$location_item['operator'] == '==' &&
$location_item['value'] == $taxonomy
){
$filtered_groups[] = $group;
}
}
endforeach;
endforeach;
return $filtered_groups;
}
function zk_acf_get_fields_by_tax($taxonomy){
$groups = zk_acf_get_field_groups_by_tax($taxonomy);
$fields = [];
foreach($groups as $group){
$group_fields = acf_get_fields($group['key']);
if(is_array($group_fields)){
$fields = array_merge($fields, $group_fields);
}
}
return $fields;
}
## Then I copy class-acf-field-taxonomy.php in my theme and modify ajax_add_term function
function ajax_add_term() {
// vars
$args = wp_parse_args($_POST, array(
'nonce' => '',
'field_key' => '',
'term_name' => '',
'term_parent' => ''
));
// verify nonce
if( !acf_verify_ajax() ) {
die();
}
// load field
$field = acf_get_field( $args['field_key'] );
if( !$field ) {
die();
}
// vars
$taxonomy_obj = get_taxonomy($field['taxonomy']);
$taxonomy_label = $taxonomy_obj->labels->singular_name;
$custom_fields = zk_acf_get_fields_by_tax($field['taxonomy']);
// validate cap
// note: this situation should never occur due to condition of the add new button
if( !current_user_can( $taxonomy_obj->cap->manage_terms) ) {
wp_send_json_error(array(
'error' => sprintf( __('User unable to add new %s', 'acf'), $taxonomy_label )
));
}
// save?
if( $args['term_name'] ) {
// exists
if( term_exists($args['term_name'], $field['taxonomy'], $args['term_parent']) ) {
wp_send_json_error(array(
'error' => sprintf( __('%s already exists', 'acf'), $taxonomy_label )
));
}
// vars
$extra = array();
if( $args['term_parent'] ) {
$extra['parent'] = (int) $args['term_parent'];
}
// insert
$data = wp_insert_term( $args['term_name'], $field['taxonomy'], $extra );
// error
if( is_wp_error($data) ) {
wp_send_json_error(array(
'error' => $data->get_error_message()
));
}
// load term
$term = get_term($data['term_id']);
foreach($custom_fields as $custom_field){
$value = $_POST[$custom_field['name']];
if($value){
update_field($custom_field['name'], $value, $term);
}
}
// prepend ancenstors count to term name
$prefix = '';
$ancestors = get_ancestors( $term->term_id, $term->taxonomy );
if( !empty($ancestors) ) {
$prefix = str_repeat('- ', count($ancestors));
}
// success
wp_send_json_success(array(
'message' => sprintf( __('%s added', 'acf'), $taxonomy_label ),
'term_id' => $term->term_id,
'term_name' => $term->name,
'term_label' => $prefix . $term->name,
'term_parent' => $term->parent
));
}
?><form method="post"><?php
acf_render_field_wrap(array(
'label' => __('Name', 'acf'),
'name' => 'term_name',
'type' => 'text'
));
foreach($custom_fields as $field){
acf_render_field_wrap($field);
?>
<script>
acf.add_filter('prepare_for_ajax', function (data) {
data['<?php echo $field['name']; ?>'] = jQuery('#acf-<?php echo $field['key']; ?>').val();
console.log('data',data);
return data;
});
</script>
<?php
}
if( is_taxonomy_hierarchical( $field['taxonomy'] ) ) {
$choices = array();
$response = $this->get_ajax_query($args);
if( $response ) {
foreach( $response['results'] as $v ) {
$choices[ $v['id'] ] = $v['text'];
}
}
acf_render_field_wrap(array(
'label' => __('Parent', 'acf'),
'name' => 'term_parent',
'type' => 'select',
'allow_null' => 1,
'ui' => 0,
'choices' => $choices
));
}
?><p class="acf-submit">
<button class="acf-submit-button button button-primary" type="submit"><?php _e("Add", 'acf'); ?></button>
</p>
</form><?php
// die
die;
}
Any news about that ?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.