WordPress Static file acceleration, integrated CDN

Source: Internet
Author: User

A recent company's online store project, with the bottom of the wordpress+woocommerce, a lot of custom additional features are developed by me, the final deployment of the plan to integrate the CDN, the static content is diverted to the Edge server, wordpress only processing data logic, That is, just run the PHP program.

This involves how to replace the default static resource URL into a CDN URL, turned over the internet, there are already Daniel wrote a reliable plugin, this Daniel's blog address

https://dannyvankooten.com/using-a-cdn-with-wordpress/

People have already made their own projects on GitHub.

Https://github.com/dannyvankooten/wp-cdn-loader

Download plugins directly to Wp-content/plugins, unzip, activate on admin page to use

Https://github.com/dannyvankooten/wp-cdn-loader/archive/master.zip

First of all to add this line in the wp-config.php, can be used as a switch to open the CDN, another switch is in the background

Turn off the function of this plugin ~

Define (' Dvk_cdn_url ', ' http://xxxxxx.cloudfront.net ');//the plugin won ' t replace assets when Script_debug was enabled.

Once configured, refresh the page to see that all css,js,jpg,png URLs are replaced with the set values (

Http://xxxxxx.cloudfront.net

), stating that the configuration was successful!

Curiosity, studied the code written by others, a total of 2 files, the main file

<?php/*Plugin Name: CDN LoaderDescription: This plugin will load  Your assets from a given cdn instead of the local server. author: danny van kootenversion: 1.0author uri: https://dannyvankooten.com/*/ Namespace cdn_loader;if ( ! defined (  ' abspath '  )  )  {     Exit;} Add_action (  ' Template_redirect ',  function ()  {    // don ' T run  if script_debug is set to true    if ( defined (  ' Script_debug '  )  && SCRIPT_DEBUG )  {         return;    }    // load class     require_once __dir__ .  '/src/urlrewriter.php ';     // get url  of cdn & site     $cdn _url =  ( defined (  ' Dvk_cdn_url '  )  ?  DVK_CDN_URL :  " );     $site _url = get_site_url ();     // instantiate class     $url _rewriter = new  Urlrewriter (  $CDN _url,  $site _url );     $url _rewriter->add_hooks ();});

Class library file

<?phpnamespace CDN_Loader;class UrlRewriter {    /**      *  @var  string     */    private  $cdn _ url =  ';    /**     *  @var  string      */    private  $site _url =  ';     /**     * Constructor     *      *  @param  string  $cdn _url     *  @param  string   $site _url     */    public function __construct (   $CDN _url,  $site _url )  {        // store  cdn url & site url in property          $this-&GT;SITE_URL&NBSP;=&NBSP; $site _url;         $this->cdn_url =  $cdn _url;     }    public function add_hooks ()  {         // add nothing if cdn url is empty         if (  " ===  $this->cdn_url )  {             return false;         }        // add rewrite filters  for plugin & theme assets        add_ Filter (  ' Theme_root_uri ',  array (  $this,  ' rewrite '  ), 99, 1 );         add_filter (  ' Plugins_url ',  array (  $this,  ' Rewrite '  ), 99, 1 );         // add rewrite filters for misc  Scripts and styles        add_filter (  ' Script_loader_ Src ',  array (  $this,  ' rewrite '  ), 99, 1 );         add_filter (  ' style_loader_src ',  array (  $this,  ' rewrite '  ),  99,  1 );         return true;    }     /**     *  @param   $url       *     *  @return  mixed     */     public function rewrite (  $url  )  {          $url  = str_replace (  $this->site_url,  $this->cdn_url,  $url  );         return  $url;     }} 

Write here today, the company is going to work, have time to add the explanation.

This article is from the "Home Circle Exchange Study" blog, please be sure to keep this source http://brucetam.blog.51cto.com/1863614/1725648

WordPress Static file acceleration, integrated CDN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.