Thread Starter
peh4
(@peh4)
I tried to follow this -> https://www.advancedcustomfields.com/resources/integrating-custom-field-types/
but i don’t know where to paste this (tried in myTheme/functions.php but it crashes)
add_filter( 'acf/rest/get_fields', function ( $fields, $resource, $http_method ) {
if ( ! is_array( $fields ) ) {
return $fields;
}
// Get our field type by the field name.
$field_type = acf_get_field_type( 'font-awesome' );
// If our field hasn't been registered and initialized by ACF, do so now.
if ( ! $field_type instanceof acf_field ) {
// Initialize the class containing our custom field type. If there are any constructor args, you will need to pass those in as well.
$field = new acf_field_font_awesome();
acf_register_field_type( $field );
}
// Get the field array (by the field name) and add it to the array of fields supported by REST.
$fields[] = acf_get_field( 'font-awesome');
return $fields;
}, 10, 3 );
I haven’t tried this personally so I do not have a resolution in mind. But it does seem like you are probably on the right track with the code you shared. I suspect you are running up against some timing issues where the code you put in your themes functions.php is executing before the plugin has registered the field. It looks like the code block tries to account for this and create a new instance of the field if it doesn’t yet exist, and that is likely where the error you mentioned is coming up. Can you share what error(s) you are seeing?
Also can you let me know which major version of FontAwesome you are loading with the plugin (5, 6?)
-
This reply was modified 2 months ago by
Matt Keys.
Thread Starter
peh4
(@peh4)
Thank you for your quick answer that motivated me 🙂 this is now working, the last lines caused the error and was useless
I hope this helps you to add this behavior by default 😉
add_filter( 'acf/rest/get_fields', function ( $fields, $resource, $http_method ) {
if ( ! is_array( $fields ) ) {
return $fields;
}
// Get our field type by the field name.
$field_type = acf_get_field_type( 'font-awesome' );
// If our field hasn't been registered and initialized by ACF, do so now.
if ( ! $field_type instanceof acf_field ) {
// Initialize the class containing our custom field type. If there are any constructor args, you will need to pass those in as well.
$field = new acf_field_font_awesome([]);
acf_register_field_type( $field );
}
return $fields;
}, 10, 3 );
Glad to hear it is working for you now. This is sort of a workaround to a bigger ‘issue’ with the way this plugin includes itself. This uses an older method to add fields to ACF. I spent some time a few weeks ago refactoring to the new method but then ran out of time and got busy with other work and personal life stuff.
But the hope is to eventually get this plugin on the new method, which should have some trickle down benefits like working out of the box with REST.
Thread Starter
peh4
(@peh4)
great to hear that, take care 👍