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 - 1 through 15 (of 43 total)

1 [2](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/?output_format=md)
[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/2/?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/#post-1813548)
 * Can anybody help me?
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/#post-1813551)
 * Try
 *     ```
       $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>
         <?php }
       ```
   
 *  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/#post-1813560)
 * Sorry, my pasted code is not correct, I’ve used code like give you. This doesn’t
   work for me.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/#post-1813635)
 * This:
 *     ```
       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 }
       ```
   
 * In your original code, `$pretty` is always `null`.
 *  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/#post-1813637)
 * Do you test this code? For me this code doesn’t work.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/#post-1813643)
 * Are you using that code in a function? If you are, did you declare `global $post;`?
 *  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/#post-1813645)
 * Yes to both questions.
 *  [santosh.sahoo](https://wordpress.org/support/users/santoshsahoo/)
 * (@santoshsahoo)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/#post-1813646)
 * use wp_print_script hook to include your script…eg:
 *     ```
       add_action("wp_print_scripts","my_func");
       function my_func(){<?
       <!--you can use wp_enqueue_scripts() instead of what i have written below -->
       <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.prettyPhoto.js"></script>
       <?php
       }
       ```
   
 * read about wp_print_script hook, and wp_enqueue_script() for better understanding.
   
   Hope this will solve ur prob.
 *  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/#post-1813647)
 * I know this and I’m use this hook.
    My function like this:
 *     ```
       function my_scripts() {
       	global $post;
       	if(!is_admin()){
       	wp_enqueue_script('jquery');
       	if ((is_single() || is_page()) && (strpos($post->post_content, 'rel="prettyPhoto"') > 1)) {
       	wp_enqueue_script( 'prettyPhotojs', get_bloginfo('template_directory').'/includes/js/jquery.prettyPhoto.js', array( 'jquery' ), '', true );
       	}
       	}
       }
   
       add_action('wp_print_scripts', 'my_scripts');
       ```
   
 * Doesn’t work.
 *  [santosh.sahoo](https://wordpress.org/support/users/santoshsahoo/)
 * (@santoshsahoo)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/#post-1813648)
 * i dont remember…but i think before using wp-enqueue_script you need to register
   that js file.
    will let u know shortly…5 mins…
 *  [santosh.sahoo](https://wordpress.org/support/users/santoshsahoo/)
 * (@santoshsahoo)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/#post-1813649)
 * [http://codex.wordpress.org/Function_Reference/wp_register_script](http://codex.wordpress.org/Function_Reference/wp_register_script)
   
   check the above link
 *  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/#post-1813650)
 * Thank you, but this doesn’t help me. This is not resolve my problem.
 *  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/#post-1813662)
 * Nobody knows? I think I’m not a first who trying resolve this problem.
 *  [Joseph](https://wordpress.org/support/users/jpe24524/)
 * (@jpe24524)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/#post-1813665)
 * [http://codex.wordpress.org/Function_Reference/wp_enqueue_script](http://codex.wordpress.org/Function_Reference/wp_enqueue_script)
   says to not use `wp_print_scripts`.
 * > This function will not work if it is called from a wp_head action, as the <
   > script> tags are output before wp_head runs. Instead, call wp_enqueue_script
   > from an init action function (to load it in all pages), template_redirect (
   > to load it in public pages only), or admin_print_scripts (for admin pages only).
   > Do not use wp_print_scripts
 * You need to hook it to `template_redirect`. `init` action won’t work for your
   case because it’s too early in the process so the conditional tags won’t work
   and `$post` is not yet set.
 *  [artbycrunk](https://wordpress.org/support/users/artbycrunk/)
 * (@artbycrunk)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/load-javascript-only-when-specify/#post-1813669)
 * this code needs to exist in your functions.php file..
    use this if u want to 
   load jquery from google api
 *     ```
       function my_init() {
       	if (!is_admin()) {
       		wp_deregister_script('jquery');
       		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js?ver=1.4.2', false, '1.4.2');
       		wp_enqueue_script('jquery');
       		}
       	}
   
       	add_action('init', 'my_init');
       ```
   
 * or use this to load wordpress local copy.. it is not required to give the path
   of the script.. as wordpress is already aware of all inbuilt libraries.
 *     ```
       function my_init() {
       	if (!is_admin()) {
       		wp_enqueue_script('jquery');
       		}
       	}
   
       	add_action('init', 'my_init');
       ```
   
 * now.. considering that u havent got a solution as yet.. i would like to ask when
   is it that you really want the jquery plugin to load..i mean is this prettyphoto
   specific.. or it should only load when anything inside your theme requires jquery?

Viewing 15 replies - 1 through 15 (of 43 total)

1 [2](https://wordpress.org/support/topic/load-javascript-only-when-specify/page/2/?output_format=md)
[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/2/?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
