0

This is the best I've gotten so far, but I'm getting many errors.

Starting with the main issue; it does redirect to the correct url but the page is showing nothing.

Here is what I am attempting: Reference code

Here is my code.

function pre_process_shortcode() {
  if (!is_singular()) return;
  global $post;
  $user = wp_get_current_user();
  $product_id = 27;
  $product_bought = wc_customer_bought_product( $user->user_email, $user->ID , $product_id );
  if (!empty($post->post_content)) {
    $regex = get_shortcode_regex();
    preg_match_all('/'.$regex.'/',$post->post_content,$matches);
    if (!empty($matches[2]) && in_array('protactPage',$matches[2]) && $product_bought && is_user_logged_in()) {
      wp_redirect('https://learndigital-mena.com/cart-2/');
        exit();
    } else {
      // login form or redirect to login page
         wp_redirect('https://learndigital-mena/my-account/');
        exit();
    }
  }
}
add_action('template_redirect','pre_process_shortcode',1);

dale landry
  • 7,831
  • 2
  • 16
  • 28
Omar Aljoundi
  • 21
  • 1
  • 4
  • Omar, it seems you did not properly add the link and only the reference text made it into your question, please edit your question and reapply the reference code link properly, make sure the link itself is added. – dale landry Dec 19 '20 at 20:39
  • Okay, i will add it to your question for you. – dale landry Dec 19 '20 at 20:41
  • Here : https://wordpress.stackexchange.com/questions/101498/redirect-function-inside-a-shortcode/101515 – Omar Aljoundi Dec 19 '20 at 20:42
  • Please edit your question and apply any textual edits to your link content ;) Simply hit the edit button on the bottom of your question and add to or edit the text/link I added. – dale landry Dec 19 '20 at 20:43

1 Answers1

0

Note this is only client side and not server side :
You can try to replace wp_redirect('https://learndigital-mena/my-account/'); to echo '<script>window.location="https://learndigital-mena/my-account/"</script>'; or header("Location: https://learndigital-mena/my-account/"), but header location will be at the top of the script.

Vandalin
  • 90
  • 9