admin管理员组文章数量:1130349
I'm programmatically adding image attachments to posts, however I'm not uploading the image, just storing the full url which is hosted on a CDN e.g.
.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768
However when the template is rendered it prepends the base url e.g.
http://localhost:8888/wordpress/.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768
I'm trying to hook into the media attachments to sort this out but I can't get anything to work.
Are there any filter hooks available so I can not prepend the base url if the path starts with r'http[s]?://' ?
Edit
This is the code I'm using to add the images:
$file = '.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768';
$attachment = array(
'post_title' => $file,
'post_mime_type' => "image/jpg",
);
$attach_id = wp_insert_attachment($attachment, $file);
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
I'm programmatically adding image attachments to posts, however I'm not uploading the image, just storing the full url which is hosted on a CDN e.g.
https://i.atcdn.co.uk/imgser-uk/imgser-uk/servlet/media.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768
However when the template is rendered it prepends the base url e.g.
http://localhost:8888/wordpress/https://i.atcdn.co.uk/imgser-uk/imgser-uk/servlet/media.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768
I'm trying to hook into the media attachments to sort this out but I can't get anything to work.
Are there any filter hooks available so I can not prepend the base url if the path starts with r'http[s]?://' ?
Edit
This is the code I'm using to add the images:
$file = 'https://i.atcdn.co.uk/imgser-uk/imgser-uk/servlet/media.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768';
$attachment = array(
'post_title' => $file,
'post_mime_type' => "image/jpg",
);
$attach_id = wp_insert_attachment($attachment, $file);
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
Share
Improve this question
edited Nov 2, 2018 at 11:26
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Nov 1, 2018 at 22:59
dpdentondpdenton
1011 bronze badge
2
- How are you adding these URLs to begin with? – Jacob Peattie Commented Nov 2, 2018 at 0:00
- I've just edited the post – dpdenton Commented Nov 2, 2018 at 8:59
1 Answer
Reset to default 0You cannot insert remote file in this manner.
<?php wp_insert_attachment( $attachment, $filename, $parent_post_id ); ?>
$filename (string) (optional) Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be in the uploads
https://codex.wordpress/Function_Reference/wp_insert_attachment
You should first download it to temp dir.
If you wish to keep files on your CDN and simply save URI, consider adding a custom field and saving it there.
I'm programmatically adding image attachments to posts, however I'm not uploading the image, just storing the full url which is hosted on a CDN e.g.
.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768
However when the template is rendered it prepends the base url e.g.
http://localhost:8888/wordpress/.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768
I'm trying to hook into the media attachments to sort this out but I can't get anything to work.
Are there any filter hooks available so I can not prepend the base url if the path starts with r'http[s]?://' ?
Edit
This is the code I'm using to add the images:
$file = '.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768';
$attachment = array(
'post_title' => $file,
'post_mime_type' => "image/jpg",
);
$attach_id = wp_insert_attachment($attachment, $file);
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
I'm programmatically adding image attachments to posts, however I'm not uploading the image, just storing the full url which is hosted on a CDN e.g.
https://i.atcdn.co.uk/imgser-uk/imgser-uk/servlet/media.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768
However when the template is rendered it prepends the base url e.g.
http://localhost:8888/wordpress/https://i.atcdn.co.uk/imgser-uk/imgser-uk/servlet/media.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768
I'm trying to hook into the media attachments to sort this out but I can't get anything to work.
Are there any filter hooks available so I can not prepend the base url if the path starts with r'http[s]?://' ?
Edit
This is the code I'm using to add the images:
$file = 'https://i.atcdn.co.uk/imgser-uk/imgser-uk/servlet/media.jpg?id=fa348829bc924e28a649624e52f7191e&width=1024&height=768';
$attachment = array(
'post_title' => $file,
'post_mime_type' => "image/jpg",
);
$attach_id = wp_insert_attachment($attachment, $file);
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
Share
Improve this question
edited Nov 2, 2018 at 11:26
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Nov 1, 2018 at 22:59
dpdentondpdenton
1011 bronze badge
2
- How are you adding these URLs to begin with? – Jacob Peattie Commented Nov 2, 2018 at 0:00
- I've just edited the post – dpdenton Commented Nov 2, 2018 at 8:59
1 Answer
Reset to default 0You cannot insert remote file in this manner.
<?php wp_insert_attachment( $attachment, $filename, $parent_post_id ); ?>
$filename (string) (optional) Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be in the uploads
https://codex.wordpress/Function_Reference/wp_insert_attachment
You should first download it to temp dir.
If you wish to keep files on your CDN and simply save URI, consider adding a custom field and saving it there.
本文标签: themesDon39t prepend WordPress base url to image paths
版权声明:本文标题:themes - Don't prepend WordPress base url to image paths 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749211638a2333461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论