Articles of wp localize script

localizing variable on front-page.php template fails but succeeds on single page and taxonomy archive

I have the following code in a php include file: function pass_the_var() { $video_embed_code = jkaw_oembed_code(); return array( ’embedCode’ => $video_embed_code ); } earlier in the same file I also enqueue my scripts: wp_register_script(‘jkaw-js’, get_template_directory_uri() . ‘/lib/js/jkaw.js’, false, ‘1.0’, true); wp_enqueue_script( ‘jkaw-js’ ); wp_localize_script( ‘jkaw-js’, ‘phpVars’, pass_the_var() ); I am using this code on a […]

Calling wp_localize_script in a different function

I am trying to localize a variable in a javascript below.. The scenario is that I want to dynamically get the post_id and localize it in the jquery I have enqueued through wp_enqueue_scripts. Here’s my code: function add_scripts() { wp_enqueue_style(‘jplayercss’,plugins_url( ‘skin/jplayer.blue.monday.css’,__FILE__)); wp_enqueue_script( ‘jplayer’,plugins_url( ‘js/jquery.jplayer.min.js’ , __FILE__ ) ); wp_enqueue_script( ‘jplayerjs’,plugins_url( ‘js/mfsjplayer.js’,__FILE__), array(‘jquery’) ); } add_action( […]

Problem in wp_localize_script

I am try to enque and localize my word-press scripts My code for for enque my scripts is function load_my_scripts() { wp_register_script(‘custom-js’, get_stylesheet_directory_uri() . ‘/js/custom.js’, array(‘jquery’)); wp_enqueue_script(‘twentyfifteen-custom-js’, get_template_directory_uri() . ‘/js/custom.js’, array(), ‘20141010’, true); wp_localize_script( ‘custom-js’, ‘MyAjax’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ) ) ); } add_action(‘wp_enqueue_scripts’, ‘load_my_scripts’); My custom js is being enqueue if i […]

wp_localize_script not passing the data

I’ve been trying to work with a small plugin and I tried to pass my data to js via wp_localize_script but when I’m doing console.log() inside my js it is returning undefined. Can anyone tell me what I’m doing wrong here: $shortcode_data = array(); /*Fetching the plugin directoryt path*/ $dir = plugin_dir_url( __FILE__ ); // […]

Getting a variable inside foreach from PHP to JS after localization

I believe I have properly enqueue’ed and localized my script – I’ve got $q showing up in the DOM. Now I’m sure I’m missing the simple last step to accessing a variable in the JS function. Here’s my PHP: add_action( ‘wp_enqueue_scripts’, ‘sdm_load_javascript_files’ ); function sdm_load_javascript_files() { wp_register_script( ‘showMake’, get_template_directory_uri() . ‘/js/functions.js’, array(‘jquery’), ‘1.1.0’, true ); […]

Can wp_localize_script be used within a shortcode?

I am developing a plugin, and I would like to make data available to the client-side JS code executed by one of my shortcodes. wp_localize_script seems like the obvious way to do that, but it’s not working for me: my script is included, but not the JS data. The data depends on a value in […]

Can't access PHP array inside script localization from javascript

I have a loop to retrieve all users with the specific meta_key user values from wp_user_meta database and then put it in wp_localize_script array so I could access the data with Javascript and do my thing with it. Unfortunately, when I run the loop and put all the results to the variable, and try to […]

Can't seem to get wp_localize_script to work

This is my code: wp_enqueue_script(‘ms_ajax_localized’); wp_localize_script( ‘ms_ajax_localized’, ‘MS_Ajax’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ), ‘nextNonce’ => wp_create_nonce( ‘myajax-next-nonce’ )) ); I popped it into my functions.php Bashing my head against the wall. I’ve used wp_localize_script before and I’m just blind to what is wrong this time. When I load the page I can’t find the […]

How do I pass the template url to javascript in the ADMIN area of my theme?

I need to pass the path to a php file (located in a folder in my theme) to a JS file being used in my admin area but I have no idea how to accomplish that. What Im trying to do, is build a page where selecting some options in a form will pass the […]

Localizing methods for jQuery append prepend wrap parent

So I am serializing some options in WordPress and then localizing them to be used in my jQuery script, however what I am trying to do next isn’t working. jQuery(‘.advert’)+ lu_ban_object.method +(‘<div>Hello World</div>’) So in my db I’ve got some values serialized; following is the data that is being localized to used as method chosen […]