Make WordPress Core

Opened 12 years ago

Last modified 5 years ago

#22075 new enhancement

Allow custom attributes to be set in `wp_get_attachment_link`

Reported by: webord's profile webord Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Media Keywords: dev-feedback has-patch needs-refresh needs-unit-tests
Focuses: Cc:

Description

I answered a question not so long ago on WordPress StackExchange.

There I saw the need on a filter for wp_get_attachment_link() to allow the developer to add or remove attributes to the HTML without having to use a regex on it or creating the a tag again.

So I've done this small patch with changes to apply this enhancement to the function.

Attachments (3)

22075.diff (2.2 KB) - added by webord 12 years ago.
post-template.php
22075.2.diff (2.2 KB) - added by webord 11 years ago.
22075.3.diff (4.8 KB) - added by ericlewis 10 years ago.

Download all attachments as: .zip

Change History (12)

@webord
12 years ago

post-template.php

#1 @SergeyBiryukov
12 years ago

  • Version changed from trunk to 3.4

@webord
11 years ago

#2 @webord
11 years ago

Got the $attr variable to the filter 'wp_get_attachment_link' in the new patch.

#3 @wonderboymusic
10 years ago

  • Milestone changed from Awaiting Review to Future Release

#4 @ericlewis
10 years ago

  • Keywords has-patch removed

Sure, sounds valid.

I'm not a fan of daisy chaining more parameters onto the function's signature. Perhaps we could consider moving over to an array hash while supporting backwards compatibility a la BuddyPress?

#5 @ericlewis
10 years ago

  • Summary changed from Improving `wp_get_attachment_link` to Allow custom attributes to be set in `wp_get_attachment_link`

@ericlewis
10 years ago

#6 @ericlewis
10 years ago

attachment:22075.3.diff changes the function to take an array hash as the only parameter, with backwards compatibility inspired by BuddyPress.

#7 follow-up: @ericlewis
10 years ago

After thinking about this for a bit, I'm not sure we want to tack on more options to this function.

The question from WPSE asks "how do I customize the output of the gallery shortcode to look nothing like what it does out of the box," and in all honesty if you want to do that, you should probably do it via the post_gallery shortcode.

To the topic at hand, if you want to add custom attributes to the anchor tag in wp_get_attachment_link(), you should probably use the wp_get_attachment_link filter and instead of regexing just rebuild the string however you see fit.

#8 in reply to: ↑ 7 @SergeyBiryukov
10 years ago

Replying to ericlewis:

To the topic at hand, if you want to add custom attributes to the anchor tag in wp_get_attachment_link(), you should probably use the wp_get_attachment_link filter and instead of regexing just rebuild the string however you see fit.

I tend to agree. It's a short function, there's not much to rebuild.

#9 @chriscct7
8 years ago

  • Keywords has-patch needs-refresh needs-unit-tests added
Note: See TracTickets for help on using tickets.