-
-
Save saas786/c8fa08c4449e1392d6a0b758fbd26f2b to your computer and use it in GitHub Desktop.
Port switch to get an workaround to use WordPress Multisite also without the default port 80.
This file contains 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 # -*- coding: utf-8 -*- | |
declare( strict_types = 1 ); | |
/** | |
* Plugin Name: Port Switch | |
* Plugin URI: https://gist.github.com/bueltge/51013ab809f5f0f5e305c70c2d393fff | |
* Description: Port switch to get an workaround to use WordPress Multisite also without the default port 80. | |
* Version: dev | |
* Author: Frank Bültge | |
* Author URI: https://bueltge.de | |
* License: MIT | |
* License URI: LICENSE | |
* | |
* Kudos to https://benohead.com/wordpress-running-multisite-different-port/ | |
* Kudos to https://wordpress.stackexchange.com/questions/212978/multisite-network-port-num-issues | |
*/ | |
namespace Bueltge\PortSwitch; | |
defined( 'ABSPATH' ) || die(); | |
const PORT = 81; | |
/** | |
* Get around the problem with wpmu_create_blog() where sanitize_user() | |
* strips out the semicolon (:) in the $domain string | |
* This means created sites with hostnames of | |
* e.g. example.tld8080 instead of example.tld:8080 | |
*/ | |
add_filter( 'sanitize_user', function ( $username, $raw_username, $strict ) { | |
if ( $strict // wpmu_create_blog uses strict mode | |
&& is_multisite() // multisite check | |
&& PORT == parse_url( $raw_username, PHP_URL_PORT ) // raw domain has port | |
&& false === strpos( $username, ':' . PORT ) // stripped domain is without correct port | |
) | |
$username = str_replace( PORT, ':' . PORT, $username ); | |
return $username; | |
}, 1, 3 ); | |
/** | |
* Temporarly change the port (e.g. :8080 ) to :80 to get around | |
* the core restriction in the network.php page. | |
*/ | |
add_action( 'load-network.php', function() { | |
add_filter( 'option_active_plugins', function( $value ) { | |
add_filter( 'option_siteurl', function( $value ) { | |
// Network step 2 | |
if ( is_multisite() || network_domain_check() ) | |
return $value; | |
// Network step 1 | |
static $count = 0; | |
if ( 0 === $count++ ) | |
$value = str_replace( ':' . PORT, ':80', $value ); | |
return $value; | |
} ); | |
return $value; | |
} ); | |
} ); | |
add_filter( 'gettext', function( $text, $original, $domain) { | |
add_action( 'current_screen', function() { | |
if ( 'network' !== get_current_screen()->id ) { | |
return; | |
} | |
} ); | |
switch( $original ) { | |
case 'The internet address of your network will be %s.': | |
$text = sprintf( | |
__( 'The internet address of your network will be: %s', 'portswitch' ), | |
'<code>' . get_clean_basedomain() . '</code>' | |
); | |
break; | |
} | |
return $text; | |
}, 99, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment