• Resolved artflutter

    (@artflutter)


    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() {
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter artflutter

    (@artflutter)

    I have been investigating and found that almost every WordPress plugin includes its required javascript using the wp_head function. Surely there must be a more efficient way, especially since some plugins are only ever used on one page (i.e. contact form plugin). I have been able to include some of the javascript within the body but I have not been able to get the wp_enqueue function to work within the body of my function.

    I am trying to include mootools which is quite large and only used on one page of my site, so obviouslly I don’t want it to load on every page. I could try including it in the body but I want to use the wp_enqueue function to avoid any conflicts with the many other plugins which also use mootools.

    I would appreciate some advice/thoughts from other plugin authors.

    if you are writing this just for your own usage, I can make a recommendation.. add your own hook, and call it on the page(s) you need it.

    Ive added a cpl of my own hooks, one of which is just an alternative to wp_head.

    If, on the other hand, you will be sharing your plugin, that might not be such a good idea.

    Thread Starter artflutter

    (@artflutter)

    Well I do plan on sharing it if I eventually get it working. I’ll just wp_enqueue mootools and the js and css files I can include at the bottom of the body.

    I tore the wp-head hook out of a plugin for exactly the same reason, only needed the js on one (not all pages).

    I put a conditional location test into the head section to just load it on the page I needed the js on. You could build a admin page that simply accepts post Ids of pages you want your js to load on, then feed these into the conditional statement, which would be more elegant than dipping into the plugin code to set these post numbers… Just a suggestion (would also make the plugin very portable and easy to upgrade!

    Thread Starter artflutter

    (@artflutter)

    Thanks ninjaboy, I’m glad I’m not the only person frustrated by the many javascript libraries which slow down the loading of every site. While yours is a good idea and would work well, the yahoo developer network actually recommends inserting javascript at the end of the <body>, which I have done so far with no ill-effects. This has the benefit of the page with the plugin loading faster and unnecessary script wont be loaded in the <head> of every page.

    On a side note I have been using Packer to compress all my javascript files which has also speed up site loading times. It can shrink the 180kb uncompressed mootools library down to just 19kb with gzip compression.

    man, 19k… I wouldn’t even worry about it!!

    At the end of body… That’s a new one for me… I’ll have to take a look at that!

    Correct me if I’m wrong, but wouldn’t that still load the js on every page still??

    What’s the js do anyway, you mentioned you may release it as a plugin, I have developed a couple of plugin that have admin pages and store options in the database… I may be interested in helping out on development!!

    so am i right in thinking that there is no plugin or hack that could somehow restrict plugins from putting scripts in wp_head on pages where the plugin is not in use?

    i’ve read elsewhere that conditionals could be used on per-plugin basis… what about within a theme’s functions.php?

    pardon my ignorance if these question are silly
    just trying to clean up :>)

    Edit: I wholeheartedly support this idea

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘including javascript only on pages where plugin is used’ is closed to new replies.