Skip to content

Instantly share code, notes, and snippets.

@jdembowski
Created April 12, 2018 17:09
Show Gist options
  • Save jdembowski/aa3cce8ad0d6007b3f4f49a87477f72b to your computer and use it in GitHub Desktop.
Save jdembowski/aa3cce8ad0d6007b3f4f49a87477f72b to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Make new sites in the network with https URLs
Description: Force new sites in a multisite network to use HTTPS as the scheme.
Plugin Author: Jan Dembowski
This probably should not be necessary and the scheme should be picked up
by WordPress. But I could not get my new sites to use https so here I am.
This plugin
1. hooks wpmu_new_blogs
https://codex.wordpress.org/Plugin_API/Action_Reference/wpmu_new_blog
2. switches to the new blog id
3. obtains the home and siteurl options
4. replaces the ^http:/ in those strings with https:/
5. and updates the options for that new site
It's a horrible hack. It does not do any checking and if the new site is not setup
with a valid x.509 cert for TLS then the site will not load or it will
toss scary browser warnings to the user.
This should be copied into mu-plugins to use.
*/
add_action( 'wpmu_new_blog', 'mh_new_site_http', 10, 6 );
function mh_new_site_http( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
switch_to_blog( $blog_id );
$mh_old_home_url = trailingslashit( esc_url( get_option( 'home' ) ) );
$mh_old_site_url = trailingslashit( esc_url( get_option( 'siteurl' ) ) );
$mh_new_home_url = preg_replace( '/^http:/' , 'https:' , $mh_old_home_url );
$mh_new_site_url = preg_replace( '/^http:/' , 'https:' , $mh_old_site_url );
update_option( 'home', $mh_new_home_url );
update_option( 'siteurl', $mh_new_site_url );
restore_current_blog();
}
// add_action( 'init' , 'mh_my_hooks' );
function mh_my_hooks() {
remove_filters ( 'deprecated_function_trigger_error' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment