Home Forums General Issues acf not defined – JavaScript API


acf not defined – JavaScript API

  • I’m trying to use the ACF JS API, however when I try to inspect the acf object in the console, the console returns

    (Uncaught ReferenceError: acf is not defined

    I’m using the latest Version of ACF, 5.8.0 beta 3 and have jQuery installed too. Am I missing a step in order to be able to use the acf JS API?

  • Your script is running before ACF is initialized. More than likely you’re including your script too soon or running it too soon. When are your enqueuing your script? You should probably do this on the acf/input/admin_enqueue_scripts hook

    Something not mentioned, but an added precaution is to make your script dependent of “acf-input”

    Also ensure that your script does not run until the document is ready.

  • John, thanks for your reply. Does that mean I should create a separat JS file in order to use the acf js api and shouldn’t use my main.js file with the hook add_action( 'wp_enqueue_scripts', 'my_scripts' );?
    And can you explain what you mean by making the script dependant on ‘acf-input’? I don’t quite understand.

  • Sorry I did not get back to this sooner.

    When I build custom JS that extends ACF I use a separate JS file for this. I enqueue this script when ACF does so that it’s only loaded when ACF is.

    // this is lacking, but I want to highlight how to make your script dependent acf
    add_action('acf/input/admin_enqueue_scripts', 'my_acf_extension_enqueue');
    function my_acf_extension_enqueue() {
      $handle = 'my-handle';
      $src = 'url of script';
      // $deps is where we tell WP what scripts need to load first
      $deps = array('acf-input');
      wp_enqueue_script($handle, $src, $deps, false, true);
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘acf not defined – JavaScript API’ is closed to new replies.