0

I found this great bit of code that works very well thanks to:

http://www.sant-media.co.uk/2010/07/how-to-use-prettyphoto-in-wordpress-galleries/

add_filter( 'wp_get_attachment_link', 'lod_prettyPhoto');

function lod_prettyPhoto ($content) {
    $content = preg_replace("/<a/","<a rel=\"prettyPhoto[gallery1]\"",$content,1);
    return $content;
}

I want to use the native WordPress gallery and use prettyPhoto as jquery lightbox, so far so good.

I am learning PHP and I believe what I am doing above is finding and replacing the link tag and replacing it with a link tag and the rel = "prettyPhoto[gallery1]"

But I want the rel tag to be generated by the alt field that the client can complete and in that way have multiple prettyPhoto galleries (and not just one photo gallery called "gallery1").

So I tried reworking the code ( pasted below ), creating a variable for the alt tag, and then using this variable inside the rel tag of my code, but on the line where I created the variable I get a PHP error:

syntax error, unexpected '(', expecting T_VARIABLE or '$' in ...

and a Google search hasn't helped me figure out what I'm doing wrong either.

Any help would be greatly appreciated! _Cindy

My new code with dynamically generated rel tags:

add_filter( 'wp_get_attachment_link', 'lod_prettyPhoto');

function lod_prettyPhoto ($content) {

    $relAlt = $(this).closest("img").attr("alt");

    $content = preg_replace("/<a/","<a rel=\"prettyPhoto[$relAlt]\"",$content,1);
    return $content;
}
cecebar
  • 1
  • 4

1 Answers1

0

You have some jQuery mixed into your PHP: $(this).closest("img").attr("alt"). That's where your PHP error is.

To do what you're looking for, write a function like this:

add_filter( 'wp_get_attachment_link', 'lod_prettyPhoto');

function lod_prettyPhoto ($content) {
    $galleryId = preg_grep('/data-gallery="(.+)"/', $content);
    $content = preg_replace('/<a/', '<a rel="prettyPhoto[$galleryId]"', $content);
    return $content;
}

You should not use the alt attribute as a data storage mechanism, as that has a specific meaning in HTML.

DJ Madeira
  • 366
  • 2
  • 9
  • D.j. Thanks so much for your help. I've been using a temp solution and embarrassed to say I am just getting back to this issue. So with your example, will data-gallery use just one gallery (or some would say album) in WordPress that the client created and not all the photos in the entire media library...? And I agree w your assessment about alt tags - but I wanted to use one of the WP gallery fields available to the client when adding an image using the WP gallery feature (so either title, description, or alt (though not alt, you're right). – cecebar Sep 02 '14 at 19:42