无法使用Node.js请求更新ACF字段

时间:2019-01-29 23:06:02

标签: javascript node.js wordpress http request

我正在尝试使用nodejs更新acf字段,这是我的nodejs代码;

var postData = {update : '1', field : 'acf-field-text'}
var querystring = require("querystring");
var qs = querystring.stringify(postData);
var qslength = qs.length;

var clientServerOptions = {
uri: 'http://localhost/wp-json/wp/v2/micro/138',
body:qs,  
auth: {
    username: 'username',
    password: 'blablabla'
},
method: 'POST',  
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': qslength
    }
}  

request(clientServerOptions, function (error, response) {
    console.log(response, response.statusCode); 
});

这是我的function.php代码;

add_action( 'rest_api_init', function () {

register_rest_route( 'wp/v2', '/micro/(?P<id>\d+)', array(
    'methods' => array('POST','PUT'),
    'callback' => 'microserviceOnline',
) );

});

function microserviceOnline(WP_REST_Request $request) { 

// Update the field
$result = update_field($request['field'], $request['update'], 
$request['id']);  

return $result;

}

我得到的结果是>>>>>假200

acf_field没有更新,请帮助!

0 个答案:

没有答案