• 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)
  • Thread Starter Deniska

    (@deniska)

    Can anybody help me?

    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

    (@deniska)

    Sorry, my pasted code is not correct, I’ve used code like give you. This doesn’t work for me.

    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

    (@deniska)

    Do you test this code? For me this code doesn’t work.

    Are you using that code in a function? If you are, did you declare global $post;?

    Thread Starter Deniska

    (@deniska)

    Yes to both questions.

    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

    (@deniska)

    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.

    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…

    Thread Starter Deniska

    (@deniska)

    Thank you, but this doesn’t help me. This is not resolve my problem.

    Thread Starter Deniska

    (@deniska)

    Nobody knows? I think I’m not a first who trying resolve this problem.

    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.

    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)
  • The topic ‘Load javascript only when specify.’ is closed to new replies.