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

    (@deniska)

    This is only for prettyPhoto. Only for content that have “rel=’prettyPhoto'” inside.

    Have you tried using the template_redirect hook instead of wp_print_scripts?

    Thread Starter Deniska

    (@deniska)

    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, but nothing.

    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

    (@deniska)

    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 to do this and nothing for me.

    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

    (@deniska)

    I copy/paste your code and add /includes/. I check my code before save.

    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

    (@deniska)

    I’m checking global variable $wp_scripts and they show all scripts that my blog have and prettyPhoto is not present.

    Thread Starter Deniska

    (@deniska)

    No, in code I have 'rel="prettyPhoto"', in template I have rel="prettyPhoto". Quotes the same.

    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

    (@deniska)

    It’s to complicated. I have the same code like you past. I don’t know whats wrong.((

    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

    (@deniska)

    No, only for some images, not for all.

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