Set custom CDN url
-
Can we set a custom CDN url somehow?
I want to replace the long S3 url by something like cdn.mydomain.tld/uploads/…../image.jpgOther S3 plugins have exact this feature that uses url rewriting for this.
It’s also better for SEO.Thanks!
-
Hi @7grafix,
Thanks for reaching out to us.
Just to confirm, you’d like to serve your files via CDN, right?
You might want to send us an email at hello(at)profaceoff.com so that we can support you faster.
A quick and unsupported way to achieve this can be done with tweaks to the v1.5.0 code base of the plugin.
Disclaimer: The below code tweaks are a hint for exploration and understanding where the solution would be applied to the plugin; because the next time the plugin is updated, all the custom code tweaks would be wiped out.
Who is this for? The below solution is for developers who know what they are doing, understand all the risks and are willing to experiment on staging sites (not live sites).
Outcome – Ability to use the WordPress media library with an image served from an S3 Bucket via an SEO friendly custom subdomain; not from the WordPress /wp-content/uploads/ folder.
Use [Show in Media Library] button Notice the ‘File URL’ (aka GUID) is usinf the SSU_BUCKET_DOMAIN as the host Media Library image available to be used as a Featured Image in Posts/Pages Notice the same Featured Image media library file is available to use in a content block Proof of solution is the generated content at https://www.learnbw.com/loading/0recordmode-in-transformations/
Notice the end user experience (client side web browser) using the SEO friendly custom subdomain Benefit – The images are served from the S3 network, significantly reducing the number of calls to the WordPress server itself.
Prerequisites: For the below tweak to work, all the AWS S3 and DNS configuration needs to be ready … you need to already be publicly serving your bucket content to anyone on the internet via an SEO friendly subdomain.
- [Done] AWS S3 bucket created.
- [Done] AWS Cloudfront distribution created.
- With the free SSL certificate enabling HTPPS traffic.
- And pointing to the S3 bucket to be the origin server.
- [Done] Your domains DNS configured with a CNAME record pointing to the AWS Cloudfront distribution.
- Note that this CNAME is the DNS subdomain part of the fully qualified domain name. It is the RDN of rdn.dataprofessionals.com.au in the example below.
- Note that rdn.dataprofessionals.com.au has additional configuration to make it a Referrer Delivery Network (RDN) subdomain. While it is publicly available, it expects the caller to use the ‘referrer’ HTTP Header to identify themselves; acknowledging that this type of hot-link protection is not a replacement for true security.
Step 1 – Define a new constant in wp-config.php. Replacing domain-name with the externally publicly available sub-domain (SEO friendly) that you have already configured.
define('SSU_BUCKET_DOMAIN', 'domain-name');
This sample wp-config.php is pointing to an AWS S3 bucket which is called the same name as the publicly available sub-domain.
/** Plugin - S3 Smart Uploader. */ define('SSU_PROVIDER', 'aws'); define('SSU_KEY', 'XXX'); define('SSU_SECRET', 'XXX'); define('SSU_BUCKET', 'rdn.dataprofessionals.com.au'); define('SSU_BUCKET_DOMAIN', 'rdn.dataprofessionals.com.au'); // optional define('SSU_FOLDER', 'XXX'); // optional define('SSU_REGION', 'XXX'); define('SSU_SURL_EXPIRY', 60); // in seconds
Step 2 – The /wp-content/plugins/wp-s3-smart-upload/includes/class-s3-smart-upload-s3-service.php file has two methods to be tweaked.
/** * Create media attachment from s3 link * * @param mixed $file File object. * @param string $signed_url S3 Link. * * @return array $metadata Attachment ID */ public function create_media_attachment_from_s3_url( $file, $signed_url ) { $file_name = sanitize_text_field( $file['name'] ); // Get public URL from signed URL to fix the special character case. $public_url = $this->get_public_url_from_signed_url( $signed_url ); $wp_filetype = wp_check_filetype( $file_name ); // v1.5.1 Use custom domain for S3 bucket. $metadata_should_get = false; $s3_bucket_url = $public_url; if (defined('SSU_BUCKET_DOMAIN')) { $metadata_should_get = true; $public_url_host = parse_url($public_url, PHP_URL_HOST); $public_url = str_replace($public_url_host, SSU_BUCKET_DOMAIN, $public_url); } $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'guid' => urldecode( $public_url ), 'post_title' => preg_replace( '/\.[^.]+$/', '', $file_name ), 'post_content' => '', 'post_status' => 'inherit', ); $attachment_id = wp_insert_attachment( $attachment, $file_name ); add_post_meta( $attachment_id, 's3_public_url', $public_url ); // v1.5.1 Use custom domain for S3 bucket. // $metadata = $this->generate_attachment_metadata_for_s3_link( $attachment_id, $file, $public_url ); $metadata = $this->generate_attachment_metadata_for_s3_link( $attachment_id, $file, $s3_bucket_url, $metadata_should_get ); wp_update_attachment_metadata( $attachment_id, $metadata ); return $metadata; }
/** * Create media attachment from s3 link * * @param mixed $file_name File name. * @param string $url S3 Link. * * @return array $metadata Attachment metadata * @throws Exception * @since 1.2.0 */ public function create_media_attachment( $file_name, $url ) { $file_name = sanitize_text_field( $file_name ); $url = sanitize_url( $url ); $mime_type = ssu_get_mime_type( $file_name ); // v1.5.1 Use custom domain for S3 bucket. $s3_bucket_url = $url; if (defined('SSU_BUCKET_DOMAIN')) { $url_host = parse_url($url, PHP_URL_HOST); $url = str_replace($url_host, SSU_BUCKET_DOMAIN, $url); } $attachment = array( 'post_mime_type' => $mime_type, 'guid' => $url, 'post_title' => wp_basename( $file_name ), 'post_content' => '', 'post_status' => 'inherit', ); $attachment_id = wp_insert_attachment( $attachment, $file_name ); if ( is_wp_error( $attachment_id ) ) { throw new Exception( 'Can not insert attachment' ); } add_post_meta( $attachment_id, 's3_public_url', $url ); // v1.5.1 Use custom domain for S3 bucket; use original S3 bucket for attachment metadata (width/height) $metadata = $this->generate_attachment_metadata_for_s3_link( $attachment_id, array( 'name' => $file_name, 'size' => 0, 'width' => 0, 'height' => 0, ), // $url, v1.5.0 $s3_bucket_url, true ); wp_update_attachment_metadata( $attachment_id, $metadata ); return $metadata; }
Hope this helps … happy exploring.
-
This reply was modified 2 years ago by
john-lang-86. Reason: Add captions to the images
- The topic ‘Set custom CDN url’ is closed to new replies.