使用Wordpress Rest API JS填充ACF字段 [英] Populate ACF Fields using Wordpress Rest API JS

查看:153
本文介绍了使用Wordpress Rest API JS填充ACF字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用WordPress的新手,并且正在使用Advanced Custom Fields插件.看起来不错,但我想知道是否可以通过WordPress REST API通过POST新建对象(使用ACF创建).我已经将它用于GET我所有的自定义对象(感谢ACF to REST API Plugin).

我将WordPress作为后端,将NextJS作为前端,因此我想创建一个新的HTML表单,用户可以在其中输入一些信息并直接创建该对象的实例.

如果不可能,保存到数据库(常见的MySQL实例)并模拟我所需的相同操作的机制是什么?我想阻止进行一些自定义实现(如果有的话)(让我知道您是否需要有关该问题或数据的更多信息)

经过研究,我发现我试图使用错误的端点来创建对象.

现在我可以创建自己的对象(自定义帖子类型),但是无法填充ACF字段...

我发送了标准请求:

var data = JSON.stringify({
  "title": "Test00",
  "status": "publish",
  "acf":{
    "customfield1":"Some value..."
  }
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "http://localhost:8000/wp-json/wp/v2/custom");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMCIsImlhdCI6MTU2MDM3NTQxNywibmJmIjoxNTYwMzc1NDE3LCJleHAiOjE1NjA5ODAyMTcsImRhdGEiOnsidXNlciI6eyJpZCI6IjIifX19.BCyrlFm_qD3-9DzCxQ37n4pJYkTasvLaN34NJtFAMC4");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);

我收到这个消息:

{
  ...
  "acf":{
    "customfield1":null
  }
}

有什么办法可以一次完成所有操作吗?我应该创建对象,然后发送额外的信息吗?

解决方案

与GET请求一样,您也可以使用POST请求将数据存储到CMS中.您需要做的是通过POST API调用传递授权标头.
您可以在此处获取有关授权机制的更多详细信息: https: //developer.wordpress.org/rest-api/using-the-rest-api/authentication/

标题:

Authorization:Bearer <token>
Content-Type:application/json

第二,您可以将 Body 数据作为RAW json进行传递,如下所示:

{
"title":"Sample ACF field demo",
"status": "publish",
"fields": 
    {      
        "text_custom_field_name" : "Text value",
        "checkbox_custom_field_name" : [
                "Option1,",
                "Option2,",
                "Option3"
            ],
        "textarea_custom_field_name" : "This is message field",
        "boolean_custom_field_name" : [
                true
            ]
    }
}

请让我知道是否需要任何帮助.

谢谢

I'm new working with WordPress and I'm playing around with the Advanced Custom Fields plugin. It seems nice but I'd like to know if it's possible to POST a new object (created using ACF) through the WordPress REST API? I'm already using it to GET all my custom objects (thanks to ACF to REST API Plugin).

I'm using WordPress as my backend and NextJS as the frontend so I'd like to create a new HTML form, where the user can input some info and directly create an instance of that object.

If it's not possible, what's the mechanism to save to the database (common MySQL instance) and simulate the same operation I need? I'd like to prevent going through some custom implementation if it's something out there yet (let me know if you need more info about the problem or the data)

EDIT:

After some research, I figured out that I was trying to create the object using the incorrect endpoint.

Now I'm able to create my own object (custom post type) but I'm not able to populate the ACF fields...

I send an standard request:

var data = JSON.stringify({
  "title": "Test00",
  "status": "publish",
  "acf":{
    "customfield1":"Some value..."
  }
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "http://localhost:8000/wp-json/wp/v2/custom");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMCIsImlhdCI6MTU2MDM3NTQxNywibmJmIjoxNTYwMzc1NDE3LCJleHAiOjE1NjA5ODAyMTcsImRhdGEiOnsidXNlciI6eyJpZCI6IjIifX19.BCyrlFm_qD3-9DzCxQ37n4pJYkTasvLaN34NJtFAMC4");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);

And I receive this:

{
  ...
  "acf":{
    "customfield1":null
  }
}

Is there any way to do it all at once? Should I create the object and then, send the extra info?

解决方案

Like the GET request, You can use the POST request as well to store data into the CMS. What you need to do is to pass the authorization headers with the POST API call.
You can get more detail about the authorization mechanism here: https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

Headers:

Authorization:Bearer <token>
Content-Type:application/json

Secondly you can pass the Body data as a RAW json as below:

{
"title":"Sample ACF field demo",
"status": "publish",
"fields": 
    {      
        "text_custom_field_name" : "Text value",
        "checkbox_custom_field_name" : [
                "Option1,",
                "Option2,",
                "Option3"
            ],
        "textarea_custom_field_name" : "This is message field",
        "boolean_custom_field_name" : [
                true
            ]
    }
}

Please let me know if any help needed.

Thanks

这篇关于使用Wordpress Rest API JS填充ACF字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆