Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm learning about plugins and shortcodes. I noticed that when I activate my plugin its code gets loaded on all of my pages-- even pages that don't have my shortcode. (I don't mean content vs. admin pages). On some content pages, I use a particular shortcode and on other content pages, I don't-- but in any case, the plugin's code is loaded. How do I make it so that the the plugin is included only on pages where the shortcode is used?

More Detail:

Let's say I've got a plugin that makes a lightbox. I activate the plugin. On my "Cool Images" page, I use shortcode to make a lightbox. When I check View Source on my "About" page, which doesn't use the lightbox shortcode, I see that the lightbox plugin's code has been loaded. My pages will load faster if I write my plugin so that its code is only loaded on the pages where it's needed. Is this possible? Otherwise, I'll have code for lots of plugins loaded on pages unnecessarily. Any ideas?

share|improve this question
1  
Are you trying to enqueue something, or trying to execute a PHP script on pages that have the shortcode? –  m0r7if3r Jan 9 '12 at 1:09
    
+1 @m0r7if3r - I also would like to understand that ..The approaches would be very different –  krembo99 Jan 9 '12 at 5:50
    
@mrOr7if3r, Thanks for the message. For example, let's say I've got a Google Maps plugin. Does that plugin have to be loaded on every page-- i.e. pages that don't use the plugin? Let's say I have an "About" page which doesn't have the Google Maps shortcode on it. If I look at View Source, I see that the plugin's code was loaded on that page (event though I'm not using). Does code for plugins have to load on every single page? Thanks. –  Laxmidi Jan 9 '12 at 17:41
    
Good question which imho leads to (new) best practice, as WP now enables us to enqueue scripts at any point in time. –  Raphael Jan 25 '12 at 12:06

5 Answers 5

Could do something along the lines of (in the header.php):

<?php
        if ( have_posts() && ( is_page() || is_single() ) ) {
            $content = get_the_content(the_post());
            if ( stripos( $content , '[your-shortcode') ) { function_for_scripts(); } 
        }   
?>

Checks if current load is page or post, then grabs content and searches for your shortcode. Upon finding it, it executes your function.

share|improve this answer
    
Thank you for your message. I tried using your solution, but, unfortunately, I couldn't get it to work. –  Laxmidi Jan 25 '12 at 6:10
    
Was added to the header? –  Noel Tock Jan 25 '12 at 7:09
    
Have you already seen get_shortcode_regex()? –  kaiser Feb 7 at 16:11

Check for admin UI variables

One menu item is a special case: Comments, as they get registered using the add_WHATEVER_(submenu)page() API.

// All need to be stated as beeing global
global $pagenow, $typenow, $hook_suffix, $parent_file, $submenu_file, $post_type_object;
if ( 'THE-OUTPUT.php' === $WHATEVER_YOU_CHOOSE_TO_CHECK )
    // do stuff

These are non-consitent and hardcoded into wp core. Note that not all are set on all pages.

Hook into page-specific admin UI hooks

Then there're also special, page-specific hooks, that you can take a look at in admin-footer.php and admin-header.php:

// Examples:
// Header
"admin_head-$hook_suffix"
"admin_print_styles-$hook_suffix"
"admin_print_scripts-$hook_suffix"
// Footer
"admin_footer-$hook_suffix"

Some examples in the real world: Post Screen

// Examples how the result looks like
admin_print_styles-post.php
admin_print_styles-post-new.php

Then there's also the $hook_suffix that you can check against for enqueueing scripts, right when you're hooking your action:

do_action( 'admin_enqueue_scripts', $hook_suffix );

Update

To get easier (1-click) access to this data/info, we built a freely available, developer friendly plugin named "(WCM) Current Admin Info", which is available on GitHub. The plugin can in the near future also be found in the official wp.org repository.

Screen shots

To give a preview of what you get with this plugin:

Screen

Hooks

Globals

share|improve this answer

The simplest way is to just check for the shortcode.

function your_scripts()
{ 
    global $post;
    wp_register_script('your-script',$the_path_to_your_script,'0.1',true);
    if ( strstr( $post->post_content, '[your-shortcode' ) ) 
    {
        wp_enqueue_script('your-script');
    }
}
add_action( 'wp_enqueue_scripts', 'your_scripts');
share|improve this answer
    
There's get_shortcode_regex() for that. –  kaiser Feb 7 at 16:10

I found this solution:

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );

function my_deregister_javascript() {
    wp_deregister_script( 'my_scripts_handle' );
}
share|improve this answer
1  
But you solutions will remove scripts from all pages –  Mamaduka Jan 25 '12 at 7:12

WP v3.3 gave us the ability to run wp_enqueue_script in the middle of a page. Ticket 9346

This has made it much easier to include your JS with better granularity (when using shortcodes, at least). Here, jquery will be only included when our shortcode is fired.

function get_slideshow() {
  // Do some stuff...

  // Load up scripts right from within our shortcode function (requires WP 3.3+)
  wp_enqueue_script( 'jquery', array(), null, true  ); 

  return;
}
add_shortcode( 'cool_slideshow', 'get_slideshow' );
share|improve this answer
    
This is how it should be done, imo. –  Raphael Jan 25 '12 at 12:06
    
Excellent, I hadn't realised it was now possible to run wp_enqueue_script in the middle of a page. A much cleaner way to only load scripts / css when needed! –  Rick Curran Feb 10 '12 at 15:48
    
Yes, this is so handy. I get a little giddy every time get the chance to do it. :p –  goto10 Feb 10 '12 at 17:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.