0

Now i have this snippet in my functions.php

/**
 * Add rel to links.
 */
function add_rel_to_gallery($link) {
    return str_replace('<a href=', '<a data-fancybox="true" rel="group" href=', $link);
}
add_filter( 'wp_get_attachment_link', 'add_rel_to_gallery' );

Every link in gallery has rel="group", but actually i want get something like that:

rel="group-$gallery-id"

How do this?

What is gallery id?

<div id="gallery-1" class="gallery galleryid-1457 gallery-columns-5 gallery-size-thumbnail">
    <dl class="gallery-item">
      <dt class="gallery-icon landscape">
        <a data-fancybox="true" rel="group" href="smt.jpg"><img src="smt.jpg" class="attachment-thumbnail" alt="ult-collection_sh10154" height="150" width="150"></a>
      </dt></dl><dl class="gallery-item">
      <dt class="gallery-icon landscape">
        <a data-fancybox="true" rel="group" href="smt.jpg"><img src="smt.jpg" class="attachment-thumbnail" alt="ult-collection_sh10153_1" height="150" width="150"></a>
      </dt>
    </dl>
</div>

In this output gallery id is 1457.

I need gallery id (or something that can grouped images from one gallery to one rel group) for create fancybox groups:

Note: ID's are single use and are only applied to one element. Galleries are created from elements who have the same "data-fancybox-group" or "rel" attribute value.

via http://fancyapps.com/fancybox/#examples

To @s_ha_dum:

function add_rel_to_gallery($link, $id) {
  return str_replace('<a href=', '<a data-fancybox="true" rel="group-'.$id.'" href=', $link);
}
add_filter( 'wp_get_attachment_link', 'add_rel_to_gallery', 10, 2 );

Output:

<div id="gallery-1" class="gallery galleryid-1457 gallery-columns-5 gallery-size-thumbnail"><dl class="gallery-item">
      <dt class="gallery-icon landscape">
        <a data-fancybox="true" rel="group-1461" href="http://streetball.name/wp-content/uploads/2013/04/ult-collection_sh10153_1.jpeg"><img src="http://streetball.name/wp-content/uploads/2013/04/ult-collection_sh10153_1-150x150.jpeg" class="attachment-thumbnail" alt="ult-collection_sh10153_1" height="150" width="150"></a>
      </dt></dl><dl class="gallery-item">
      <dt class="gallery-icon landscape">
        <a data-fancybox="true" rel="group-1467" href="http://streetball.name/wp-content/uploads/2013/04/ult-collection_sh10348_1.jpeg"><img src="http://streetball.name/wp-content/uploads/2013/04/ult-collection_sh10348_1-150x150.jpeg" class="attachment-thumbnail" alt="ult-collection_sh10348_1" height="150" width="150"></a>
      </dt></dl>
</div>
  • What exactly is $gallery? – kaiser Feb 16 '14 at 13:26
  • @kaiser i add explanation to the question. – user123 Feb 16 '14 at 14:02
0

That filter passes more arguments than you are using.

return apply_filters( 'wp_get_attachment_link', "<a href='$url'>$link_text</a>", $id, $size, $permalink, $icon, $text );

The second of those is the one you need. So you need to alter you callback to use the second parameter, and alter your add_filter to ask for it by setting the fourth argument to 2.

function add_rel_to_gallery($link, $id) {
  return str_replace('<a href=', '<a data-fancybox="true" rel="group-'.$id.'" href=', $link);
}
add_filter( 'wp_get_attachment_link', 'add_rel_to_gallery', 10, 2 );

After seeing your edit, I realized that I misread what you were attempting and I tried to find a way to create a static gallery specific ID without abusing hooks too much. I am not sure I succeeded in that last part but the following seems to work:

function get_id($inc = false) {
  static $id;
  if ($inc) {
    $id++;
  }
  return $id;
}

function replace($link) {
  $id = get_id();
  return str_replace('<a href=', '<a data-fancybox="true" rel="group-'.$id.'" href=', $link);
}

add_filter(
  'post_gallery',
  function() {
    get_id(true);
    add_filter('wp_get_attachment_link','replace');
  }
);

The correct answer is probably to use the post_gallery hook to hijack the rest of the gallery shortcode, or just overwrite the gallery shortcode with one of your own.

  • Actually, for every link i get unique ID. – user123 Feb 16 '14 at 16:57
  • Please, see post update. – user123 Feb 16 '14 at 17:28

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.