Categories
How It Works

Why CDN Media Needs a Plugin

There’s one simple reason, but understanding that reason comes with understanding how media is saved in general.

I wanted to write a blog post for work (something I try to do once a month) so I thought “I should write about using a CDN for images. I wonder if you can do this without a plugin…”

One hour later, and 1200 words, the answer is no. You cannot.

My initial goal was to move media files (and only the media) from http://example.com/wp-content/uploads/ to http://static.example.com/wordpress/ on DreamObjects. I’ve written a plugin that does that, but to my frustration, I found you cannot do this without a plugin for one incredibly simple reason.

But before the big reveal, let’s break down, in simple terms, what I would actually need to do in order to move the uploads. My two assumptions are that I already have a WordPress powered site (check) and I already have DreamObjects (check)

  1. Setup a bucket on DreamObjects (or AWS whatever) to house my images, and give it a CDN alias.
  2. Copy all the images to the bucket.
  3. Change the Upload URL to http://static.example.com/wordpress/ (my CDN alias for the bucket).
  4. Edit all posts and GUIDs to the new URL.

The thing is, I can actually do all of that!

Setup a Bucket on DreamObjects Setup a Bucket on DreamObjects

Log in to your Panel, click on Cloud, go to DreamObjects, and create a new bucket in DreamObjects. I called my domain-static because that’s easy for me to remember. Static content lives here. Once you have your bucket, click on the “Change Settings” link and add an alias of “static” to the domain you’re hosting all this on (example.com in this example).

You can name your alias anything you want. I just picked static because I knew I wanted my URLs for images to be http://static.example.com/wordpress/2016/01/happynew.jpg and this makes it simple. I like to leave my CDN available for more than just WordPress, and doing this will let me have more folders like wiki or gallery.

Next, scroll down to “DreamSpeed CDN” and turn on CDN Support. You’ll notice that tells you a special URL for CDN:

Connect to the accelerated DreamSpeed CDN version of this bucket at: examplecom-uploads.objects-us-east-1.dream.io

Don’t worry! You can use your custom URL too. You don’t have to, but most of us want to, to feel special. In order to use your custom alias that we just made, scroll up a little and check the box for ‘CDN’ on your alias. Press save and you’re good to go!

Top ↑

Copy all the Images Copy all the Images

Set the bucket public first!

Just trust me, here. Okay? Good. Set the bucket public and then upload all the images. I used Transmit. Cyberduck also works.

Top ↑

Change the Upload URL Change the Upload URL

This part was scary.

We need a moment of history first. Up until WordPress 3.5 this was actually pretty easy to do. You went to your media page and you changed things. As of 3.5, WordPress decided that this was more trouble than it was worth. Too many people were accidentally doing silly things, breaking their sites, and it was too dangerous. So they removed the part of the screen that let you change this.

I have issues with this, since for what I want to do, it makes it a hassle. But given the flaw in my great plan, I recognize the change as one that protects people who know less than I do.

Using a plugin to restore this missing setting would be the easiest on many levels. And there are a few plugins that do this but I like Upload Url and Path Enabler. It’s simple and to the point.

Of course, I love wp-cli and we have it on all our servers at DreamHost, so this command is similarly awesome:

wp option set upload_url_path http://static.example.com/wordpress

The last option for this is the secret WordPress page called “All Settings” — This is hands down the most powerful and dangerous page in all of WordPress. It lists all your options and settings that you have in the database. And yes, you can edit many of them in your browser at http://example.com/wp-admin/options.php

Go ahead. Take a look. It lists a lot of things, most of which you should never, ever, ever, touch. If you were going to use this, search for upload_url_path, change the value from empty to http://static.example.com/wordpress, and press save.

Top ↑

Edit All Your Posts Edit All Your Posts

This is a sucky part. You have to edit all your posts now to move the existing images. This is because those changes you made are only for images going forward. Which is really cool and means you didn’t break anything so far. But WordPress hardcodes the paths to images in your posts (for many reasons) so you need to change all of them.

If you know how to use the command line, this is actually really easy:

wp search-replace http://example.com/wp-content/uploads/ http://static.example.com/wordpress/

Run that command and WP-CLI will magically fix all your posts.

If you don’t want to use the command line, I recommend Velvet Blues Update URLs.

Top ↑

Done! Done!

And to be clear on things, this worked perfectly. My media library showed properly, all my images showed, and everything looked perfect. So I decided to run some basic tests and upload new images…. and that’s where my house of cards fell apart.

You cannot save files to another server.

Top ↑

Actually You Can… if you sudo Actually You Can… if you sudo

Now I have to tell you something. I lied. You totally can do this. The problem is that it’s hard and requires mounting your bucket as a local filesystem so that it would be available via the file-path of the server. You see, you could save files to /folder/location/wordpress/ anywhere on the server. I save mine to /home/user/public_html/static/ on one set up, and I have a static domain (domain-static.com) that runs from that folder.

What that means if I had a location I could access, I could tell WordPress to save there, making it ‘local’ enough to trick it. You can do this with tools like s3fs-fuse, however they’re incredibly complicated and weird for the layman. And that right there was my problem.

If you’re trying to present something as ‘Without a plugin’ then you don’t want to make the tradeoff be ‘..but with sudo access to a VPS.’ That’s unreasonable.

And it’s why, right now, if you want to use a CDN for your media in WordPress, use a plugin.

2 replies on “Why CDN Media Needs a Plugin”

Or, use a cdn mechanism that does reverse proxy of your site. So, if you reference a file the cdn lacks, it gets it, saves it, and serves it. Still probably best to use a plugin for it, but it can work without one.

Comments are closed.