$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>themes - Don't prepend WordPress base url to image paths|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far