Articles of wp localize script

Get Current User info using wp_localize_script, in functions.php

I need to pass the info from wp_get_current_user() to the front end for a script that uses it. To achieve this, I am using wp_localize_script() to pass the information. I put the code at the top of my functions.php file but it doesn’t work. Here it is. The idea is that on the login event, […]

How can I output a php value into a JS file within WordPress?

I am trying to use get_theme_mod option from php to load into my js file. I’ve used wp_localize_script to convert. The tweet_id is where I need to place the $tweet value outputted. Any help would be great! (PS: I’ve registered the script in functions.php already.) PHP FILE <? $tweet = get_theme_mod( ‘tweets_widget_id’ ); wp_localize_script( ‘twitter’, […]

Localize variable for multiple Shortcodes

I have this code for flexslider Shortcode add_shortcode(‘flexslider’, function($atts){ global $post; $ids = explode(‘,’, $atts[ids]); $uniqid = uniqid(); wp_enqueue_script( ‘shortcode_flexslider’); wp_localize_script( ‘shortcode_flexslider’, ‘slider’, array(‘id’ => $uniqid)); foreach( $ids as $id ) { $imgLinks = wp_get_attachment_image_src($id, large); $imgThumb = wp_get_attachment_image_src($id, thumbnail); $slider .= ‘<li><img src=”‘.$imgLinks[0].'”>’.$imgCaptionContent.'</li>’; $carousel .= ‘<li><img src=”‘.$imgThumb[0].'”></li>’; } $structure ='<div id=”slider’.$uniqid.'” class=”flexslider”><ul class=”slides”>’ .$slider. […]

How do you pass a boolean value to wp_localize_script

This question already has an answer here: Passing boolean values with wp_localize_script 1 answer

esc_html__ security : what for in this example?

how does esc_html__ (the 2nd one) protect the $message variable from being hacked ? what’s the point to use this protection here (the second one with a plain text)? <?php function unknown(){ /* If the user input any text, escape it. */ if ( !empty ( $_POST[‘unknown’] ) ) $message = esc_html ( $_POST[‘unknown’] ); […]

How to get the post ID when creating JS variables with localize_script

how do you set the current post ID as a JS variable with localize_script? It seems like the $post variable isn’t availible in the functions.php file. When is it created? Do i have to add localize script to a hook? Which?

Use of global variables within plugin

I am creating my first plugin ever and I found my self having to call the same option variables within various parts of the same page therefore I sought for a solution and stumbled upon global variables which seems to be working for me, however after reading a few articles people seem to be against […]

Wp_localize_script from Shortcode

I’ve been trying for hours and tried several answers from here and Google but still no luck.. need help. I am making a shortcode (slider) and trying to execute a javascript file (for the slider’s script) using parameters taken from shortcode’s attributes. function slider_default($atts, $content) { extract(shortcode_atts(array( “animation” => ”, “animationspeed” => ”, ), $atts)); […]

Ajaxing in functions.php

To ajax filtered posts, I needed to put most of my index.php template into the functions.php file (followed this tutorial) and do from there. But now javascript won’t work – basically the accordion style listings won’t open and close like they did before, trailers won’t embed, etc. Click on a tag to demo here. Is […]

Can not pass the value of google pie chart from custom meta box

I am trying to build a google pie chart in my wordpress site based on user input . Hence I have created custom meta box , and use wp_localize_script . Here is what I have done to pass custom metabox value to wordpress . function pie_load_scripts() { global $post; $title = get_post_meta($post->ID, ‘title’, true); $telecom […]