Thanks Hube2, you’re spot on. This is what worked for me:
I have a text input with the “Field Group” set as ‘test’
HTML
<form id="test-form" action="">
<input type="text" name="input-test" value="<?php the_field('test'); ?>">
<input type="submit" value="Update">
</form>
PHP (In the themes function.php file)
function test_function() {
// Set variables
$input_test = $_POST['input-test'];
// Check variables for fallbacks
if (!isset($input_test) || $input_test == "") { $input_test = "Fall Back"; }
// Update the field
update_field('test', $input_test);
}
add_action( 'wp_ajax_nopriv_test_function', 'test_function' );
add_action( 'wp_ajax_test_function','test_function' );
Javascript
var ajaxurl = 'http://'+window.location.host+'/wp-admin/admin-ajax.php';
var form = "#test-form";
$(form).submit(function(event) {
event.preventDefault();
$.ajax({
url: ajaxurl + "?action=test_function",
type: 'post',
data: $(form).serialize(),
success: function(data) {
console.log("SUCCESS!");
},
error: function(data) {
console.log("FAILURE");
}
});
});
Ah that’s perfect. Thank you for sharing!
It also works when you try to re-order the menu too.
function custom_menu_order($menu_ord) {
if (!$menu_ord) return true;
return array(
'index.php', // Dashboard
'separator1', // First separator
'edit.php?post_type=page', // Pages
'edit.php', // Posts
'upload.php', // Media
'acf-options', // ACF Options
'separator2', // Second separator
'users.php', // Users
);
}
add_filter('custom_menu_order', 'custom_menu_order');
add_filter('menu_order', 'custom_menu_order');