Support

Account

Home Forums ACF PRO Detect if a field is being loaded for editing or for being displayed

Solved

Detect if a field is being loaded for editing or for being displayed

  • Hi there!

    I’m trying to find a robust way to detect inside the filter acf/load_value, if a value was requested by an acf_field_group or directly for being displayed. Is there any way? If not, this would be a highly helpful feature to run different logic on the value, depending on context.

    If there isn’t a known solution for that, I will contact ACF PRO support directly.

  • Normally this can be done using is_admin() This should be false when displaying a field from a template. The only time it might be for edit when this is false is if you are using an acf_form() on the front of the site. In this case you can optionally add/remove the filter before you call acf_form() depending on whether or not you want the field filtered.

  • Hi John, as always, you are the fastest! πŸ™‚

    is_admin() is not enough for my use case, not even in combination with !wp_doing_ajax(). I often also use get_field() in the admin area, for example to display ACF field data in manage_post_posts_custom_column or the like.

    I came up with an admittedly pretty hacky solution:

    
    // inside the acf/load_value callback:
    /**
     * Don't touch the value if it was requested by <code>acf_render_fields</code>
     */
    $backtrace = wp_debug_backtrace_summary(null, 0, false);
    if( array_search('acf_render_fields', $backtrace) !== false ) return $value; 
    

    This checks if the function was invoced by acf_render_fields(), which is only used when an ACF form is being rendered (also when using acf_form() in the frontend).

    Anyways, thanks for helping me think!! πŸ™‚

  • I would most likely conditionally add the filter.

    In functions.php or wherever you are adding the filter

    
    if (!is_admin()) {
      add_filter('acf/load_value/name=field-name', 'your-function-name');
    }
    

    This would mean that the filter would not be applied in the admin or during any ajax requests.

    If you wanted to make it even more specific you could add the filter only just before your got the field. This would ensure that it is only applied when you want it to be applied.

    
    add_filter('acf/load_value/name=field-name', 'your-function-name');
    $value = get_field('field_name');
    
  • Hi John,

    thank you. Maybe your answers will help others at some point – I needed a more general solution, based on field type checks. The above solution using wp_debug_backtrace_summary works great for me. Will mark it as solved.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.