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 15 replies - 16 through 30 (of 43 total)

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

 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/#post-1813680)
 * This is only for prettyPhoto. Only for content that have “rel=’prettyPhoto'” 
   inside.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/#post-1813681)
 * Have you tried using the `template_redirect` hook instead of `wp_print_scripts`?
 *  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/2/#post-1813686)
 * I’m not sure but this hook works only for templates that I choose. If I add rel
   =prettyPhoto to images that not belong to my templates this script doesn’t load.
   This is not universal. If I’m not right please explain how this hook work.
    I
   found [this snippet](http://yoast.com/conditional-thickbox-loading/), 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/2/#post-1813696)
 * If the hooks doesn’t work, you could just do the if statement in your footer.
   php after `wp_footer()`.
 *     ```
       <?php if ((is_single() || is_page()) && (strpos($post->post_content, 'rel="prettyPhoto"') > 1)) { ?>
       <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/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/2/#post-1813697)
 * Nothing! I don’t know why this doesn’t work for me. Is there any way to check
   what return this function, e.g. `print_r`? [Another way](http://www.carrcommunications.com/2010/11/add-javascript-to-one-page-only-in-wordpress/)
   to do this and nothing for me.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/#post-1813698)
 * Try checking `$wp_scripts` global variable.
 * So doing a simple if statement in the footer doesn’t work? Did you copy paste
   exactly what I posted because I just noticed you have `/includes/` in one of 
   the code you’ve posted?
 * If doing the if statement without using `wp_enqueue_script` doesn’t work then
   that would suggest the condition is wrong.
 *  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/2/#post-1813699)
 * I copy/paste your code and add `/includes/`. I check my code before save.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/#post-1813700)
 * Then that means there’s something wrong with your if statement condition.
 * You wrote
 * > Only for content that have **“rel=’prettyPhoto'”** inside.
 * but you using double quotes `"prettyPhoto"` in the code?
 *  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/2/#post-1813701)
 * I’m checking global variable `$wp_scripts` and they show all scripts that my 
   blog have and prettyPhoto is not present.
 *  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/2/#post-1813702)
 * No, in code I have `'rel="prettyPhoto"'`, in template I have `rel="prettyPhoto"`.
   Quotes the same.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/#post-1813703)
 * Well, like I’ve said, if using the following code after `wp_footer();` in footer.
   php (not in a function) doesn’t work then it means your condition is wrong.
 *     ```
       <?php if ((is_single() || is_page()) && (strpos($post->post_content, 'rel="prettyPhoto"') > 1)) { ?>
       <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/includes/js/jquery.prettyPhoto.js"></script>
       <?php } ?>
       ```
   
 * I don’t use prettyphoto so I changed `'rel="prettyPhoto"'` to something else 
   and tried it on my test blog. It works fine. The js code is displaying in the
   source code of the desired 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/2/#post-1813704)
 * It’s to complicated. I have the same code like you past. I don’t know whats 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/2/#post-1813706)
 * I had assume your condition was correct from the start so it’s never crossed 
   my mind but I’m pretty sure the reason it’s not working is because `rel="prettyPhoto"`
   doesn’t exist in `post_content`. The plugin should be adding it in through `the_content`
   filter everytime the page generates.
 * Does the plugin add `rel="prettyPhoto"` to every `<img>` tag? If so, you can 
   just check for `<img>` tags with `strpos($post->post_content, '<img') !== false`
 *  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/2/#post-1813707)
 * No, only for some images, not for all.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/#post-1813708)
 * Ok, try this:
 *     ```
       function my_prettyphoto_script($content) {
       	if ((is_single() || is_page()) && (strpos($content, 'rel="prettyPhoto') > 1)) {
       		wp_enqueue_script( 'prettyPhotojs', get_bloginfo('template_directory').'/includes/js/jquery.prettyPhoto.js', array( 'jquery' ), '', true );
       	}
       	return $content;
       }
       add_filter('the_content', 'my_prettyphoto_script', 100, 1);
       ```
   
 * Adding a condition to enqueue prettyphoto script after the rels have been added
   through `the_content` filter.
 * Edit: I’ve tried the plugin and I left out the closing double quote in `'rel="
   prettyphoto'` because it adds the post ID after it.

Viewing 15 replies - 16 through 30 (of 43 total)

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

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
