Created
June 13, 2019 13:23
-
-
Save benoitmercusot/35a71b8222c21ba10cf1a33bca21d831 to your computer and use it in GitHub Desktop.
Use WordPress with dynamic domain name. Inspired by livemode from Local by Flywheel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WP Dynamic Distant Urls. (Adapted from Local by Flywheel Relative URL (for Live Links)) | |
License: GPLv2 or later | |
*/ | |
$original_domain = parse_url( get_option( 'siteurl' ), PHP_URL_HOST ); | |
$distant_domain = $_SERVER['SERVER_NAME']; | |
if( $original_domain !== $distant_domain ){ | |
add_filter('template_include', 'bm_ob_start_template_include', 0); | |
remove_action('template_redirect', 'redirect_canonical'); | |
$filters = array( | |
'wp_redirect', | |
'bloginfo_url', | |
'the_permalink', | |
'wp_list_pages', | |
'wp_list_categories', | |
'the_content_more_link', | |
'the_tags', | |
'the_author_posts_link', | |
'post_link', | |
'post_type_link', | |
'page_link', | |
'attachment_link', | |
'get_shortlink', | |
'post_type_archive_link', | |
'get_pagenum_link', | |
'get_comments_pagenum_link', | |
'term_link', | |
'search_link', | |
'day_link', | |
'month_link', | |
'year_link', | |
'option_siteurl', | |
'blog_option_siteurl', | |
'option_home', | |
'admin_url', | |
'get_admin_url', | |
'get_site_url', | |
'network_admin_url', | |
'home_url', | |
'includes_url', | |
'site_url', | |
'site_option_siteurl', | |
'network_home_url', | |
'network_site_url', | |
'get_the_author_url', | |
'get_comment_link', | |
'wp_get_attachment_image_src', | |
'wp_get_attachment_thumb_url', | |
'wp_get_attachment_url', | |
'wp_login_url', | |
'wp_logout_url', | |
'wp_lostpassword_url', | |
'get_stylesheet_uri', | |
'get_locale_stylesheet_uri', | |
'script_loader_src', | |
'style_loader_src', | |
'get_theme_root_uri', | |
'plugins_url', | |
'stylesheet_directory_uri', | |
'template_directory_uri' | |
); | |
foreach ( $filters as $filter ) { | |
add_filter($filter, 'bm_dynamic_url_rewrite'); | |
} | |
} | |
function bm_ob_start_template_include($template) { | |
ob_start('bm_dynamic_url_rewrite'); | |
return $template; | |
} | |
function bm_dynamic_url_rewrite($str) { | |
global $original_domain, $distant_domain; | |
$str = str_replace('www.' . $original_domain, $distant_domain, $str); | |
$str = str_replace($original_domain, $distant_domain, $str); | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment