Title: Load javascript only when specify.
Last modified: August 19, 2016

---

# Load javascript only when specify.

 *  [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/)
 * Hi. Please help me load jquery plugin only when they should be calling.
    Now 
   I’m talking about prettyPhoto plugin. I found this tips
 *     ```
       $pretty = ($pretty || ((is_single() || is_page()) &&
        (strpos($post->post_content, 'rel="prettyPhoto"') > 1)));
         if ($pretty) {
         <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.prettyPhoto.js"></script>
         }
       ```
   
 * but this doesn’t work. This code delete plugin from all pages.
    Please help me.

Viewing 13 replies - 31 through 43 (of 43 total)

[←](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/?output_format=md)
[1](https://wordpress.org/support/topic/load-javascript-only-when-specify/?output_format=md)
[2](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/?output_format=md)
3

 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813709)
 * I’m sorry but this function doesn’t work for me. I think in your code must be
   this `'rel="prettyPhoto"'`?
    Edit: Oh I see. But nothing.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813710)
 * That’s odd.. I’ve tried it with the plugin (WP-prettyPhoto) installed and the
   script is been correctly enqueued according to the condition.
 * Do you mind showing me an example page?
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813711)
 * My site at this time only on local mashine. What code do you need?
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813713)
 * Sorry, my mistake…
 * I forgot to remove the code in footer.php so I thought the hook works but it 
   indeed doesn’t.
 * I’ve been testing it and even though the if statement condition is correct, enqueue
   only works when I remove the `strpos` part from the condition. I’m stumped…
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813714)
 * Ok, seems like like wordpress runs through the filter twice, once somewhere in
   the head and another at the hook (the_content). `wp_enqueue_script` only seems
   to work when it’s called earlier and `the_content` hook is too late in the process,
   hence the reason why the code only works when the `strpos` part is removed (rel
   has not been added yet).
 * Here’s a solution:
 * Use the following filter to check and set a global variable.
 *     ```
       function my_prettyphoto_script($content) {
       	global $pretty;
       	if ((is_single() || is_page()) && (strpos($content, 'rel="prettyPhoto') > 1))
       		$pretty = true;
   
       	return $content;
       }
       add_filter('the_content', 'my_prettyphoto_script', 100, 1);
       ```
   
 * Put the following in footer.php after `wp_footer();` to add the script if `$pretty`
   is true.
 *     ```
       <?php global $pretty; if ($pretty === true) { ?><script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/includes/js/jquery.prettyPhoto.js"></script><?php } ?>
       ```
   
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813716)
 * Is this solution work for you? For me this doesn’t work. I try to check all my
   files, maybe something is wrong.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813718)
 * Yes, it works for me.
 * Edit: Just so there’s no confusion, what I mean by it works is the js script 
   link is added correctly. The plugin itself doesn’t seem to work on my test blog
   though. All I get is a lightbox with no images.
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813723)
 * In your source code prettyPhoto.js is present? I don’t know what’s going wrong.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813724)
 * Yes, it’s present on pages that matches the conditions.
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813730)
 * Can you test for yourself this [code](http://www.mikoder.com.au/2009/07/selectively-include-javascript-files)?
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813733)
 * Hmmm… That’s what we started with isn’t it? I’ve already explained why it doesn’t
   work.
 * Here it is again. The plugin adds `rel="prettyPhoto"` when `the_content()` is
   called, it doesn’t exist in post_content (database), so you can’t add the script
   using `wp_enqueue_script` because it’s too late for the function to work when
   wordpress starts loading the template files.
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813734)
 * No, we start only from bit of this code. Link looks like plugin.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813735)
 * Sure it’s a plugin but the inside is still the same so it’s not going to work.

Viewing 13 replies - 31 through 43 (of 43 total)

[←](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/?output_format=md)
[1](https://wordpress.org/support/topic/load-javascript-only-when-specify/?output_format=md)
[2](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/?output_format=md)
3

The topic ‘Load javascript only when specify.’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 43 replies
 * 4 participants
 * Last reply from: [Joseph](https://wordpress.org/support/users/jpe24524/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/3/#post-1813735)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
