Enqueue scripts all over but not in single.php

with is_singular() if (!is_singular(‘post’)) { function live_search() { wp_enqueue_script(‘live_search’, get_bloginfo(‘stylesheet_directory’) .’/assets/js/live.search.js’, array(‘jquery’), ‘1.0.1’, true ); wp_localize_script( ‘live_search’, ‘MovieWordpressSearch’, array( ‘api’ => moviewp_url_search(), ‘nonce’ => moviewp_create_nonce(‘moviewp-search-nonce’), ‘area’ => “.live-search”, ‘more’ => “Show all”, ) ); } add_action(‘wp_enqueue_scripts’, ‘live_search’); }

Localize variables with TinyMCE script

Looks as though Tiny MCE is not enqueued. Looks like it is loaded through the _WP_Editors class in class-wp-editor.php. There is a action hook in there called before_wp_tiny_mce where you can output a script with some localised variables… This is what I am going to do.

Script Localization doesn’t work

Copying from the answer here regarding variable scope Variables inside a function are only available inside that function. Variables outside of functions are available anywhere outside of functions, but not inside any function. Because of that, you need to add your $translations array within the kvkoolitus_load_comment_validation function, like function kvkoolitus_load_comment_validation(){ $translations = array( ‘value1’ =>’This … Read more

Different uniqid when calld in wp_localize_script and shortcode

Inside the __construct() you can put this global $shortcode_id; $shortcode_id = 0; in the display_gallery_shortcode() function you can do this <?php global $shortcode_id; $shortcode_id ++; $settings_arr = array();//Here is your settings for each shortcode ob_start(); ?> <script> if(!my_plugin_data) var my_plugin_data = {} my_plugin_data[<?php echo $shortcode_id ?>] = <?php echo json_encode($settings_arr) ?> </script> <div>MY SHORTCODE CONTENT … Read more

wp_localize_script not working on ajax response

Please try below code : script.js jQuery(function($) { var i; for(i=1;i<21;i++){ $(‘body’).on(‘change’, ‘#file’+i, function() { var file_data = $(this).prop(‘files’)[0]; var id = $(this).attr(‘id’); var form_data = new FormData(); form_data.append(‘file’, file_data); form_data.append(‘action’, ‘file_upload’); jQuery.ajax({ url: pw_script_vars.ajax_url, type: ‘POST’, contentType: false, processData: false, data: form_data, success: function (response) { alert( response.alert ); } }); }); } }); … Read more