admin管理员组

文章数量:1327676

I'm trying to store in a variable the content of an ACF field so that later on I can use it in a jQuery function using the plugin "Simple Custom CSS and JS".

I found what I thought was the answer in the following link Get ACF value in external jQuery document but I'm getting an error inside the console saying "Uncaught ReferenceError: MyFields is not defined".

This is my PHP code:

add_action ('wp_enqueue_scripts','wpse244551_add_field');

function wpse244551_add_field () {
  $my_field = array ();
  $my_field[] = array ('field1' => get_field ('variable_product_html'));
  wp_localize_script ('myScript', 'MyFields', $my_field);
}

This is my jQuery code (Note: I'm using the Simple Custom CSS and JS to write my code):

jQuery(document).ready(function( $ ){
    
    function myScript() {
        var phpValue = MyFields.field1;
        console.log(phpValue);
    }
  
    myScript();

});

本文标签: Get with jQuery the value of an ACF field