Write to Repeater Fields of Ultimate Fields with WordPress REST API

Ultimate Fields is a WordPress plugin that add custom fields to existing post types, both built-in and custom. It’s the only free plugin I found that can add a repeating field (field with arbitrary number of items. However, this plugin has not been updated for almost a year, and there’s a bug where repeater fields cannot be written using REST API.

I was just trying to update the field with a JSON array of objects and got this error message saying [FIELD_NAME] is not of type 'string'. After digging through the source code, I found that the REST API adapter is checking each field class for a defined JSON schema type. When the type is not found, it fallbacks to string.

Surprisingly, get_api_data_type is not used anywhere in the code except the few lines above, while obviously repeater fields should take an array type instead.

So it’s simple to solve this issue now. All I need to do is to add the method returning "array" to ultimate-fields/core/classes/Field/Repeater.php.

public function get_api_data_type() {
    return "array";
}

With that, the REST API will be happy to recognise the repeater field input properly as a list, and write to the database as expected.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, enter the URL of your response which should contain a link to this post’s permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post’s URL again. (Find out more about Webmentions.) 要在你自己的网站上回应,请输入你的回应页面的 URL,该页面应包含指向此文章永久链接 URL 的链接。你的回应随后将显示在此页面上(可能需要经过审核)。想更新或删除你的回应?请更新或删除你的文章并再次输入你的文章 URL。(了解更多关于 Webmention 的信息。) 自分のサイトで返信するには、この投稿のパーマリンク URL へのリンクを含む返信ページの URL を入力してください。あなたの返信は(承認後に)このページに表示されます。返信を更新または削除したい場合は、あなたの投稿を更新または削除し、再度その投稿の URL を入力してください。(Webmention について詳しくはこちら。)