So when WooCommerce updated to version 2 they replaced the fancybox lightbox scripts with prettyphoto. I would like to use this on all my wordpress site's pages, not just the product pages, but the scripts are only loaded on a product page. I have a jQuery script to add the relevant css class to my image links but I need a filter/hook to make sure the actual scripts are loaded.

Can anyone help?

  • Is your jQuery script being properly enqueued using WP functions? In that case you can probably simply add what is needed to dependencies. Please provide some more information on what exactly you are doing and how it ties to woocommerce code. – Rarst May 4 '13 at 19:17
  • Yes jQuery is loaded on all pages. Is it just a case of using wp_enqueue_script to load the relevant prettyphoto scripts? I'm not sure of the handles woocommerce uses or how many files are needed. I'm guessing a js and a css – harryg May 7 '13 at 12:35

Try adding this to your theme's function.php. It worked for me, hope it helps.


add_action( 'wp_enqueue_scripts', 'lightbox' );
function lightbox() {
global $woocommerce;
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script( 'prettyPhoto', $woocommerce->plugin_url() . '/assets/js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js', array( 'jquery' ), $woocommerce->version, true );
wp_enqueue_script( 'prettyPhoto-init', $woocommerce->plugin_url() . '/assets/js/prettyPhoto/jquery.prettyPhoto.init' . $suffix . '.js', array( 'jquery' ), $woocommerce->version, true );
wp_enqueue_style( 'woocommerce_prettyPhoto_css', $woocommerce->plugin_url() . '/assets/css/prettyPhoto.css' );
| improve this answer | |

for new woocommerce 2.1.2 you'll need to use data-rel="prettyPhoto"

/* add rel prettyphoto to all images */
function autoadd_rel_prettyPhoto($content) {
 global $post;
 $pattern = "/(<a(?![^>]*?data-rel=['\"]prettyPhoto.*)[^>]*?href=['\"][^'\"]+?\.(?:bmp|gif|jpg|jpeg|png)['\"][^\>]*)>/i";
 $replacement = '$1 data-rel="prettyPhoto['.$post->ID.']">';
 $content = preg_replace($pattern, $replacement, $content);
 return $content;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.