I'm not sure I correctly "insert" scripts in my pages.
Concrete example: I need to generate a map with the google API, and decided to write a plugin to do that. Question now is what is the correct way to handle scrip insertion.
First attempt, my plugin file uses add_shortcode to attach a function that just returns <script type=\"text/javascript\" src=\"... in the middle of the page/post.
That works, but I understand it's better to put scripts in hearder/footer and I should enqueue them, only when needed.
So here's my second attempt:
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
add_shortcode( 'mymapshortcode', 'do_mymapshortcode');
function my_enqueue_scripts() {
// enqueue scripts if main query is for a page or post that has the shortcode
if ( isset($GLOBALS['wp_the_query'])
&& is_a($GLOBALS['wp_the_query']->get_queried_object(),'WP_Post')
&& has_shortcode($GLOBALS['wp_the_query']->get_queried_object()->post_content, 'mymapshortcode') ){
$target=plugin_dir_url( __FILE__ ) . 'mymapshortcode.js';
wp_enqueue_script('gmapscript','http://maps.google.com/maps/api/js?sensor=false');
wp_enqueue_script('mygmapscript',$target,false,null,false);
}
}
function do_mymapshortcode() {
// retrun a div for the map to be written
return "<div id=\"themap\" style=\"width:500px; height:500px;\">
<p>Loading map...</p><noscript><p>Cannot show map because js is disabled</p></noscript>
</div>";
}
?>
And then, if I'd decide to inline my script (so small it's not worth the overhead of fetching it from a separate file), I'd go with something like that:
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts2' );
add_action( 'wp_head', 'my_wp_head2' );
add_shortcode( 'mymapshortcode2', 'do_mymapshortcode2');
function my_wp_head2() {
// echo inline script if main query is for a page or post that has the shortcode
if ( isset($GLOBALS['wp_the_query'])
&& is_a($GLOBALS['wp_the_query']->get_queried_object(),'WP_Post')
&& has_shortcode($GLOBALS['wp_the_query']->get_queried_object()->post_content, 'mymapshortcode2') ){
$output= <<<'EOT'
<script>/* my min js goes here */ </script>
EOT;
echo $output;
}
}
function my_enqueue_scripts2() {
// enqueue script if main query is for a page or post that has the shortcode
if ( isset($GLOBALS['wp_the_query'])
&& is_a($GLOBALS['wp_the_query']->get_queried_object(),'WP_Post')
&& has_shortcode($GLOBALS['wp_the_query']->get_queried_object()->post_content, 'mymapshortcode2') ){
wp_enqueue_script('gmapscript2','http://maps.google.com/maps/api/js?sensor=false');
}
}
function do_mymapshortcode2() {
// retrun a div for the map to be written
return "<div id=\"themap\" style=\"width:500px; height:500px;\">
<p>Loading map...<noscript><p>Cannot show map because js is disabled</noscript></p>
</div>";
}
?>
Is it how it is supposed to be done or am I doing it wrong?
Also, is it possible / better to move the tests for has_shortcode to enclose the add_action so that it's tested only once, like the following:
if ( isset($GLOBALS['wp_the_query'])
&& is_a($GLOBALS['wp_the_query']->get_queried_object(),'WP_Post')
&& has_shortcode($GLOBALS['wp_the_query']->get_queried_object()->post_content, 'mymapshortcode') ){
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts2' );
add_action( 'wp_head', 'my_wp_head2' );
}