Most efficient way to add javascript file to specific post and/or pages?

I am wondering what the most efficient method is to add a javascript file specifically for a post and/or page. Here are a few solutions I came up with: Switch to HTML editing view and post your JavaScript in there (pretty bad solution) Custom fields with the specific JavaScript for that post/page in the key … Read more

How to enqueue scripts on custom post add/edit pages?

I’m trying to enqueue a JS script only when someone is adding or editing a custom post type I created called “recipes”. Currently the script works ok when I do this: if (is_admin()){ wp_enqueue_script( ‘my-script’ ); } But this loads it in every admin page, I’m assuming I need to hook it to a function … Read more

Could the WP script/style loader be used to concatenate and gzip scripts and styles in the front-end?

WP has a nice javascript loader included in wp-admin: http://core.trac.wordpress.org/browser/tags/3.0.4/wp-admin/load-scripts.php and a CSS loader: http://core.trac.wordpress.org/browser/tags/3.0.4/wp-admin/load-styles.php I was wondering if it’s possible to use them in the front-end too, not just admin, because they can concatenate all enqueued scripts, and serve them as a single gzipped file 4 late answer From a brief look: You’d have … Read more

How to add defer=”defer” tag in plugin javascripts?

I couldn’t add defer tag in plugin javascripts. Google developer pagespeed test suggests me to add defer tag in contact form 7 javascripts. This is how contact form 7 includes javascript in header. add_action( ‘wp_enqueue_scripts’, ‘wpcf7_enqueue_scripts’ ); function wpcf7_enqueue_scripts() { // jquery.form.js originally bundled with WordPress is out of date and deprecated // so we … Read more

Is it possible to reuse wp.media.editor Modal for dialogs other than media

To expand: I’d like to utilize the same Modal code/appearance ( as used in wp.media.Modal, wp.media.FocusManager ) to open a modal of my own custom dialog, not the Media Editor. In the past, I’ve used thickbox for this sort of thing, but wp.media.Modal appears to be the way of the future for modals — Not … Read more

WP Rest API – How to get featured image

I’m very new to this API, in fact I’ve only spent couple hours on it so far. I’ve done my research but cannot find anything about it… The issue is, I can’t seem to get the featured image of a post. The JSON returns “featured_media: 0”. getPosts: function() { var burl = “http://www.example.com/wp-json/wp/v2/posts”; var dataDiv … Read more

HTTP Status 405 – Request method ‘POST’ not supported (Spring MVC)

I found the problem that was causing the HTTP error. In the setFalse() function that is triggered by the Save button my code was trying to submit the form that contained the button. function setFalse(){ document.getElementById(“hasId”).value =”false”; document.deliveryForm.submit(); document.submitForm.submit(); when I remove the document.submitForm.submit(); it works: function setFalse(){ document.getElementById(“hasId”).value =”false”; document.deliveryForm.submit() @Roger Lindsjö Thank you for spotting my error … Read more

Get all unique values in a JavaScript array (remove duplicates)

With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values: function onlyUnique(value, index, self) { return self.indexOf(value) === index; } // usage example: var a = [‘a’, 1, ‘a’, 2, ‘1’]; var unique = a.filter(onlyUnique); console.log(unique); // [‘a’, 1, 2, ‘1’]  Run code snippetExpand … Read more