• Resolved peh4

    (@peh4)


    Hi,

    First i want to thank you for this awesome plugin

    I have a problem to access the icon via the wp-api. The others “normal” fields showed up but not this particular one

    /wp-json/wp/v2/mycustomtype

    {…

    acf: [ number: 0, color: “#d7827c”] //i should have an icon value here

    …}

    How can i fix this ?

    Best regards,

    • This topic was modified 11 months, 3 weeks ago by peh4.
Viewing 6 replies - 1 through 6 (of 6 total)
  • 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 );
    
    Plugin Author Matt Keys

    (@mattkeys)

    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 11 months, 3 weeks 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 );
    Plugin Author Matt Keys

    (@mattkeys)

    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 👍

    Plugin Author Matt Keys

    (@mattkeys)

    Hey @peh4,

    I ended up spending some time on this tonight along with a few other updates I’ve been sitting on. It’s currently in version 4.0.7 of this plugin. I haven’t fully launched that version yet but it can be downloaded here:

    https://wordpress.org/plugins/advanced-custom-fields-font-awesome/advanced/

    Go to “ADVANCED OPTIONS” and download the “Development Version” if you are able to test it out.

    Can you download and test this new version to see if it resolves the problems you were seeing with this plugin (before your workaround).

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can’t access FA field from wp-api’ is closed to new replies.