Make WordPress Core

Opened 8 years ago

Last modified 9 months ago

#13429 new defect (bug)

Updating Link URL on image within Admin with Gallery

Reported by: vshoward Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 2.9.2
Component: Gallery Keywords: has-patch 2nd-opinion
Focuses: Cc:


Image insertion no longer allows url to off site resource within Gallery.

When inserting a gallery you are unable to specify the Link URL. It keep reverting back to the default.

Attachments (8)

wp-link-issue.png (109.8 KB) - added by vshoward 8 years ago.
Image of problem
13429.patch (1012 bytes) - added by SergeyBiryukov 7 years ago.
13429.2.patch (1017 bytes) - added by SergeyBiryukov 7 years ago.
save-custom-link-url.php (969 bytes) - added by SergeyBiryukov 7 years ago.
13429-3.patch (3.4 KB) - added by azaozz 6 years ago.
13429-4.patch (4.7 KB) - added by azaozz 6 years ago.
13429-5.patch (4.7 KB) - added by azaozz 6 years ago.
13429-6.patch (3.2 KB) - added by azaozz 6 years ago.

Download all attachments as: .zip

Change History (82)

#1 @ocean90
8 years ago

  • Keywords reporter-feedback added; images removed
  • Priority changed from high to normal

Did you mean these options? http://grab.by/4wYN

Maybe you can give use some more details.

Last edited 7 years ago by ocean90 (previous) (diff)

#2 @nacin
8 years ago

  • Keywords Link URL Gallery removed
  • Milestone changed from 2.9.3 to Unassigned

8 years ago

Image of problem

#3 @vshoward
8 years ago

The link you added is broken. I've attached a image of the field that is causing the problem. I am unable to update this to point to anything but the default. I'm trying to get these to point to video posted on Vimeo, which will open in a lightbox on the site. If I remember correctly, you used to be able to do this without any problem.

#4 @vshoward
7 years ago

As you can see, there are people talking about this on the forums..



Any idea when this will be working again?

#5 @miqrogroove
7 years ago

+1, duplicated by #13981

Please move to 3.1 milestone.

#6 @miqrogroove
7 years ago

  • Keywords reporter-feedback removed

#7 @nacin
7 years ago

  • Milestone changed from Awaiting Review to 3.1

#8 @nacin
7 years ago

  • Keywords needs-patch added
  • Milestone changed from 3.1 to Future Release

No patch.

#9 @dd32
7 years ago

Closed #15836 and #15270 as duplicates.

#10 @SergeyBiryukov
7 years ago

  • Keywords has-patch added; needs-patch removed

#11 @SergeyBiryukov
7 years ago

  • Keywords dev-feedback added

Also duplicates: #12315, #13981.

Shouldn't this be in 3.2?

#13 @garyc40
7 years ago

Sergey's patch looks fine, except:

update_post_meta( $attachment_id, '_wp_attachment_url', esc_attr($attachment['url']) ); 

should be:

update_post_meta( $attachment_id, '_wp_attachment_url', esc_url_raw($attachment['url']) ); 

#14 @SergeyBiryukov
7 years ago

Thanks, updated.

#15 follow-up: @macscr
7 years ago

This is a bug, why isnt it coming out with the next release? Its obviously been a bug for 8 plus months. When someone enters something into a field and clicks save changes, it should save all the changes. Is my request off base?

#16 @miqrogroove
7 years ago

macscr, it's a culture thing. The WordPress staff place a very low priority on quality assurance. Best not to get upset about it.

#17 @scribu
7 years ago

miqrogroove: Stop spreading misinformation.

To quote from IRC:

sivel: not everything can always be targested to be fixe in the current release. if we did that, then we wold never be able to make a deliverable

#18 @nacin
7 years ago

  • Keywords 3.2-early added; dev-feedback removed

This has been broken for some time and isn't a high priority fix. Now that there is a patch, this can be considered for inclusion in 3.2.

#19 in reply to: ↑ 15 @SergeyBiryukov
7 years ago

Replying to macscr:

This is a bug, why isnt it coming out with the next release?

I've attached a plugin which does basically the same what the patch does. Perhaps it can be a workaround in the meantime.

#20 @designsimply
7 years ago

  • Keywords tested added

Tested 13429.2.patch with r17484. Works for me.

#21 @dd32
7 years ago

Closed #17508 and #17377 as duplicates

#22 @oconnorstp
6 years ago

thanks for the plugin SergeyBiryukov.

the good news -- it works!

the bad news -- it only works if I insert the pictures individually into the post. I can insert them all at the same time and the saved external URLs are used.

i decided to do a blog post to document this -- i'll keep updating it as i go. here's the link;


as i say in the post. the "save" function works. thanks! but the "post a gallery with external URLs" capability still doesn't.

#23 @sillybean
6 years ago

  • Cc steph@… added

#24 @davouch
6 years ago

  • Cc davouch added

No intention to bring any confusion in here as it seems the patch is on its good way to some future release. However there is a wp-admin-only_no-plugin workaround, or at least it works for me.

  1. before inserting the the image to your post press the None button right bellow the Link URL text input area.
  2. then just press Insert into post button as you do usually
  3. save your post
  4. now get straight back to edit the picture's properties with Edit Image icon again
  5. type your desired URL and just save the settings with Update
  6. finally save your post and voila! :)


  1. If you prefer to avoid the Edit Image properties window loading and get all of it slightly quicker, you could use the common TinyURL text URL tool instead at the point d. and adopt the rest points accordingly.
  2. It works for inserted galleries too, you just have to do this with each single pic.

(edited by ocean90, italic font fixed)

Last edited 6 years ago by ocean90 (previous) (diff)

#25 @a4jp.com
6 years ago

  • Severity changed from normal to major
  • Version changed from 2.9.2 to 3.2.1

I just loaded the plugin and now I can save the external website URL in the field, but when I update and view the gallery on the regular page, the URL show up as the address for the uploaded file instead of the website I want the image to link to... If I go back to the gallery settings again the URL shows up correctly. Any ideas on fixing this? What is happening?

Is this plugin compatible with WordPress 3.2.1? It would also be better is this wasn't a plugin but the actual WordPress PHP file just got fixed.

#26 @a4jp.com
6 years ago

Here is a bit more info on the problem.

Without a plugin:
In a regular installation the "none" & "post URL" buttons of the gallery don't work. They display the correct address and look like they would work but nothing saves. So the URL link in the field always end up as the "File URL". Even if you click on a button then save the settings, and then update the gallery, the URL always goes back to the "File URL".

After loading the plugin/fix:
If we load the plugin we get the ability to save URL addresses but no matter what we do, the address of the URL on the real page (live website) shows up as the "File URL". When we go back to the gallery settings we can see the URL, we want an image to go to, has saved properly, but it doesn't link to the address on the live website.

*We have a plugin that helps us save a URL address, we just need someone to get the address to link up properly, so we can change the "File URL" to another URL. Please help us.


Glen and the a4jp team.

#27 @a4jp.com
6 years ago

I don't need this plugin to link a regular image to a URL in WordPress 3.2.1. You must have fixed that before. :) The problem is only with the gallery.

#28 @a4jp.com
6 years ago

I even edited the media.php file :( I can post it if you want. The line numbers a a bit different though.

#29 @a4jp.com
6 years ago

I could be wrong but it looks like lots of curly brackets have been left out of the if statements. Is that the problem?


Glen Rowell

Last edited 6 years ago by a4jp.com (previous) (diff)

#30 @a4jp.com
6 years ago

Any ideas or updates to the save-custom-link-url.php and/or wp-admin/includes/media.php file?

#31 @a4jp.com
6 years ago

Does anyone know where the code that makes to gallery open pictures is? I'm going to look for it now and see if I can find any mistakes in the PHP file.


#32 @ocean90
6 years ago

  • Keywords 3.2-early tested removed
  • Severity changed from major to normal
  • Version changed from 3.2.1 to 2.9.2

Version number is used to track when the bug was initially reported.

#33 @a4jp.com
6 years ago

I just want to make sure I'm doing everything right.

I added this code to the wp-admin/includes/media.php file around line 470.

 	  update_post_meta($attachment_id, '_wp_attachment_url', esc_url_raw($attachment['url']));

Then I commented out this code around line 946

	*this line was added below
	*$url = '';
	*if ( $url_type == 'file' )
		*$url = $file;
	*elseif ( $url_type == 'post' )
		*$url = $link; 

*I just used lots of stars to make it easy to find later.

Then I added this code below it

$url = get_post_meta($post->ID, '_wp_attachment_url', true); 
		   if($url_type == 'file')
 		     $url = $file; 
			 elseif($url_type == 'post')
 		     $url = $link; 
	/*this line was added above*/

Then I saved and uploaded the media.php file.

I copied the code below into a new file called save-custom-link-url.php and put it in a folder called save-custom-link-url in the plugins folder of wp-content and uploaded it to my server. The I just activated the file.

Plugin Name: Save Custom Link URL
Version: 0.1
Plugin URI: http://core.trac.wordpress.org/ticket/13429
Description: Allows to specify a custom Link URL for any attachment.
Author: Sergey Biryukov
Author URI: http://profiles.wordpress.org/sergeybiryukov/

function _save_attachment_url($post, $attachment) {
if ( isset($attachment['url']) ) 
update_post_meta( $post['ID'], '_wp_attachment_url', esc_url_raw($attachment['url']) ); 

return $post;
add_filter('attachment_fields_to_save', '_save_attachment_url', 10, 2);

function _replace_attachment_url($form_fields, $post) {
if ( isset($form_fields['url']['html']) ) {
$url = get_post_meta( $post->ID, '_wp_attachment_url', true );
if ( ! empty($url) )
$form_fields['url']['html'] = preg_replace( "/value='.*?'/", "value='$url'", $form_fields['url']['html'] );

return $form_fields;
add_filter('attachment_fields_to_edit', '_replace_attachment_url', 10, 2);

Now I can save the URL I want I just need someone to help me get the saved URL working on the live webpage... When I click on the image it just takes me to the image and not the URL.

Almost there...

If anyone can see anything stopping the saved URL from becoming the link I would love to find out what it is. I will make a tutorial telling everyone how to fix this problem as well. And I hope someone at WordPress reads this and helps. I've only been running 2 WordPress sites for just over a month. Well almost 2 months now but I still have a lot to learn.


Glen Rowell

#34 @a4jp.com
6 years ago

I noticed one thing when inspecting the input field of the "edit gallery area" where we type in the Link URL (show -> single image settings area). The "value" tag of the field changes but the "name" tag is staying the same (<input type="text" class="text urlfield" name="attachments[1957][url]" value="http://a4jp.com/gallery-3dcgi/">). Maybe that is where the problem is. How do I get the name tag to clear when I save my URL, or does something else need to change to fix the problem?


#35 @ocean90
6 years ago

Glen, please use the support forum or wait until it's fixed.

#36 @a4jp.com
6 years ago

I think I can fix this problem I just need someone to tell me where the code is that makes an image in the gallery a link...

I don't want to have to open most the PHP files to find it though.

I thought of an even easier fix for now. Where can I find the link and image in the MySQL database for a gallery pic? wp_posts? I have my gallery on a page. The address is http://a4jp.com/gallery777

Any help is greatly appreciated.


#37 @a4jp.com
6 years ago

Okay. Sorry.

#38 @daveslc
6 years ago

  • Version changed from 2.9.2 to 3.3.1

When is this going to be addressed? This is still an issue in 3.3.1.

#39 @daveslc
6 years ago

  • Cc daveslc added

#40 follow-up: @helenyhou
6 years ago

  • Version changed from 3.3.1 to 2.9.2

Version number indicates when the bug was initially introduced/reported.

#41 in reply to: ↑ 40 @daveslc
6 years ago

Last edited 6 years ago by daveslc (previous) (diff)

#42 @daveslc
6 years ago

The bug appears when you insert an image and add an external link.

The link is 'saved' to the editor, but then reverts when 'Update' is clicked.

The workaround is to remove: rel="attachment wp-att-999" from the <a href ="link to external" rel="attachment wp-att-999">.
Hence, the bug is probably being caused by rel="attachment wp-att-999 being improperly inserted in the <a href> tag when trying to link to an externalI (non-image attachment) URL.

#43 @benz001
6 years ago

  • Cc benz001 added
  • Keywords 2nd-opinion added

This isn't limited to external URLs for me on any site running 3.3.1 (and probably earlier).
Any URL inserted through the insert image dialog gets rewritten to link to the attachment page, even if you edit it in the html view.

I don't think the place to fix this bug is in media.php, unless it's to stop the rel="attachment" attribute getting added in the first place, and I can't see an obvious way to decide when and when not to add that attribute.

Another way to attack it is via the _fix_attachment_links( ) function in /wp-admin/includes/post.php.

The trigger for the error looks like it's the preg_replace_callback

		 $_fix_attachment_link_id = $id;
		$post_replace[$i] = preg_replace_callback( "#href=(\"|')[^'\"]*\\1#", '_fix_attachment_links_replace_cb', $anchor );

If I follow this correctly it's completely rewriting the href attribute by calling into get_attachment_link() in the callback function.
So any link with rel="attachment" and a parsable ID in that rel anywhere in the post content will get destroyed by this every time a post is saved.

Perhaps the solution is to check the url more closely to see if it is in fact a link to an attachment before doing the callback??

Last edited 6 years ago by benz001 (previous) (diff)

#44 @azaozz
6 years ago

Traced this back to #1870. It seems that the problem for which _fix_attachment_links() was introduced doesn't exist any more.

Did some quick testing and don't see any problems with attachment links and URLs to attached files that would require them to be fixed/rewritten after the post is saved. Seems we can safely stop replacing the URLs.

There is also another part to _fix_attachment_links(). It attaches orphaned attachments when they are used in a post (if the "wp-att-###" bit is present). This is useful for files that were uploaded directly from the Media -> Add New screen. However we are also doing something similar while inserting a file into the editor (in media.php), so not sure if repeating it is needed.

Overall removing _fix_attachment_links() seems the proper solution. This also improves performance while saving a post.

Last edited 6 years ago by azaozz (previous) (diff)

6 years ago

#45 @CarolineElisa
6 years ago

To me it is crucial that this bug is fixed.

Can anyone explain how to use the patch? Sorry, new to all this.


Last edited 6 years ago by CarolineElisa (previous) (diff)

#46 @a4jp.com
6 years ago

It has taken 7 months but I almost have a perfect fix for the native gallery bug. I just need to fix the code so it doesn't end up in a Colorbox or Lightbox like on my test site. You can see the working gallery links to pages in my site at the bottom of the gallery test page. It's the same page I was working on 7 months ago but the pages show up in Colorbox popups at the moment. http://a4jp.com/gallery777

I'm sure there are a lot of people here that could have fixed this problem in a night though :o Sorry it took me so long I had to learn a lot. I just need to fix the Colorbox problem now.

Can someone also help me make links to sort post data on the search.PHP page?

Is it best to make the code into a plugin after everything is working or just post it here???

Glen Charles Rowell

#47 @ryan
6 years ago

Related: #7040

#48 @ryan
6 years ago

azaozz and I chatted in IRC. We cannot lose _fix_attachment_links() entirely because it changes attachment links from attachment_id=xxx to the permalink version when the post is published. It could probably be reworked to run only when changing from draft to publish however. As it is now it does an extra save every time a post with attachment links is saved.

Last edited 6 years ago by ryan (previous) (diff)

#49 @ryan
6 years ago

Proposal: Call _fix_attachment_links in _transition_post_status() when transitioning from not published to published. Only rewrite attachment links of the form attachment_id=xxx. Drop the orphan checking code. Only re-save the post if a link was rewritten.

Last edited 6 years ago by ryan (previous) (diff)

#50 @ryan
6 years ago

Verified that inserting an unattached image into a post attaches the image and sets post_parent correctly without calling _fix_attachment_links().

#51 follow-up: @nacin
6 years ago

The one side effect would be ?attachment_id=xxx links getting inserted, then causing a redirect. It is kind of nice to be able to handle this transparently.

Verified that inserting an unattached image into a post attaches the image and sets post_parent correctly without calling _fix_attachment_links().

Where does this occur?

#52 in reply to: ↑ 51 ; follow-up: @azaozz
6 years ago

Replying to nacin:

Where does this occur?

When clicking the "Insert into Post" button in the gallery popup (while creating the content that is inserted).

Tested several ways to make that more transparent, _fix_attachment_links() is a "filter", perhaps better run on the content right before saving published or updated post. If run on "transition_post_status" it starts a loop (as it calls insert_post too). That would also prevent running all the hooks for saving a post for second or third time.

#53 in reply to: ↑ 52 ; follow-up: @nacin
6 years ago

Replying to azaozz:

Replying to nacin:

Where does this occur?

When clicking the "Insert into Post" button in the gallery popup (while creating the content that is inserted).

I meant in the code.

Tested several ways to make that more transparent, _fix_attachment_links() is a "filter", perhaps better run on the content right before saving published or updated post. If run on "transition_post_status" it starts a loop (as it calls insert_post too). That would also prevent running all the hooks for saving a post for second or third time.

There are ways to avoid that.

#54 in reply to: ↑ 53 @azaozz
6 years ago

Replying to nacin:

I meant in the code.

There's a hidden "post_parent" field for orphan attachments that gets saved when the attachment is inserted in a post: http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/media.php#L1270

There are ways to avoid that.

Yes, but to make that filtering transparent it would be better to filter post_content before saving in the DB rather than re-saving the post again. Patch is mostly ready, will add it after some testing.

6 years ago

#55 @azaozz
6 years ago

Changes for _fix_attachment_links() in 13429-4.patch:

  • Filters post_content using the 'wp_insert_post_data' filter.
  • Runs only on publishing or updating a post.
  • Checks the links three-way:
    • attachment_id=### in href,
    • matching wp-att-### in rel,
    • matching site URL in href.

#56 follow-up: @ryan
6 years ago

Looks good overall. I think $site_url could go after the checks that determine if attachment_id links are present. I hope "remove the protocol" is robust enough, I think we usually do preg_replace() for that. I'll do some testing.

6 years ago

#57 in reply to: ↑ 56 @azaozz
6 years ago

Replying to ryan:

In 13429-5.patch moved the $site_url after that check and made the removal of the protocol from $site_url a bit more robust (trying to avoid preg_replace() and use substr() + strpos(), but the difference for such short string is probably negligible).

#58 follow-up: @ryan
6 years ago

The links aren't updated upon publish. You must save one more time after the initial publish to get updated links. This is because the wp_insert_data action is fired prior to DB update so the get_attachment_link() call in _fix_attachment_links() doesn't have access to the new post_name value.

6 years ago

#59 in reply to: ↑ 58 @azaozz
6 years ago

Replying to ryan:

Ugh, was trying to find out why it was re-saving posts, thanks for spotting that. Updated the patch, it re-saves only published posts and only if there's a change (removed the slash matching from the regex as we pull the post from the DB, so it's not slashed).

#60 follow-up: @ryan
6 years ago

Patch works as a nice improvement on the existing code, but changing the link urls in a gallery still seems impossible.

#61 @azaozz
6 years ago

In [20308]:

Improve _fix_attachment_links(), replace attachment URLs with the real permalink only for published posts, re-save only when there are changes, see #13429

#62 @SergeyBiryukov
6 years ago

  • Milestone changed from Future Release to 3.4

Closed #12226 as a duplicate.

#63 @SergeyBiryukov
6 years ago

  • Milestone changed from 3.4 to Future Release

Didn't mean to change milestone.

#64 @wycks
6 years ago

  • Cc wycks added


#65 @eagleapex
6 years ago

This is a major feature for me and other users. Putting links on gallery items is a big deal. When will this be resolved?
Currently on 3.3.2

Last edited 6 years ago by eagleapex (previous) (diff)

#66 in reply to: ↑ description @bunnygirl
5 years ago

  • Cc bunnygirl added

Thank you, SergeyBiryukov, for the plugin that allows custom URLs to be saved for images.

Currently on version 3.4.1 and still no fix to the gallery problem, however. The problem, I take it, is that there is no option under "Gallery Settings" to link to anything other than file or attachment page?

I worked around this by creating the same div container that the gallery creates, and inserting into it the files individually in thumbnail size. Then I added the class to the thumbnails that the gallery would have. This worked perfectly to create a "gallery" that looks exactly like the actual galleries. However, it was a lot more hassle than inserting a gallery simply would have been, and I would not want to have to do it for a large gallery (mine was only 6 images).

#67 @bananastalktome
5 years ago

  • Cc bananastalktome@… added

#68 in reply to: ↑ 60 @wordpresssites
5 years ago

Replying to ryan:

Patch works as a nice improvement on the existing code, but changing the link urls in a gallery still seems impossible.

There's a solution for this which works with NGG custom fields so why can't this be used for the native gallery?

Still not working on 3.5

#69 @SergeyBiryukov
4 years ago

#28359 was marked as a duplicate.

#70 @magdigit
4 years ago

Is my conclusion right: So after 4 years and in the meantime reaching version 3.9.1. there STILL can't be links attached to the individual images inside the so-called splendid Gallery?
How sad!!!

#71 @ocean90
3 years ago

#31209 was marked as a duplicate.

#72 @SergeyBiryukov
23 months ago

#35406 was marked as a duplicate.

#73 @melchoyce
19 months ago

What's blocking this from happening?

#74 @druesome
9 months ago

A bit of discussion in the WordPress.com forums is also happening:


107 replies so far since 2013.

Any chance of this seeing the light of day this year?

Note: See TracTickets for help on using tickets.