Support

Account

Home Forums Gutenberg ACF Blocks, Javascipt API and field values Reply To: ACF Blocks, Javascipt API and field values

  • I don’t use blocks, so this may help you and it may not.

    Generally when I need to do this I will code something for the back end that uses the ACF JS API and something different for the front end. In the admin you may as well use jQuery because it is already loaded by ACF.

    When I want to make values of fields available to JS on the front end of the site I do one of the following.

    The first would be if I was including a separate js file using wp_enqueue_script(). In this case I wp_localize_script().

    The second method is when I’m adding incline JS code. In this case I just add what I need to the inline JS

    
    // example
    var MyVar = '<?php the_field('some_field_name');';
    

    There have been occasions in the paste where I write the values into incline JS that will later be used by a script loaded into a file.

    All of the above methods are used if the values needed will not change from page load to page load so that some type of cache plugin is used.

    If the values needed are dynamic and might change for different visitors then I generally use an AJAX request to get the needed values so that caching will not interfere. this usually also means that I’m doing one of the above as well to supply static values needed for making any AJAX requests. It has been a long time since I’ve done AJAX requests without the aide of jQuery.