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

How to retrieve a value from an input field in a media view template

After digging through the media modal code, I came up with the answer to my question: wp.media.controller.Custom = wp.media.controller.State.extend({ initialize: function(){ this.props = new Backbone.Model(); }, // called when the toolbar button is clicked customAction: function( controller ){ // get the value of a media view template form field using // this.props.get(‘key’), where ‘key’ is … Read more

AngularJS with route and JSON API

Doublecheck if there’s a “nobase” error in your console. If so, according to the AngularJS documentation, and fix.io (credits for this answer go to the author of that post) the base tag is to the rescue. <html ng-app=”app”> <head> <base href=”https://wordpress.stackexchange.com/angular-wp/”> <title>AngularJS Demo Theme</title> <?php wp_head(); ?> Please note the “/angular-wp/” is the sub-context in … Read more