Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active May 7, 2020 18:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bekarice/57dbb20278683319d3e1 to your computer and use it in GitHub Desktop.
Save bekarice/57dbb20278683319d3e1 to your computer and use it in GitHub Desktop.
Make the product box images in WordPress Simple PayPal Shopping Cart lightbox previews instead
/**
* Let's make the product box images in WPSPSC lightbox previews instead
*
* The plugin is here: https://wordpress.org/plugins/wordpress-simple-paypal-shopping-cart
* Using this lightbox plugin: https://wordpress.org/plugins/responsive-lightbox/
*/
function make_wpspsc_thumbs_lightbox_previews( $thumbnail, $atts ) {
// Code Credit: http://stackoverflow.com/questions/19323324/how-to-get-image-src-attribute-value-from-php-string
$doc = new DOMDocument();
// Let's make sure we don't execute this code on the wrong elements & blow up the site
if ( ! is_object( $doc ) ) {
return $thumbnail;
}
libxml_use_internal_errors(true);
$doc->loadHTML( $thumbnail );
$xpath = new DOMXPath($doc);
$imgs = $xpath->query("//img");
for ($i=0; $i < $imgs->length; $i++) {
$img = $imgs->item($i);
$src = $img->getAttribute("src");
}
// Now let's use the image src we just got & rel="lightbox" because that's what Responsive Lightbox from dFactory uses
$new_thumbnail = '<a href="'.$src.'" rel="lightbox">'.$thumbnail.'</a>';
return $new_thumbnail;
}
add_filter( 'wspsc_product_box_thumbnail_code', 'make_wpspsc_thumbs_lightbox_previews' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment