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.
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?
My site at this time only on local mashine. What code do you need?
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…
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 } ?>
Is this solution work for you? For me this doesn’t work. I try to check all my files, maybe something is wrong.
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.
In your source code prettyPhoto.js is present? I don’t know what’s going wrong.
Yes, it’s present on pages that matches the conditions.
Can you test for yourself this code?
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.
No, we start only from bit of this code. Link looks like plugin.
Sure it’s a plugin but the inside is still the same so it’s not going to work.