What is the “proper” way to generate a javascript variable depending on a custom field value?

You may wish to look at this question. In that question, the admin-ajax url is passed through, but it can be any variable (or array of variables), like for instance $nameAttribute. For instance, you would call wp_localize_script(‘my_js_file_handle’,’my_js_object’,array(‘attribute’=>$nameAttribute))` then in your js file, the variable would be accessible from my_js_object.attribute. (You must enqueue the javascript file … Read more

Pass data from wordpress to javascript in JSON

Try wrapping your array in json_encode(): function add_map_data() { $objName = “MapData”; $array = array( “MapViewLatitude” => “51.505”, “MapViewLongitude” => “-0.09”, ); wp_enqueue_script( ‘mapdata’, get_bloginfo(‘template_url’).’/custom/map.js’, null, null, false ); wp_localize_script( ‘mapdata’, $objName, array(‘my_arr’ =>json_encode($array))); } add_action( ‘wp_enqueue_scripts’, ‘add_map_data’); Next step is to parse the JSON string back to a JSON object in JavaScript so: //first … Read more

JavaScript && operator in visual editor

You should use wp_enqueue_script for your javascript files instead of adding the code directly into the post content. ps: You can check out the callbacks on the the_content filter, using add_action(‘wp_footer’,function(){ global $wp_filter; printf(‘<pre>%s</pre>’,print_r( $wp_filter[‘the_content’],true)); }); to display them in the footer part of your theme. Then you will see callbacks like wpautop, wptexturize, convert_chars … Read more

Resolving Javascript errors likely related to Gantry framework

Fixed. Thanks to some feedback on reddit (see this thread and this one), I learned that the issue was that the mootools.js script was being loaded far too late. I solved it (rather inelegantly, I’m sure) by adding this line to themes/rt_gantry_wp/index.php, around line 30 or so (before the if blocks): $gantry->addScript(‘/wp-content/plugins/gantry/js/mootools.js’); It’s no longer … Read more

How to add custom javascript into from custom plugin

You must use the hook to enqueue the scripts in WordPress. Use the hook wp_enqueue_scriptsfor the front end and admin_enqueue_scripts for the back end side. If you are enqueueing scripts and styles, you will want to use one of these three hooks: wp_enqueue_scripts (for the frontend) login_enqueue_scripts (for the login screen) admin_enqueue_scripts (for the admin … Read more

Any required callbacks to WP.com or WP.org?

It is a little hard to quickly audit WP code base for wordpress.org links, because they are extensively used in inline documentation. On top of my head the main (if not only) requests are probably to api.wordpress.org for updates. For resources WP generally only uses bundled assets. The two notable exceptions are: Deprecated libraries (such … Read more