Skip to content

Instantly share code, notes, and snippets.

@jamesmorrison
Created September 29, 2017 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmorrison/d87cc191c109456a1d4c2df75c440bbc to your computer and use it in GitHub Desktop.
Save jamesmorrison/d87cc191c109456a1d4c2df75c440bbc to your computer and use it in GitHub Desktop.
Improve WP Signup - redirect to the main network URL with referral parameters
<?php
/**
* @package Improve WP Signup - redirect to the main network URL with referral parameters
* @author James Morrison
*
* The WP core /wp-signup.php file calls "get_header( 'wp-signup' );" we can hook into this to force a redirect
* Add this as a MU Plugin, make sure NOBLOGREDIRECT is *not* defined as it'll override this
*
* Notes:
* Whilst you can set up a redirect from the NOBLOGREDIRECT constant, the advantage of using this
* is that you can capture the path that triggered the call to signup; as seen in the example below,
* this uses Google Analytics parameters to help track where the requests came from without
* the need to setup infrastructure level redirects.
*
* Additionally, NOBLOGREDIRECT has a nasty habit of messing about with 404 pages - in some cases
* it treats these as an invalid network site and triggers the redirect instead of showing the 404 page
*
**/
// Security check
defined( 'ABSPATH' ) || die();
// Hook into the header action to redirect
add_action( 'get_header', function( $name ) {
if ( 'wp-signup' == $name ) {
wp_redirect(
esc_url_raw(
add_query_arg( [
'utm_source' => isset( $_GET[ 'new' ] ) ? esc_attr( $_GET[ 'new' ] ) : '',
'utm_medium' => 'referral',
'utm_campaign' => 'no-blog-redirect'
], network_home_url() )
),
301 );
exit();
}
}, 1, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment