Php 多个字段的寄存器\u rest\u字段上的更新\u回调不起作用
我的代码对于使用Php 多个字段的寄存器\u rest\u字段上的更新\u回调不起作用,php,wordpress,wordpress-rest-api,Php,Wordpress,Wordpress Rest Api,我的代码对于使用register\u rest\u field注册的一个字段非常有效。这对get\u callback和update\u callback都有效。我能够从rest端点获得预期的响应。但每当我添加多个要注册的字段时,API就会崩溃。尽管get_回调非常有效,但是向API发布更新时,它会在注册的第一个更新字段上崩溃。下面是代码 add_action( 'rest_api_init', 'register_post_fields' ); // Register post field
register\u rest\u field
注册的一个字段非常有效。这对get\u callback
和update\u callback
都有效。我能够从rest端点获得预期的响应。但每当我添加多个要注册的字段时,API就会崩溃。尽管get_回调非常有效,但是向API发布更新时,它会在注册的第一个更新字段上崩溃。下面是代码
add_action( 'rest_api_init', 'register_post_fields' );
// Register post fields.
function register_post_fields() {
// works if only this one field is added like below
register_rest_field( 'post', 'source_name', array(
'get_callback' => 'get_post_source_name',
'update_callback' => 'update_post_source_name',
'schema' => array(
'description' => __( 'Post views.' )
),
));
// On adding below code block for second field crashes on posting update
register_rest_field( 'post', 'source_url', array(
'get_callback' => 'get_post_source_url',
'update_callback' => 'update_post_source_url',
'schema' => array(
'description' => __( 'Post views.' ),
'type' => 'string'
),
));
}
function get_post_source_name( $post_obj ) {
$post_id = $post_obj['id'];
return get_post_meta($post_id, 'source_name', true);
}
function update_post_source_name( $value, $post, $key ) {
$post_id = update_post_meta( $post->ID, $key, $value );
if ( false === $post_id ) {
return new WP_Error(
'Update failed',
__( 'Failed to update Source name.' ),
array( 'status' => 500 )
);
}
return true;
}
function get_post_source_url( $post_obj ) {
$post_id = $post_obj['id'];
return get_post_meta($post_id, 'source_url', true);
}
function update_post_source_url( $value, $post, $key ) {
$post_id = update_post_meta( $post->ID, $key, $value );
if ( false === $post_id ) {
return new WP_Error(
'Update failed',
__( 'Failed to update Source url.' ),
array( 'status' => 500 )
);
}
return true;
}
我的目标是能够像这样添加多个rest字段,但是发布更新会导致应用程序崩溃,因为第一个API的响应失败