custom post types - register_rest_field update_callback don't work for $_FILES

I try to insert a post with REST API who contain text and image in one api call.My image is an acf field.I register my f

I try to insert a post with REST API who contain text and image in one api call.

My image is an acf field.

I register my field like that:

register_rest_field( 'project', 'logo', array(
    'get_callback'       => 'get_field_logo',
    'update_callback'    => 'update_field_logo',
    )
);
register_rest_field( 'project', 'headline', array(
    'get_callback'       => 'get_field_headline',
    'update_callback'    => 'update_field_headline',
    )
);

I send my request via Postman

update_field_headline is correctly call, but update_field_logo is never call.

I try to debug wp-includes/rest-api/endpoints/class-wp-rest-controller.php

on line 421 is see:

// Don't run the update callbacks if the data wasn't passed in the request.
if ( ! isset( $request[ $field_name ] ) ) {
    continue;
}

I think update_field_logo is never call because "logo" is not set inside $_POST variable but inside $_FILES variable. So in line 421 the field is skipped.

If I modifiy the core file on line 421 like below everythings work well

if ( ! isset( $request[ $field_name ] ) && ! isset($_FILES[ $field_name ] ) {
    continue;
}

But modify WordPress Core file is not a solution...

Any idea how can I allow files to be trigger on update_callback ?

I try to insert a post with REST API who contain text and image in one api call.

My image is an acf field.

I register my field like that:

register_rest_field( 'project', 'logo', array(
    'get_callback'       => 'get_field_logo',
    'update_callback'    => 'update_field_logo',
    )
);
register_rest_field( 'project', 'headline', array(
    'get_callback'       => 'get_field_headline',
    'update_callback'    => 'update_field_headline',
    )
);

I send my request via Postman

update_field_headline is correctly call, but update_field_logo is never call.

I try to debug wp-includes/rest-api/endpoints/class-wp-rest-controller.php

on line 421 is see:

// Don't run the update callbacks if the data wasn't passed in the request.
if ( ! isset( $request[ $field_name ] ) ) {
    continue;
}

I think update_field_logo is never call because "logo" is not set inside $_POST variable but inside $_FILES variable. So in line 421 the field is skipped.

If I modifiy the core file on line 421 like below everythings work well

if ( ! isset( $request[ $field_name ] ) && ! isset($_FILES[ $field_name ] ) {
    continue;
}

But modify WordPress Core file is not a solution...

Any idea how can I allow files to be trigger on update_callback ?

Share Improve this question asked Sep 11, 2019 at 15:34 ZecKaZecKa 7781 gold badge6 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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 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 ], $post, $field_name, $request, $my_post_type );

        if ( is_wp_error( $result ) ) {
            return $result;
        }
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745163546a4614513.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信