Support » Plugin: Advanced Custom Fields: Font Awesome Field » ACF FA Field only returns json

  • Resolved jdoubleya

    (@jdoubleya)


    I’ve updated to FA 6 and have the ACF FA field set to return the Icon Class, it seems to be only returning json instead of the classes. See below.

    “{ “style” : “light”, “id” : “phone”, “label” : “Phone”, “unicode” : “f095″ }”

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Matt Keys

    (@mattkeys)

    Hey @jdoubleya,

    That looks like what I might expect to see if you ask ACF to return the field ‘raw’ without formatting.

    https://www.advancedcustomfields.com/resources/get_field/

    Note the third parameter. Can you confirm (maybe paste) your call to get this field data to make sure it isn’t setting this value to false somehow?

    Thanks,

    Matt Keys

    Thread Starter jdoubleya

    (@jdoubleya)

    Hey @mattkeys,

    Thanks for the quick reply. The field is in a repeater which is in an options page. The following code used to return the classes before updating the plugin and FA 6.

    $icon = isset($options['options_header_button_group_buttons_' . $i . '_button_icon']) ? '<i class="' . $options['options_header_button_group_buttons_' . $i . '_button_icon'] . '"></i>' : '';

    I wrote the following temporary fix which works for the time being.

    $iconArr = isset($options['options_header_button_group_buttons_' . $i . '_button_icon']) ? json_decode($options['options_header_button_group_buttons_' . $i . '_button_icon']) : '';
    $iconStyle = !empty($iconArr) ? $iconArr->style : '';
    $iconId = !empty($iconArr) ? $iconArr->id : '';
    $icon = !empty($iconArr) ? '<i class="fa-'.$iconStyle.' fa-'.$iconId.'"></i>' : '';
    Plugin Author Matt Keys

    (@mattkeys)

    If you follow the code ‘upstream’ and find where the $options['options_header_button_group_buttons_' . $i . '_button_icon'] value gets set, that is where I expect the issue to be located. I am pretty sure that the call that is being made to get_sub_field() is incorrectly getting the field value without formatting and you should be able to change a ‘false’ value to a ‘true’ one, or just omit the second parameter completely.

    (note because you are in a repeater it is get_sub_field() instead of get_field() )

    It would make sense that this went unnoticed in your code in previous versions of this plugin because before this latest release the raw return from get_field() or get_sub_field() would be the classes. But going forward it is now JSON as I needed to store more information in the DB to support some of the new features of this plugin.

    If you don’t mind chasing it down I’d appreciate it. Because if I am wrong then there is a much more serious bug happening and I’d like to learn more about it so I can fix it.

    • This reply was modified 5 months, 3 weeks ago by Matt Keys.
    Thread Starter jdoubleya

    (@jdoubleya)

    Here’s a minified version of the code setup.

    
    $options = wp_load_alloptions(); // load all wp options
    $buttons = $options['options_header_button_group_buttons'];
    
    // loop through header button group
    for ($i = 0; $i < $buttons; $i++) {
      // get icon class
      $icon = isset($options['options_header_button_group_buttons_' . $i . '_button_icon']) ? '<i class="' . $options['options_header_button_group_buttons_' . $i . '_button_icon'] . '"></i>' : '';
    
      // format button and add to array
      $buttons[] = '<a href="' . $url . '" class="btn ' . $style . '" title="' . $title . '"' . $target . ' role="button">' . $icon .  '<span class="text d-none d-xxl-inline-block">' . $title . '</span></a>';
    }
    

    I did create a new test field and used
    $icon = get_field('icon', 'options');
    which returns the icon class correctly “fa-solid fa-phone-xmark”, so it seems to be an issue with pulling the values from the wp_options.

    Plugin Author Matt Keys

    (@mattkeys)

    Okay yes that would do it. Thank you for tracking that down.

    I’ve never actually seen that approach for working with ACF field data. A big shortcoming of bypassing the ACF functions when getting field values, is that it skips the formatting of the field data, which is why you are getting this odd behavior.

    From a performance standpoint, depending on the particular site in question, the data from wp_load_alloptions() could be pretty small, but it could also be huge. As it is loading anything registered anywhere in WordPress that has ‘autoload’ set to true (which is the default for any WordPress option). I’ve seen sites in the past where this was over 1MB of data.

    I’d recommend sticking with the ACF functions and just grabbing specifically what you need. You could get the entire repeater like:

    $buttons = get_field( 'options_header_button_group_buttons', 'option' );

    I typically work with repeater data using a loop like:

    if ( have_rows( 'my_option_name', 'option' ) ) :
    	while( have_rows( 'my_option_name', 'option' ) ) : the_row();
    		// Do what I need to do with the sub fields here
    		// use either the_sub_field() or get_sub_field() depending on if you are echoing or returning the value
    	endwhile;
    endif;

    Read more about using the ACF functions for working with options data here:

    https://www.advancedcustomfields.com/resources/get-values-from-an-options-page/

    I’m with the same issue.

    if( have_rows('incluso_no_pacote') ):
                      while( have_rows('incluso_no_pacote') ) : the_row(); ?>
                        <li>
                          <?= get_sub_field('icone'); ?>
                          <?= get_sub_field('texto'); ?>
    
                        </li>
                      <?php endwhile; endif; ?>

    and return

    { “style” : “solid”, “id” : “user-check”, “label” : “User Check”, “unicode” : “f4fc” }

    Any idea?

    Plugin Author Matt Keys

    (@mattkeys)

    Hey @roodrigoooh it sounds like you are having the same problem so it is likely the same resolution too.

    The other user who was having this issue wasn’t using the ACF fields for getting values (get_field / get_sub_field / the_field / the_sub_field / etc).

    Can you confirm if you are using those functions to get your value? Can you also confirm that you are not requesting an unformatted value be returned by those functions? This is for instance the second parameter in the_field or get_field.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.