Support

Account

Home Forums ACF PRO Load ACF Groups With Ajax

Solving

Load ACF Groups With Ajax

  • I have field groups that are dynamically created in PHP (acf_add_local_field_group) for posts of a certain post type according to one of it’s meta values.

    It works fine, however should the meta value change, the user must save the post for the proper ACF field group to render.

    I would like to know if there’s an easy way to load an ACF field group via ajax, and have it save properly.

    So far I’ve managed to load the field groups via ajax when the meta value is changed, however if I modify any of the fields, the changes are not saved.

    My javascript

    var $groups_wrap = $('#normal-sortables');
    
    $('div[id^="acf-gen"]', $groups_wrap).remove();
    
    $.get( window.location.href, {type_id:type_id}, function(data) {
    
    	var $response = $('<div />').html(data);
    	
    	var $divs = $response.find('div[id^="acf-gen"]');
    	
    	$groups_wrap.append($divs);
    	
    });
  • You need to do 2 things. The first is to create a custom location rule that is based on your custom field. The second thing to do is to call the ACF function that will update the field groups displayed on the page.

    I played around a bit with this in the past to create custom location rules based on ACF choice fields. https://github.com/Hube2/acf-custom-field-locations-rule. With some work this can be adapted to work on other types of fields… but I would build the function for a specific field only. The plugin I created is not very efficient because it checks every ACF field.

  • @hube2 Thanks for the github link!

    I’ll check this out as soon as I can once I’m back on this project and update here.

  • @sgray Hey have you ever got back to this project?

    I have something similar where I have a metabox I created for the page editor in WordPress. With Select Values. I wonder How I can dynamically load the acf group when chosen on select? I gotten to the part where I have ajax calling functions but I’m not so familiar in the realm of Advanced Custom Fields

  • Hi @cheezyblue

    Unfortunately we didn’t go anywhere with this idea due to deadlines. We haven’t run into a situation needing this again so we haven’t explored it further.

    Good luck!

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.