Support

Account

Home Forums ACF PRO get_field(‘field_name’, ‘option’) not working while called at CF7 hook

Solved

get_field(‘field_name’, ‘option’) not working while called at CF7 hook

  • Hi, I’m creating a script that will overwrite CF7 recipient basing on selected location (one of form fields). I’ve defined locations and assigned recipients using ACF options page (repeater field).
    Then I want to iterate over the locations and find proper e-mail address. The problem is that when I try to get options field value I receive NULL.

    When I try get_field(‘field_name’, $some_post_id) I receive data, but when I use get_field(‘field_name’, ‘options’) it returns NULL.

    I found corresponding records in wp_options table and changed the ‘autoload’ column value to ‘yes’ but it didn’t help.

    My code:

    add_action('wpcf7_before_send_mail', 'cf7CustomRecipient');
    function cf7CustomRecipient($contact_form)
    {
        $submission  = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();
        
        $location     = trim($posted_data['your-location'][0]);
        
        if (isset($location) && !empty($location)) {
            var_dump(get_field('form-locations', 'options')); // returns NULL
            var_dump(get_field('investment__city', 600)); // returns proper value
            
            $formLocations = get_field('form-locations', 'options');
        
            if (!empty($formLocations)) {
               foreach ($formLocations as $formLocation) {
                  if ($formLocation['location'] == $location) {
                     $recipient = $formLocation['mail'];
                     break;
                  }
               }
            }
        }
        // recipient overwrite
    }

    Using below code:
    var_dump(get_field('form-locations', 'options'));
    directly in homepage template returns proper data, so there is no typo involved.

  • I have looked at this and I don’t have a clue. Any idea I have for why this would happen falls apart due to the fact that getting a field from a post works but getting a field from options does not work. If this was something in ACF neither should be working.

    The only thing I can come up with is that there is some type of filter being applied during the call in ACF to the WP function get_option(). The first thing I would do is search the code on the site (all plugins and theme) to see of any of the filters applied by the WP function are used.

    It could be something in ACF that I am missing. You might want to contact the developers or open a ticket on your account.

  • I’m using “ACF options for PolyLang” plugin so my options can be defined separately for each language (and a ‘fallback’ using ‘show all languages’ option from WP admin bar).

    In my hook ACF couldn’t read options saved for current language (probably due to some issue of “ACF options for PolyLang” hooks). It returned the ‘fallback’ values for all languages (default ACF behavior).

    The solution:
    When the real cause was revealed the solution was pretty simple. I just had to call get_field() like this:
    get_field(get_locale() . '_form-locations', 'options');
    or hard-coded:
    get_field('pl_PL_form-locations', 'options');

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

You must be logged in to reply to this topic.