Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#32145 closed defect (bug) (invalid)

wp_get_attachment_url() append /sites/$blog_id/ twice in url

Reported by: pareshradadiya's profile pareshradadiya Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Media Keywords: close
Focuses: multisite Cc:

Description

WordPress doesn't allow you to attach an already-attached image to another post. At the minute, I must re-upload the same image if I want to do this. So when I use wp_get_attachment_url() to get attachments url, It append /sites/$blog_id/ twice for all re-uploaded attachments.

Attachments (1)

wp-get-attachment-url.diff (3.3 KB) - added by pareshradadiya 9 years ago.

Download all attachments as: .zip

Change History (12)

#2 @pareshradadiya
9 years ago

  • Summary changed from wp_get_attachment_url add /sites/$blog_id/ twice when attach same image to multiple posts in MU sites to wp_get_attachment_url add /sites/$blog_id/ twice when attach same file to multiple posts in MU sites

#3 @pareshradadiya
9 years ago

  • Summary changed from wp_get_attachment_url add /sites/$blog_id/ twice when attach same file to multiple posts in MU sites to wp_get_attachment_url() append /sites/$blog_id/ twice in url

#4 @pareshradadiya
9 years ago

  • Version set to trunk

#5 @wonderboymusic
9 years ago

  • Milestone changed from Awaiting Review to 4.3

#6 @wonderboymusic
9 years ago

  • Keywords reporter-feedback added

@pareshradadiya - can you provide your exact steps and code here... I cannot reproduce

#7 @pareshradadiya
9 years ago

  • Keywords reporter-feedback removed

The issue is only appears in the MU sites. This snippets is for creating a clone of an attachments

/**
 * Create a copy of the attachment.
 */
function test_clone_attachment() {

        /** Get an old attachment post by id **/
        $attachment1 = get_post( '4' );

        $args = array(
                'post_mime_type' => $attachment1->post_mime_type,
                'guid'           => $attachment1->guid,
                'post_title'     => $attachment1->post_title,
                'post_content'   => $attachment1->post_content,
                'post_parent'    => 0,
        );

        $attachment2 = wp_insert_attachment( $args, wp_get_attachment_url( $attachment1->ID ), '0' );

        echo( wp_get_attachment_url( $attachment2 ) );

}

add_action( 'init', 'test_clone_attachment' );
 
Version 0, edited 9 years ago by pareshradadiya (next)

#8 @jeremyfelt
9 years ago

  • Focuses multisite added
  • Keywords close added
  • Milestone changed from 4.3 to Awaiting Review

wp_insert_attachment() expects a file location on the server rather than a URL. Using get_attached_file() instead to grab the file location as it's stored in post meta will create the proper attachment URL.

$attachment2 = wp_insert_attachment( $args, get_attached_file( $attachment1->ID ), '0' );

I can't speak for how solid things are after that when juggling a file attached to multiple posts, but it appears the code above will work with this change.

This ticket was mentioned in Slack in #core by jorbin. View the logs.


9 years ago

#10 @jorbin
9 years ago

  • Milestone changed from Awaiting Review to Future Release

While the decision to close is being made (this is not a comment on that), moving to future release so that the awaiting review report can be easier to follow.

#11 @jeremyfelt
9 years ago

  • Keywords has-patch removed
  • Milestone Future Release deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version trunk deleted

I think we can close this out. @pareshradadiya, please feel free to comment or reopen if needed. Reworking your code example should do the trick.

Note: See TracTickets for help on using tickets.