login - Set logged in user based on API response

I have an external application that I want to redirect to a page on a wordpress site. The page needs to be different bas

I have an external application that I want to redirect to a page on a wordpress site. The page needs to be different based on who is logged in.

The application has an API endpoint which I can call to get the logged in user.

Basically, I need the following to happen:

1) When the page loads, the API gets called to see who is logged in.

2) If the user does not exist, an account is created. If the user does exist, their data is displayed.

Is this possible to do? I tried googling it and found nothing. I'm thinking I will want the API call attached to the wp_init hook but not totally sure.

Any help is appreciated and I can provide more information if necessary!

I have an external application that I want to redirect to a page on a wordpress site. The page needs to be different based on who is logged in.

The application has an API endpoint which I can call to get the logged in user.

Basically, I need the following to happen:

1) When the page loads, the API gets called to see who is logged in.

2) If the user does not exist, an account is created. If the user does exist, their data is displayed.

Is this possible to do? I tried googling it and found nothing. I'm thinking I will want the API call attached to the wp_init hook but not totally sure.

Any help is appreciated and I can provide more information if necessary!

Share Improve this question asked Jun 4, 2019 at 5:56 ellenellen 3451 gold badge5 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

init is probably a good hook to do the API call, if you need know somehting about the current WP user as the user is authenticated by that time. You can check the WP user login status with is_user_logged_in(), if needed.

Or if you need to do some redirecting, then template_redirect could also be a viable hook. If you just want to change the page template, then determine the correct template inside template_include filter.

WordPress has its own HTTP API that you could perhaps use to make the API call by using wp_remote_get(). But I guess cUrl will work as well, if you prefer to use it.

If you also need to create a new user to WP and not just to your API, you can use wp_insert_user() and then use wp_signon() to log the new user in.

Here's a rough example regarding wp_remote_get(),

function check_login_status() {

  // if relevant to check wp login status
  // if ( is_user_logged_in() ) {
  //   // do something
  // }

  $api_url = 'http://api.isuserloggedin';
  // defaults shown
  $api_request_args = array(
    // 'timeout'     => 5,
    // 'redirection' => 5,
    // 'httpversion' => '1.0',
    // 'user-agent'  => 'WordPress/' . $wp_version . '; ' . home_url(),
    // 'blocking'    => true,
    // 'headers'     => array(),
    // 'cookies'     => array(),
    // 'body'        => null,
    // 'compress'    => false,
    // 'decompress'  => true,
    // 'sslverify'   => true,
    // 'stream'      => false,
    // 'filename'    => null
  );
  $api_request = wp_remote_get( $api_url, $api_request_args );

  if ( ! is_wp_error( $api_request ) ) {
    $response = wp_remote_retrieve_body( $api_request );
    // if needed, decode the $response
    // $response = json_decode( $response );

    // do something with the $response

  } else {
    // api call failed    
  }

}
add_action( 'init', 'check_login_status' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745435985a4627610.html

相关推荐

  • login - Set logged in user based on API response

    I have an external application that I want to redirect to a page on a wordpress site. The page needs to be different bas

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信