Forums

JS adding to only specific page(s) (2 posts)

  1. AlpDmr
    Member
    Posted 2 years ago #

    Hi all, i've searched many place to do it but i couldn't find any solution..
    I am developing a plugin and i want that: if users activate my plugin on specific page(s) then let my plugin puts header scripts via wp_enqueue_script command. I mean, if my plugin is used in that page then let the necessary scripts will be enqueued. If plugin is not used in that page wp_enqueue_script command of my plugin will not be executed..
    Is it possible?
    Extra Info: My plugin gets the content of pages and changes some words with others..

    Thanks right now..

  2. stvwlf
    Member
    Posted 2 years ago #

    Hi

    While not a direct answer to your question, look at this article:
    http://justintadlock.com/archives/2009/08/06/how-to-disable-scripts-and-styles

    and this comment after the article by the author of the Contact Form 7 plugin
    http://justintadlock.com/archives/2009/08/06/how-to-disable-scripts-and-styles#comment-145663
    which points to an article explaining how Contact Form 7 has enabled an option to not load the plugin's JS on every page
    http://ideasilo.wordpress.com/2009/05/31/contact-form-7-1-10/

    I don't find the method employed very user friendly but its a starting point. Perhaps you could build an array of page ID's on which the javascript was supposed to be included (or excluded, depending on how you present this to the user) and check that array in your plugin before enqueing the script, so the enqueues are conditional based on whether the user wants the JS loaded on that page.
    You could either add a check box on each page of the PageEdit form, or on the plugin admin page have a list of pages that they could check off to include (or exclude).

    Cforms 2 has a field in which you can enter a list of ID #'s on which you want the plugin's code added. But that requires the user to look up the ID of each page and enter it as a number, not a high motivation to do that work for most people.

    If you build something like I described, I suggest adding what you did as a comment on the first link I listed above. I'm sure many people would be interested in seeing the coding - I would.

Topic Closed

This topic has been closed to new replies.

About this Topic