Support

Account

Forum Replies Created

  • Super-late response, but for anyone else checking here: I had this exact same requirement, and someone else has made a plugin for it:

    https://wordpress.org/plugins/acf-multiple-taxonomy/

    This just adds another ACF field type called Multiple Taxonomies and works pretty much exactly how you’d expect.

  • In my functions.php file, I added the following:

    if (!is_admin()) {
    	add_filter('clean_url', function($url) {
    		if (strpos($url, '.js') === false || strpos($url, 'jquery.min.js') != false || strpos($url, 'acf') != false) {
    			return $url;
    		}
    		
    		return "$url' defer='defer";
    		
    	}, 11, 1);
    }

    The clean_url filter modifies the URL of any script being enqueued – this is where I was adding my own defer property to my scripts. However, as this was causing an issue with my ACF forms, I added an exception so that it didn’t add a defer for any ACF scripts.

    If you’ve got something else that’s handling script deferment, you’ll need to check to see if it has hooks that allow you to add exceptions.

  • I believe the issue I had was that, in my theme’s functions.php file, I was deferring my JS files. As such, the inline JS vars set by ACF when acf_form() is used weren’t available to the scripts at page load. I added an exception to not defer any scripts with acf in their filename, and that solved the issue for me.

  • Perfect, thank you! The first part of your answer solved my question. I kept my .click() method and then added the following:

    acf.add_filter('validation_complete', function( json, $form ){
    	if (json.errors) {
    		showLoadingBlocker(false);
    	}
    	
    	return json;
    });

    I realise I couldn’t do anything exclusively with JS after the form submission had gone through and the page redirected, but all I was really looking for was that kind of JS error handler.

  • Looks that way, yep. However, as this was my first time using acf_form() and all I’d seen it do was full-on page refreshes/redirects, I assumed that’s how it worked – I had no idea it was supposed to ajax-validate the form elements.

  • I’ve emailed Elliot, but it looks like a function in my theme to defer JS loading is the culprit that prevents acf_form() from making a call to admin-ajax.php for some reason:

    if (!is_admin()) {
    	add_filter('clean_url', function($url) {
    		if (strpos($url, '.js') === false || strpos($url, 'jquery.min.js') != false || is_page('my-account')) {
    			return $url;
    		}
    		
    		return "$url' defer='defer";
    		
    	}, 11, 1);
    }

    This adds defer="defer" to all enqueued JS files (apart from jQuery). If I comment this out, the ACF form does ajax-based inline validation. For now I’ve added to the conditional statement so that it doesn’t defer scripts when on the page containing the form.

  • I’m not sure that’s the case: the function validate_save_post in /core/validation.php will wp_die() with the validation message if the $show_errors parameter is set to true, which is exactly what I’m getting above.

    Doing a manual stack trace, it looks like it’s being called from save_user() in /forms/user.php, wherein it checks if acf_validate_save_post(true) returns true, then save the user info. That parameter is $show_errors, and is hard-set.

    For the sake of a sanity check, I enabled WP_DEBUG and all I got were a couple of notices about an undefined index in my functions file. I amended those and still got the same result, but having looked at the code, it looks like this is how it’s supposed to behave?

    Just to be clear: I’m using acf_form() on the front end to update user details. There are a couple of forms on the same page, and they all work – whenever I submit a form, I get the little spinner, then the page refreshes with the query_vars specified in my acf_form() as $_GET arguments. It’s just that when one of the required fields is left blank, I end up with this ugly wp_die() screen instead of either some ajax response or an in-form error message.

    For reference, here’s my code:

    $userObject = wp_get_current_user();
    $userID = $userObject->ID;
    
    $profileOptions = array(
       'post_id'         => 'user_' . $userID,
       'id'            => 'profile-form',
       'field_groups'      => array('vnm_profile__acf_group_user_profile'),
       'form'            => false,
       'return'         => add_query_arg(array('section-updated' => 'profile', 'updated' => 'true'), get_permalink()),
       'updated_message'   => __('Profile information updated!', 'my-theme'),
       'submit_value'      => __('Update Profile', 'my-theme'),
    );
    
    acf_form($profileOptions);
Viewing 7 posts - 1 through 7 (of 7 total)