[wp-trac] [WordPress Trac] #13429: Updating Link URL on image within Admin with Gallery

WordPress Trac wp-trac at lists.automattic.com
Sat Aug 27 13:52:12 UTC 2011


#13429: Updating Link URL on image within Admin with Gallery
--------------------------+-----------------------------
 Reporter:  vshoward      |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Future Release
Component:  Gallery       |     Version:  2.9.2
 Severity:  normal        |  Resolution:
 Keywords:  has-patch     |
--------------------------+-----------------------------

Comment (by a4jp.com):

 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

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/13429#comment:33>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list