How to redirect WordPress public site to any url

WordPress is great as a content manager, but when you want to publish that content, the performance and other requirements can make it advisable to use another publishing environment. For example, you can use Django and the wordpress JSON API. In these cases you may be interested in redirecting calls to the public part of WordPress to another URL.

Here you have the code to redirect public calls to another url. Place it into functions.php.



add_action( 'template_redirect', 'my_template_redirect', 1 );

function my_template_redirect() {

    if ( !is_admin() 
      && !wp_doing_ajax() 
      && strpos($_SERVER['REQUEST_URI'], 'wp-admin')=== false 
      && !is_user_logged_in()) {

     wp_redirect( "https://mynewsite.com", 301 );
     exit();
   }

}



 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s