Support

Account

Home Forums General Issues Ajax error: Class acf_field not found.

Solved

Ajax error: Class acf_field not found.

  • Hi,

    I’m working on a new custom field with some Ajax code. When I POST data in ajax with alert(data) I get the following error:

    Fatal error: Class ‘acf_field’ not found in /home/…/…etc/../..etc/ test.php</b> on line 3

    How to get this date with ajax? Because when I load the .php file directly I get the same error indeed, also with the standard fields.

    Thanks!

  • 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);
    		}
    	});
    });
    
  • Thanks for your response, actually this does not solve the issue. Let me explain better.

    This is a part of my php file for the field:

    <?php
    
    class acf_field_number_range extends acf_field
    {
    
    (..)
    
    <script type="text/javascript">
    
    $("#slider").bind("valuesChanged", function (e, data) {
       $.ajax({
       type: "POST",
       dataType: "text",
       url: "../wp-content/themes/twentytwelve/fields/test.php",
       data: { minValue: data.values.min, maxValue: data.values.max },
       async: false,
       success: function(data){
          alert(data);
          
        },
         error: function(xhr) {
                 alert('fail')
                }
     });
    });
    
    </script>

    But the alert popup gives this error:

    Fatal error: Class ‘acf_field’ not found in /home/…/…etc/../..etc/ test.php</b> on line 3

    How can I get these values of the range slider? Thanks

  • Hi @LeffDesign

    Can you confirm that you have registered your custom field type correctly in the register_fields action?

    It sounds like your field type is being created before ACF plugin is loaded.

  • Hi Elliot,

    Well, my field shows up in the acf plugin. I thought it would not show up or even get an error if it was not registered, right? The error is only resolving when it posts the data.

    If not, how can I check if it is registered the right way?

  • Hi @LeffDesign

    In your above code, you have the AJAX call pointing to:

    
    url: "../wp-content/themes/twentytwelve/fields/test.php",
    

    Is this correct? If so, WP will not be loaded correctly, ACF will not be included and your PHP error will make sense.

    In your first code however, you write that you are using the native wp_ajax functions.php AJAX solution.

    Can you confirm which way you are coding as they dramatically will effect ACF’s usage

  • Hi @Elliot,

    url: "../wp-content/themes/twentytwelve/fields/test.php"

    I’ve done it this way indeed, I’m not using the native wp_ajax functions. So that would be the problem. I’ll find out how to do it correctly, thanks.

  • Hi @LeffDesign

    Thanks for the reply.

    Yes, this confirms the issue. Please research how to use the WP native ajax functionality.

    This will solve your issue!

  • Is it possible to get ACF included? I’m trying to do basically the same thing but just php. I have the code working correctly in a template. But I need it to run via wp_cron. None of the ACF functions work in functions.php. Is it possible to use the data I have in options fields in a function run by wp_cron? If so please give me an example of how to make it work.

  • Hi @DanielB

    Good question..

    In short, I’m not 100% sure how to do this. You would need to instantiate WP and include the ACF plugin making sure all the correct add-ons and actions were called for the plugin to function.

    Perhaps your cron could just target a url which you could then work from within WP?

  • Hi @DanielB

    … kind of like a JSON request

  • For anyone else who may have this same issue. I just added

    require_once('/path/to/wp-load.php');

    at the top of my script. Then I run the script via cron. Works like a charm. This is not in functions.php and it’s not run by wp_cron. It’s simply a php file with the line above and my code nothing more.

Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘Ajax error: Class acf_field not found.’ is closed to new replies.