REST API Error | WordPress.org

[ad_1]

Hmm, I’ve never tried using this in REST. This was originally built before WP & REST was really a thing so probably a blind spot that just hasn’t been brought up by other users yet. Looking at the latest boilerplate for new ACF fields I don’t see any mention of the format_value_for_rest() function you are seeing errors about. I don’t extend that function in my plugin but you’d think it would be able to access it from the parent ‘acf_field’ class my field extends.

/**
 * Apply basic formatting to prepare the value for default REST output.
 *
 * @param mixed      $value
 * @param string|int $post_id
 * @param array      $field
 * @return mixed
 */
public function format_value_for_rest( $value, $post_id, array $field ) {
	return $value;
}
  • This reply was modified 2 hours, 48 minutes ago by Matt Keys.

Does adding public $show_in_rest = true; to the class in either acf-font-awesome-v5.php or acf-font-awesome-v6.php (depending which FontAwesome you are using) make any difference to the error you are seeing?

See the boilerplate I linked above for where they added that line.

public $show_in_rest = true; does not fix the error.
Seems to be related to the fieldtype not being reported at all.

acf_get_field_type( $field['type'] )

Will dive a bit deeper and report back.

Ok found the problem! As mentioned above the fieldtype needs to be registered within acf-font-awesome-v5.php or acf-font-awesome-v6.php

$fa = new acf_field_font_awesome( $this->settings );
acf_register_field_type( $fa );

Cheers

Nice, that definitely points things in the right direction. A search for that function turns up this page:

Integrating Custom Field Types With the WordPress REST API

This bit here seems to be referring to plugins like mine:

For field types that are not registered with acf_register_field_type(), such as field types that just construct themselves inside the acf/include_field_types action hook, it is possible to include them in the REST API by using the acf/rest/get_fields filter as shown below:

This plugin using the include_field_types action they reference.

Using the filter they suggest on that page will allow you to include FontAwesome into your REST fields without making any modifications to this plugin that would get overwritten during the next plugin update.

I’ll look into what the appropriate way would be for me to include this out of the box in a future update but that should get you what you need for now.

  • This reply was modified 1 minute ago by Matt Keys.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer