Skip to content

Instantly share code, notes, and snippets.

@kadimi
Created April 9, 2014 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadimi/10285884 to your computer and use it in GitHub Desktop.
Save kadimi/10285884 to your computer and use it in GitHub Desktop.
Add Javascript in header for a post, a page or a custom type post, add your code in a custom field with the name "js"
<?php
/**
* Add JavaScript code to the head section
*
* Add a custom field with the name "js" and this plugin will
* add it content to the head section of the page/post wrapped
* correctly inside <script> tags.
*/
add_action('wp_head', 'per_post_head_js');
function per_post_head_js() {
if ( $post = get_post(get_query_var('p')) ) {
echo $js = get_post_meta( $post->ID, 'js', /* $single = */ true );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment