Opened 11 years ago
Closed 11 years ago
#32145 closed defect (bug) (invalid)
wp_get_attachment_url() append /sites/$blog_id/ twice in url
| Reported by: |
|
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)
Change History (12)
#2
@
11 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
@
11 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
#6
@
11 years ago
- Keywords reporter-feedback added
@pareshradadiya - can you provide your exact steps and code here... I cannot reproduce
#7
@
11 years ago
- Keywords reporter-feedback removed
This snippets is for creating a clone of an attachments. Since the issue is only appears in the MU sub sites, wp_get_attachment_url() is seems to working as expected in the MU main sites.
python
/**
* 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' );
#8
@
11 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.
11 years ago
#10
@
11 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
@
11 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.