Add action hook into wp_localize_script

Everything is working as expected. The problem is that your hook is rendering content to the page and you would like to pass that output to the javascript variable included in the JS output. You need to capture the hook output into a variable then add to $data. // buffer output ob_start(); // run hook … Read more

How to enqueue the script without hardcoded in the theme files?

//add the JS code in a variable – without <script> $script = ” jQuery(document).ready(function(){ // add your shortcode attribute and its default value to the // gallery settings list; $.extend should work as well… _.extend(wp.media.gallery.defaults, { type: ‘default_val’ }); // join default gallery settings template with yours — store in list if (!wp.media.gallery.templates) wp.media.gallery.templates = … Read more

Admin-Ajax Error

You need to pass the values to the localization script, “right” after this line: wp_enqueue_script(‘rnm_load_more’); I’m going to pass the admin URL to your script by using wp_localize_script: $localization = array( ‘ajax_url’ => admin_url(‘admin-ajax.php’), ); wp_localize_script( ‘rnm_load_more’, ‘jjang’, $localization ); Now, you can use the ajax_url in your script: var ajaxurl = rnm_load_more.ajax_url; You can … Read more

How to call wp_localize_script() after the script?

Well, you have to call wp_licalize_script after registering the script, because you need handle of that script… And of course you can’t localize something that doesn’t exist… Here’s some example: if ( !function_exists(‘pt_scripts’) ): function pt_scripts () { wp_register_style( ‘style’, get_stylesheet_uri(), null, ‘1.3.1’, ‘all’ ); wp_enqueue_style( ‘style’ ); wp_register_script( ‘scripts’, get_template_directory_uri() . ‘/script.js’, array(‘jquery’), ‘1.3.0’, … Read more

How to pass conditional array to wp_localize_script

Ended up with this as the function: // set up the settings for the theme.js function mb_scripts_settings() { // blanks $mb_ajax_form_type = $mb_get_page_slug = $mb_redirect = $mb_redirect_time = $mb_form_disable = $mb_array = ”; // get the form type $mb_ajax_form_type = ( is_front_page() ? ‘change’ : ‘submit’ ); // get the page slug from ID $mb_get_page_slug … Read more