Joseph
Forum Replies Created
-
Forum: Fixing WordPress
In reply to: Load javascript only when specify.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…Forum: Fixing WordPress
In reply to: Load javascript only when specify.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?
Forum: Fixing WordPress
In reply to: Load javascript only when specify.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.Forum: Fixing WordPress
In reply to: Load javascript only when specify.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 inpost_content
. The plugin should be adding it in throughthe_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 withstrpos($post->post_content, '<img') !== false
Forum: Fixing WordPress
In reply to: Load javascript only when specify.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.Forum: Fixing WordPress
In reply to: Load javascript only when specify.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?Just add a link in gallery.php template and make the file name dynamic depending on how you name them.
Resize your images to the desire size before uploading or use NGG’s automatic resize function set under the images setting.
Edit: You could try setting CSS width and height.
Forum: Fixing WordPress
In reply to: Load javascript only when specify.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.Forum: Fixing WordPress
In reply to: Load javascript only when specify.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 } ?>
Forum: Fixing WordPress
In reply to: Load javascript only when specify.Have you tried using the
template_redirect
hook instead ofwp_print_scripts
?Forum: Fixing WordPress
In reply to: Load javascript only when specify.https://codex.www.ads-software.com/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.Forum: Fixing WordPress
In reply to: Load javascript only when specify.Are you using that code in a function? If you are, did you declare
global $post;
?Forum: Fixing WordPress
In reply to: Load javascript only when specify.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 alwaysnull
.Forum: Fixing WordPress
In reply to: Load javascript only when specify.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 }