1

I have the following code in my functions.php to show a lightbox on the Simple Paypal Shopping Cart Plugin

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' );

When I add something the the cart I get the warning:

Warning: Missing argument 2 for make_wpspsc_thumbs_lightbox_previews()

On line 16 which is this:

function make_wpspsc_thumbs_lightbox_previews( $thumbnail, $atts ) {

Can anyone see what is causing the problem please?

1 Answer 1

1

You didn't specify how many arguments your function accepts when you used add_filter:

add_filter( $tag, $function_to_add, $priority, $accepted_args );

Your code only passes a $tag and a $function_to_add, but it doesn't tell WordPress what priority it should be, or how many accepted arguments your function takes. You need to fix that. If you don't provide the 4th parameter, WordPress assumes your function only accepts 1 argument, even if you define 2 in your function ( $thumbnail and $atts ).

Also, if the filter only provides a single argument, then WordPress can't pull a second argument out of thin air

3
  • Thanks for your help, I've tried your code but I'm still getting the same argument, could you show me what code I should replace please?
    – Lowis
    Commented Jan 26, 2016 at 14:06
  • My code wasn't a copy paste solution, it requires that you have a basic understanding of PHP. Look at your add_filter call, you only give it 2 arguments, you need to provide a priority and how many arguments your function will accept. Your function accepts 2 arguments, $thumbnail, and $atts, but you haven't told WordPress this
    – Tom J Nowell
    Commented Jan 26, 2016 at 17:18
  • It's OK I managed to get it sorted on Reddit
    – Lowis
    Commented Jan 28, 2016 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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