Pbinder
Pbinder

Reputation: 470

Wordpress: Serve images from a CDN

I use a CDN to serve my images, which I upload into Wordpress. My settings were:

enter image description here

It worked perfectly but it doesn't work anymore since I updated to Wordpress 4.5. This are my settings now:

enter image description here

This filter doesn't work either:

function my_cdn_upload_url() {
   return 'http://media.mydomain.com';
}
add_filter( 'pre_option_upload_url_path', 'my_cdn_upload_url' );

Does anyone know how should I serve my images from a CDN?

Thank you.

Upvotes: 1

Views: 6878

Answers (5)

Lana Codes
Lana Codes

Reputation: 404

There is a hidden admin page where you can find all the options:

http://localhost/wp-admin/options.php

Find the upload_url_path option and set a value.

enter image description here


Note:

After you give it value, then the "Store uploads in this folder" which is the upload_path, and "Full URL path to files" which is the upload_url_path options will also appear on the Media Settings page:

http://localhost/wp-admin/options-media.php

enter image description here

The options only appear on the Media Settings page if a value is already set. This is the code in WordPress Core that defines how it works:

/*
 * If upload_url_path is not the default (empty),
 * or upload_path is not the default ('wp-content/uploads' or empty),
 * they can be edited, otherwise they're locked.
 */
if ( get_option( 'upload_url_path' ) || get_option( 'upload_path' ) && 'wp-content/uploads' !== get_option( 'upload_path' ) ) :

Upvotes: 0

Dave Hilditch
Dave Hilditch

Reputation: 5439

The problem with the approaches above are you won't have file write permissions to upload images to your CDN.

Another approach is to upload all of your images to somewhere - e.g. Amazon S3, or keep them on affiliate network CDNs - and then store your image URLs in postmeta for your posts or products.

You then need to alter your theme files to pull the images from the postmeta fields instead of from the featured image or thumbnail fields.

FYI - this approach will massively speed up imports since WordPress will create multiple sizes of images using up CPU and disk space.

I created a plugin to solve this:

https://www.wpintense.com/product/external-images/

Upvotes: 0

Pbinder
Pbinder

Reputation: 470

The easiest way is editing the content of "upload_url_path" in the wp_options table:

upload_url_path > http://media.mydomain.com

Thank you!

Upvotes: 3

bugnumber9
bugnumber9

Reputation: 471

Looks like you upgraded from quite an old WP version, didn't you? Uploads folder and path options were removed in Settings -> Media a long time ago.

So what does your real path for uploads look like? It's not a standard domain.com/wp-content/uploads// correct? If yes - where do new images get uploaded to after the upgrade?

Try playing around with https://wordpress.org/plugins/custom-upload-dir/ and see if it helps get your images back to CDN.

Upvotes: 2

Nirpendra Patel
Nirpendra Patel

Reputation: 679

BEST WAY is to use w3 total cache.

It has a built in cdn support and of course you can use it's main functionality , caching , most useful to speedup website.

Upvotes: -1

Related Questions