WordPress Tricks

Redirect Logged out users to different page based on Body Class : WordPress redirection using php and jQuery

Suppose you want your visitor not to see any specific pages and want them to redirect to login or register page, then you can do this using this following code. Here i used the body class of wordpress page. Using the Body class and using php and jquery you can redirect it .

<?php
   $classes = get_body_class();
if (in_array('my-profile',$classes)   || in_array('page-id-2324',$classes)    ) {

if( ! is_user_logged_in() ) {
     ?>
  <script>
 window.location.href = "https://yoursiteurl.com/register/";
  </script>	      
     
<?php
}} 
?>

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 )

Facebook photo

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

Connecting to %s