简体   繁体   中英

Wordpress add function to functions.php that only fires on login

Updated Solution Attempt:

function identify_user( $user_login, $user ) {
    $user_obj = json_encode(array("user_id"=>$user->ID,"user_email"=>$user->user_email));
    $curl = curl_init("myApp.com/endpoint");
    curl_setopt( $curl, CURLOPT_POST, true );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,$user_obj);
    curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($curl);
    curl_close( $curl );
    if(curl_error($result)) {  echo'<pre>';var_dump($user);echo'</pre>';die();}
}

add_action( 'wp_login', 'identify_user', 10, 2);

This function works when I replace wp_login with wp_enqueue scripts, but I cannot seem to get it working with wp_login.

I think you're problem is with wp_get_current_user() curl . If you're in WordPress you often don't need to use the curl library directly. WP has a built in HTTP API implemented by the WP_Http class. Even that though is probably lower than you need to go. I'd suggest trying the wp_safe_remote_post() function to do POST to your endpoint. The first parameter is the URL you want to post to and the second is an optional array of args to modify the request. A list of all accepted $args can be found here .

As for wp_get_current_user() , you shouldn't need that either because your callback is provided two arguments. The first is the username and the second contains a WP_User object (the same object returned by wp_get_current_user() ). The documentation for the hook can be found here .

Try this (original snippet was moved to the bottom of this answer):

function identify_user( $user_login, $user ) {
    if( is_user_logged_in() ):
    $_id = $user->ID;
    $_email = $user->user_email;
    $user_info = json_encode(array("user_id"=>$_id,"user_email"=>$_email));

    $request_args = array(
        'httpversion' => '1.1',
        'headers' => 'Content-Type:application/json',
        'body' => $user_info,
    );
    $response = wp_safe_remote_post("https://myWebsite.com/endpoint", $request_args);
    if (is_wp_error($response)) {
        // request failed, handle the error.
        // Use $response->get_error_message(); to get error message
    } else {
        // It worked!!!
    }
    endif;
}

You'll notice wp_safe_remote_post() will return a WP_ERROR object if the request fails for some reason. It can be useful to use the WP_Error::get_error_message() to see what went wrong if the request fails.

Simple Debugging

Additionally, if you're new to PHP/WordPress and are working in a non production environment here's a trick to figure out if the callback is actually getting run and what the content's of the arguments are.

function identify_user( $user_login, $user ) {
  echo'<pre>';
  var_dump($user); 
  echo'</pre>';
  die();
}

add_action( 'wp_login', 'identify_user', 10, 2);

This will print out the contents of $user to the screen then stop the res t of the execution. If that happens you know the problem is further down in the function body.

A Few Notes

It's worth noting that the is_user_logged_in() check is redundant because the wp_login is only fired at the very end of the wp_signon() function meaning the user was literally just authenticated but I left it in the code above because redundant security is not the worst thing ever.

Also, you can probably just use $user->ID and $user->user_email directly instead of putting them into variables as you only use them the once. I left them in the code above though because it doesn't effect whether or not the code works.

Original answer's code

function identify_user( $user_login, $user ) {
    if( is_user_logged_in() ):
    $_id = $user->ID;
    $_email = $user->user_email;
    $user_info = json_encode(array("user_id"=>$_id,"user_email"=>$_email));
    $curl = curl_init("myWebsite.com/endpoint");
    curl_setopt( $curl, CURLOPT_POST, true );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $user_info);
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_exec( $curl );
    curl_close( $curl );
    endif;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM