including javascript only on pages where plugin is used
-
Hi, I am writing a plugin which uses a couple of external javascript libraries, specifically mootools.
I want to include the mootools javascript library only on the page where my plugin is actually used. I have used the code below in my plugin but unfortunately when my plugin is enabled this code is executed on every page.function myplugin_init() { wp_enqueue_script('mootools', '/wp-content/plugins/LightForm/js/mootools.js', false, '1.11' ); } add_action('init', 'myplugin_init');
The part of my plugin which prints out stuff is myplugin_content so I thought that if I added the javascript there then it would only be included on a page that my plugin is actually used on. Unfortunately, while I got no errors the file wasn’t actually included.
<?php function myplugin_content() { add_action('init', 'myplugin_init'); }
add_shortcode(‘LightForm’, ‘LightForm_Body’);
Is there any way to only include the javascript file when my plugin is used? I know for the admin menu you can use code like below, but there is no similar hook available for a non-admin page.
$mypage = add_management_page( 'myplugin', 'myplugin', 9, __FILE__, 'myplugin_admin_page' ); add_action( "admin_print_scripts-$mypage", 'myplugin_admin_head' ); function myplugin_admin_head() { }
- The topic ‘including javascript only on pages where plugin is used’ is closed to new replies.