themes - Don't prepend WordPress base url to image paths

admin2025-06-06  0

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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749211669a317297.html

最新回复(0)