DEV Community

Eana Hufwe
Eana Hufwe

Posted on • Originally published at blog.1a23.com on

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.

The post Write to Repeater Fields of Ultimate Fields with WordPress REST API appeared first on 1A23 Blog.

Top comments (0)