0

In my plugin there are many pages. I want to redirect to a login page if the user is not loggedin on a particular page. I used below code, but it redirect to login page on all pages. I want to specific page only.

<?php
class Coupon {
  private static $instance = null;

  private function __construct(){
    add_action( 'init', array(&$this, 'add_shortcodes' ));
    add_action( 'template_redirect', array(&$this, 'redirect_user' ));
  }

  public static function get_instance(){
    if(!self::$instance){
      self::$instance = new Coupon();
    }
    return self::$instance;
  }

  function add_shortcodes() {
    add_shortcode( 'coupon-form', array(&$this,'coupon_form' ));
  }

  function redirect_user() {
    Log::d("redirected");
    if ( ! is_user_logged_in() && ! is_page( 'login' ) ) {
      wp_redirect( Login::url() );
      exit;
    }
  }
  
  function coupon_form() {
    ob_start();
      ?>
          <div class="coupon_widget_form">
              <form id="coupon_form" name="coupon_form" method="post" action="">
                  <div>
                      <label for="coupon">Coupon</label>
                      <input type="text" id="coupon" value="YZ7U PQnm EAQ3" size="25" name="coupon" />
                  </div>
                  <div>
                      <input type="submit" name="coupon_submit" value="Redeem"/>
                  </div>
              </form>
          </div>
      <?php
      return ob_get_clean();
  }
}
?>
1
  • You didn't mentioned the particular page in your code. There should be a list of page or a page within your condition. Make sense? Commented Aug 23, 2020 at 6:05

1 Answer 1

1

From what I see, you just need to change the if in your redirect_user() function to:

// List of Page slugs of the particular pages in question.
$pages = array( 'page-slug', 'another-slug', 'etc-etc' );
// .. or specify a list of Page IDs: array( 1, 2, 3, ... )

// Redirect only if the current Page's slug (or ID) is within the above list.
if ( ! is_user_logged_in() && is_page( $pages ) ) {
    wp_redirect( Login::url() );
    exit;
}

But of course, by "page", I presumed you meant a Page, i.e. post of the page type?

And if that's not the case, i.e. you're (also) referring to other WordPress pages like CPT and term archives, then you can check the conditional tags here and just use the one(s) that match your particular pages (is_page() is one of those conditional tags). But the exact condition/logic will certainly depend on the type of pages in which the login redirection should be applied.

// Example..
if ( ! is_user_logged_in() && (
    is_page( array( 'foo', 'bar', 'etc-slug' ) ) || // check if it's one of the Pages
    is_singular( 'my-cpt' )                         // or a if it's any single CPT pages
) ) {
    wp_redirect( Login::url() );
    exit;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.