我正在尝试通过REST API在发布请求后更新或添加名为“image_link”的自定义字段 .

如果我事先通过Wordpress手动添加了自定义字段,则get_callback将工作并显示自定义字段 .

add_action('rest_api_init', function(){
 register_rest_field('myposttype', 'image_link',
    array(
       'get_callback'    => 'get_for_rest_meta',
       'update_callback' => 'update_for_rest_meta',
       'schema'          => null,
    )
 );
});

function get_for_rest_meta($object, $field_name, $single=true) {
    return get_post_meta( $object['id'], $field_name);
}

之后在wp-json中:

"image_link": [
      "http://www.something.com/something"
    ]

如何使用POST请求更新/添加自定义字段到WordPress REST API?

请求:

content:"Some content"
image_link:"http://www.something.com/something"
status:"publish"
title:"Some title"

功能:

function update_for_rest_meta(???) {
    ???
}