4

I am developing a WordPress site that redirects to an external payment gateway after a user registers for the site. After the user makes a payment the user is redirected to a page to complete their sign up.

Edit for clarification: Here is the code used for the redirect:

  //code ran after a user signs up located in functions.php

  function process_user(){
    $first_name = $params[1]["first"];
    $last_name = $params[1]["last"];
    $email = $params[3]["value"];
    $password = $params[11]["value"];
    $user_args = array(
      "user_login" => $email,
      "user_pass" => $password,
      "user_email" => $email,
      "first_name" => $first_name,
      "last_name" => $last_name,
      "role" => "subscriber"
    );
    // add user
    wp_insert_user($user_args);
    $user = get_user_by('email',$email);

    //log out user if logged in
    if(is_user_logged_in()){
      wp_logout();
      wp_set_current_user(0);
    }
    $login_data = array();
    $login_data['user_login'] = $email;
    $login_data['user_password'] = $password;
    $login_data['remember'] = true;
    $user = wp_signon($login_data);
    if(!is_wp_error($user)){
        wp_set_current_user( $user->ID, $email );
        wp_set_auth_cookie( $user->ID, true, false );
        wp_redirect("https://another_payment_gateway.com");
        exit;
    }
  }

 add_action( 'wpforms_process_complete_1011','process_user',10, 3 );

The user is redirected and fills out the payment form. Upon success the user is redirected to a landing page. If they are not logged in they are shown a dialog for non logged in users. The landing page contains a shortcode function that is also located in functions.php

Here is the shortcode function:

//shortcode function from functions.php

add_shortcode( 'user_signup_completion', function(){
  ob_start();
  include(get_stylesheet_directory().'/user_signup_completion.php');
  $contents = ob_get_contents(); // put the buffer into a variable
  ob_end_clean();
  return $contents;
});
   //excerpt from user_signup_completion.php
    if(is_user_logged_in()){
      $user = wp_get_current_user();
      //code for logged in users
    }else{
      //message for non logged in users 
    }

The shortcode is used on a page created by a user in WordPress. I hope that this further explain what is going on.

If I go to the account page it is shown that the user is logged in. But when redirected from a different site is_user_logged_in() returns false. Any insight into this issue would be much appreciated.

  • Where do you put this code? Could you show us a bigger context? – Krzysiek Dróżdż Nov 19 '19 at 7:27
  • I have edited the question to give a better explanation. – frank Collins Nov 19 '19 at 16:36
0

Where are you triggering the second code block (that includes is_user_logged_in())? That code may be triggered before set_current_user, which is called after plugins_loaded.

I would take a look at the action reference and see if you are calling your check before WordPress is aware of the user.

| improve this answer | |
  • Hi, the second code block is executed on the template page. The code is part of a shortcode function that was created in the functions.php file. – frank Collins Nov 19 '19 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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