Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created May 11, 2021 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpmudev-sls/791d7890c163787214ab5533f22f23e1 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/791d7890c163787214ab5533f22f23e1 to your computer and use it in GitHub Desktop.
[Hummingbird] Replaces AO Base URL
<?php
/**
* Plugin Name: [Hummingbird] Replaces AO Base URL
* Plugin URI: https://wpmudev.com/
* Description: Useful when the site is setup on one address, but runs for users on a different URL.
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://wpmudev.com/
* Task: SLS-1799
* License: GPLv2 or later
*
* @package WPMUDEV_Hummingbird_Replace_AO_Base_URL
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WPMUDEV_Hummingbird_Replace_AO_Base_URL' ) && ! is_admin() ) {
/**
* Main class of the plugin.
*/
class WPMUDEV_Hummingbird_Replace_AO_Base_URL {
/**
* Insert below the old and the new address (base URL) without the protocol (HTTP or HTTPS)
*/
private $old_base_url = '//your-OLD-address-without-protocol-goes-here.com/';
private $new_base_url = '//your-NEW-address-without-protocol-goes-here.com/';
private static $instance = null;
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new WPMUDEV_Hummingbird_Replace_AO_Base_URL();
}
return self::$instance;
}
private function __construct() {
$this->init();
}
public function init() {
add_filter( 'style_loader_tag', array( $this, 'style_handler' ), PHP_INT_MAX );
add_filter( 'script_loader_tag', array( $this, 'script_handler' ), PHP_INT_MAX );
}
public function style_handler( $html ) {
$html = str_replace( $this->old_base_url, $this->new_base_url, $html );
return $html;
}
public function script_handler( $tag ) {
$tag = str_replace( $this->old_base_url, $this->new_base_url, $tag );
return $tag;
}
}
add_action(
'plugins_loaded',
function() {
return WPMUDEV_Hummingbird_Replace_AO_Base_URL::get_instance();
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment