Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active April 30, 2021 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/50a441c4d44504d649e3f31ccfd579ce to your computer and use it in GitHub Desktop.
Save mishterk/50a441c4d44504d649e3f31ccfd579ce to your computer and use it in GitHub Desktop.
AJAX functionality examples for Advanced Forms Pro version 1.8.0
jQuery(function ($) {
// Changing the response type to a redirect on the fly.
acf.addAction('af/form/ajax/submission', function (data, form) {
if (form.key === 'form_5f8f987654e0') {
data.type = 'redirect';
data.redirect_url = 'https://example.com';
}
});
});
jQuery(function ($) {
// Changing the success message.
acf.addAction('af/form/ajax/submission', function (data, form) {
if (form.key === 'form_5f8f987654e0') {
data.success_message = '<div style="background:green; color:white;">This is a new message!</div>'
}
});
});
<?php
advanced_form( 'form_5f8f987654e0', [ 'ajax' => true ]);
<?php
add_filter( 'af/form/ajax/response', function ( $response, $form, $args ) {
if ( $form['key'] === 'form_5f8f987654e0' ) {
// If our 'star_rating' ACF field has a value less than 3, redirect
// the user to another page.
if ( af_get_field( 'star_rating' ) < 3 ) {
$response['type'] = 'redirect';
$response['redirect_url'] = 'https://example.com';
}
}
return $response;
}, 10, 3 );
jQuery(function ($) {
// Triggering any custom JavaScript.
acf.addAction('af/form/ajax/submission', function (data, form) {
if (form.key === 'form_5f8f987654e0') {
// …add custom handler code here…
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment