Support

Account

Home Forums Backend Issues (wp-admin) Custom location rule JS refresh

Solved

Custom location rule JS refresh

  • Hi,

    I was hoping someone could point me in the right direction on implementing the js refresh on the location rules.
    I have no issue on the PHP parts but I can’t find anything on how to trigger the ajax refresh.

    From what I read, a refresh of the rules can be triggered by:

    $(‘#my_select’).live(‘change’, function(){
    acf.ajax.update( ‘rule_name’, jQuery(this).val() ).fetch();
    });

    But unfortunately this doesn’t work. Do I miss something ?
    I’m looking for triggering the rule validation from non acf fields.

    Thanks

    Michael

  • I can’t say if this will get you in the right direction or not. I have created a plugin that lets you set location rules by select fields in other field groups. When it came to the JS part of it what I did was to extend the ACF AJAX to add my own and to trigger the updated. You can see this here https://github.com/Hube2/acf-custom-field-locations-rule/blob/master/acf-custom-field-location-rule.js.

    Like I said, this extends acf.ajax. What it does is that it gets the value for a field and then triggers ACF to refresh (fetch) the field groups. There shouldn’t be any reason that you couldn’t do this with fields outside of ACF as will.

  • Sorry John for the late reply, but I just came back to it today.

    The correct code was:

    $('#my_select').live('change', function(){
        acf.ajax.update( 'rule_name', $(this).val() ).fetch();
        $(document).trigger('acf/update_field_groups');
    });

    I misspelled the rule name in my code.
    When I got the ajax to work, I finally noticed that the matching condition was always returning false 🙂

    So finally everything works, really powerful stuff these custom location rules 🙂

    Thanks

    Michael

  • A PHP error during the AJAX can also cause it to not work. Debugging those ajax requests can be tricky. Good to hear you got it working.

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

You must be logged in to reply to this topic.