Home › Forums › General Issues › Ajax error: Class acf_field not found. › Reply To: Ajax error: Class acf_field not found.
You mentioned when you loaded the .php file directly, does this mean this isn’t a part of a WordPress page? A stand-alone PHP page? I’m not sure whether ACF fields would be accessible through this or not, i’d guess not.
You’d be better off adding a php callback function for your ajax in your functions.php file.
Here’s a simple example of getting a field with AJAX:
In your functions.php:
add_action('wp_ajax_getdatefunction', 'getdate');
add_action('wp_ajax_nopriv_getdatefunction', 'getdate');
function getdate(){
the_field('your_date_field');
die();
}
In your JS file:
$('button').click(function(){
$.ajax({
type: "POST",
url:'/wp-admin/admin-ajax.php',
data: 'action=getdatefunction',
cache: true,
success: function(results){
$('.your-date-container').html(results);
alert('The date is: '+results);
}
});
});
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.