0

My users create posts with links to future posts which are in Draft status. We use Post Name as our permalink structure.

Eg.

 <a href="/future-draft-post">Here is our next fantastic post coming next week!</a>

Since they are logged in, our users click the links and are then redirected to the Drafts for editing. And as normal, when non-logged-in users click those links they get a nasty 404.

What I want is for non-logged-in users to click such links and be redirected to a custom page that says 'Coming Soon!'

How do I do that?

Two other optional features we would like.

  1. Ideally, I would like to pass the scheduled publish date to the 'Coming Soon Page! (eg. 'Coming July 10, 2016!)

  2. I would only like this behaviour for a certain category of posts. Other posts should redirect to a 404 as per usual.

So in addition to checking the status of the post we need a way to check the category and the publish date of the post in deciding whether to 404 or go to the custom page.

1 Answer 1

1

Here is what I came up with. Seems to be working. If anyone has suggestions for improvement, I'm all ears:

// Redirect links to future posts to Coming Soon page.
add_action('wp','redirect_coming_soon_posts', 0);
function redirect_coming_soon_posts(){
  global $wpdb; 
  if ($wpdb->last_result[0]->post_status == "future" &&
    $wpdb->last_result[0]->post_date_gmt != '0000-00-00 00:00:00' && 
    ! is_admin()  ):
      session_start();

     $_SESSION['next_post_id'] = $wpdb->last_result[0]->ID;         
     wp_redirect( '/coming-soon', 301 );
    exit();
   endif;
  }

function sess_start() {
  if (!session_id()) {
    session_start();
    }
 }
 add_action('init','sess_start');

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.