WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 7 weeks 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:

Description

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 4 years ago.
Image of problem
13429.patch (1012 bytes) - added by SergeyBiryukov 4 years ago.
13429.2.patch (1017 bytes) - added by SergeyBiryukov 4 years ago.
save-custom-link-url.php (969 bytes) - added by SergeyBiryukov 3 years ago.
13429-3.patch (3.4 KB) - added by azaozz 2 years ago.
13429-4.patch (4.7 KB) - added by azaozz 2 years ago.
13429-5.patch (4.7 KB) - added by azaozz 2 years ago.
13429-6.patch (3.2 KB) - added by azaozz 2 years ago.

Download all attachments as: .zip

Change History (78)

comment:1 ocean904 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 4 years ago by ocean90 (previous) (diff)

comment:2 nacin4 years ago

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

vshoward4 years ago

Image of problem

comment:3 vshoward4 years ago

Ocean90,
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.

comment:4 vshoward4 years ago

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

http://wordpress.org/support/topic/343988?replies=26

http://wordpress.org/support/topic/343943

Any idea when this will be working again?

comment:5 miqrogroove4 years ago

+1, duplicated by #13981

Please move to 3.1 milestone.

comment:6 miqrogroove4 years ago

  • Keywords reporter-feedback removed

comment:7 nacin4 years ago

  • Milestone changed from Awaiting Review to 3.1

comment:8 nacin4 years ago

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

No patch.

comment:9 dd324 years ago

Closed #15836 and #15270 as duplicates.

comment:10 SergeyBiryukov4 years ago

  • Keywords has-patch added; needs-patch removed

SergeyBiryukov4 years ago

comment:11 SergeyBiryukov4 years ago

  • Keywords dev-feedback added

Also duplicates: #12315, #13981.

Shouldn't this be in 3.2?

comment:13 garyc404 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']) ); 

comment:14 SergeyBiryukov4 years ago

Thanks, updated.

SergeyBiryukov4 years ago

comment:15 follow-up: macscr3 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?

comment:16 miqrogroove3 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.

comment:17 scribu3 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

comment:18 nacin3 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.

comment:19 in reply to: ↑ 15 SergeyBiryukov3 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.

comment:20 designsimply3 years ago

  • Keywords tested added

Tested 13429.2.patch with r17484. Works for me.

comment:21 dd323 years ago

Closed #17508 and #17377 as duplicates

comment:22 oconnorstp3 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;

http://www.haven2.com/index.php/archives/wordpress-gallery-cant-save-or-link-to-external-urls

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

comment:23 sillybean3 years ago

  • Cc steph@… added

comment:24 davouch3 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! :)

Notes:

  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 3 years ago by ocean90 (previous) (diff)

comment:25 a4jp.com3 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.

comment:26 a4jp.com3 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.

Regards,

Glen and the a4jp team.

comment:27 a4jp.com3 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.

comment:28 a4jp.com3 years ago

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

comment:29 a4jp.com3 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?

Regards,

Glen Rowell

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

comment:30 a4jp.com3 years ago

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

comment:31 a4jp.com3 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.

Glen

comment:32 ocean903 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.

comment:33 a4jp.com3 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.

if(isset($attachment['url']))
	{
 	  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(empty($url))
		{
		   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.

<?php
/*
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.

Regards,

Glen Rowell

comment:34 a4jp.com3 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?

Glen

comment:35 ocean903 years ago

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

comment:36 a4jp.com3 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.

Glen

comment:37 a4jp.com3 years ago

Okay. Sorry.

comment:38 daveslc2 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.

comment:39 daveslc2 years ago

  • Cc daveslc added

comment:40 follow-up: helenyhou2 years ago

  • Version changed from 3.3.1 to 2.9.2

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

comment:41 in reply to: ↑ 40 daveslc2 years ago

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

comment:42 daveslc2 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.

comment:43 benz0012 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 2 years ago by benz001 (previous) (diff)

comment:44 azaozz2 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 2 years ago by azaozz (previous) (diff)

azaozz2 years ago

comment:45 CarolineElisa2 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.

Thanks!

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

comment:46 a4jp.com2 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

comment:47 ryan2 years ago

Related: #7040

comment:48 ryan2 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 2 years ago by ryan (previous) (diff)

comment:49 ryan2 years ago

Proposal: Call _fix_attachment_links in _transition_post_status() when transitioning to 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.

Version 0, edited 2 years ago by ryan (next)

comment:50 ryan2 years ago

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

comment:51 follow-up: nacin2 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?

comment:52 in reply to: ↑ 51 ; follow-up: azaozz2 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.

comment:53 in reply to: ↑ 52 ; follow-up: nacin2 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.

comment:54 in reply to: ↑ 53 azaozz2 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.

azaozz2 years ago

comment:55 azaozz2 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.

comment:56 follow-up: ryan2 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.

azaozz2 years ago

comment:57 in reply to: ↑ 56 azaozz2 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).

comment:58 follow-up: ryan2 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.

azaozz2 years ago

comment:59 in reply to: ↑ 58 azaozz2 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).

comment:60 follow-up: ryan2 years ago

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

comment:61 azaozz2 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

comment:62 SergeyBiryukov2 years ago

  • Milestone changed from Future Release to 3.4

Closed #12226 as a duplicate.

comment:63 SergeyBiryukov2 years ago

  • Milestone changed from 3.4 to Future Release

Didn't mean to change milestone.

comment:64 wycks2 years ago

  • Cc wycks added

ridiculous.

comment:65 eagleapex2 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 2 years ago by eagleapex (previous) (diff)

comment:66 in reply to: ↑ description bunnygirl23 months 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).

comment:67 bananastalktome23 months ago

  • Cc bananastalktome@… added

comment:68 in reply to: ↑ 60 wordpresssites18 months 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

comment:69 SergeyBiryukov7 weeks ago

#28359 was marked as a duplicate.

comment:70 magdigit7 weeks 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!!!

Note: See TracTickets for help on using tickets.