Enqueue script based on page condition

I’m trying to figure out how to enqueue files (javascript and styles) based on a condition- is_page() from within my plugin’s config file. For a week now, I’ve been trying to find a working solution out of the myriads I’ve found online.

Here’s the original from the file:

Read More
public function enqueue_files() {
        wp_enqueue_script("jquery");
        wp_enqueue_style("wpsqt-main",plugins_url('/css/main.css',WPSQT_FILE));
    }

Here’s what I attempted:

public function enqueue_files() {
if ( is_page(xxx) ) {
        wp_enqueue_script("jquery");
        wp_enqueue_style("wpsqt-main",plugins_url('/css/main.css',WPSQT_FILE));
    }
}

When I test if ( is_page() ), it removes the scripts and styles from the headers of all pages. However, when altering it to:
if ( !is_page() ), it adds it back. Weird?

  1. Is there a way to do include the condition?
  2. Does the public function portion alter the way the condition is read?
  3. Is it possible to remove the enqueue from the plugin’s config file and place it in my function.php file so whenever the plugin is updated, I don’t have to make changes?

Here’s the file (the call to enqueue the file is at the very bottom): http://pastebin.com/sDt4sTCH

I included the actual file so you can get the context it’s used in. Thanks in advance!!
EDIT better code format

Related posts

Leave a Reply

1 comment

  1. You’re calling is_page() too early, before there has been a query, so no $post is available to test with. And if (not is_page()) will always be true in this case. The way I circumvent this is to insert the scripts into the footer as opposed to the header (after there has been a query). Note, you need to have <?php wp_footer() ?> in your website footer, usually before </body>

    class MyPlugin {
        //In the constructor we want to set a few methods as action callbacks
        public function __construct () {
            add_action('init', array(&$this, 'register_my_scripts'));
            add_action('wp_print_footer_scripts', array(&$this, 'print_my_scripts'));
        }
    
        //This method is called by init, and registers our plugin scripts for later use
        public function register_my_scripts () {
            wp_enqueue_style("wpsqt-main",plugins_url('/css/main.css',WPSQT_FILE));
        }
    
        //This method is called by wp_print_footer_scripts, and prints our <script> through wp_footer()
        public function print_my_scripts () {
            //Only render the script on specific pages
            if ( is_page(xxx) ) {
                wp_print_scripts("wpsqt-main", array('jquery'));
            }
        }
    }
    

    Alternatively, you can use a global variable and simply set it to true wherever you want your script printed. For instance, if you don’t know the page name. Simply replace the print_my_scripts method above with this one.

    public function print_my_scripts () {
        global $print_my_script;
        //Only render the script on pages which have asked for it
        if ($print_my_script) {
            wp_print_scripts("wpsqt-main", array('jquery'));
        }
    }
    

    And add this to any shortcode, widget, page template.. anything to tell our script to print on the page using the shortcode, widget, template.. and so on.

    global $print_my_script ;
    $print_my_script = true ;