• I am trying to create a click counter. So, I have created a ajax call to counter.php, where the incrementing functions will take place.

    After incrementing, I wanted to update a custom field. So, I tried to use some acf field functions. But, I get an error, Uncaught Error: Call to undefined function the_field()

    Here is the entire code.

    <?php
    $currentNumber = $_POST['currentNumber'];
    $postId = $_POST['postId'];
    
    function increment($currentNumber) {
        $newNumber = $currentNumber + 1;
        return $newNumber;
    }
    
    echo increment($currentNumber);
     
    ?>
    <p></p>
    <?php echo $postId ?>
    <?php the_field( 'coupon_users' ); ?>
    

    How to solve this error?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I think “the_field” is something that belongs to the ‘Advanced Custom Fields’ plugin. Are you using that?

    Thread Starter Nirmal Kumar

    (@onlinerockers)

    Yes @anevins, I am using that plugin

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    May be worth asking in that plugin’s support section as to how you can use their functions in your context: https://www.ads-software.com/support/plugin/advanced-custom-fields#new-post

    If you call a PHP file in your theme or plugin directly with AJAX then neither WordPress or its plugins will be loaded, so you won’t be able to use WordPress or ACF functions.

    You could include WordPress’s files into your PHP file to load WordPress, but the proper way to do AJAX in WordPress is to send requests to admin-ajax.php and hook your functionality into that file, as described here (and on the following page), or if appropriate create a custom endpoint for the REST API, as described here.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Call to undefined function’ is closed to new replies.