I want to get the user ID that is currently logged in. I did it successfully when accessed via a web browser. However, when this function is called via dialogflow as webhook / fulfillment it always returns ID = 0. This is my full code in PHP.
<?php
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', '/var/www/html/mine/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-config.php');
do_action( 'plugins_loaded' );
$current_user = wp_get_current_user();
$method = $_SERVER['REQUEST_METHOD'];
// Process only when method is POST
if($method == 'POST'){
$requestBody = file_get_contents('php://input');
$json = json_decode($requestBody);
$action = $json->queryResult->action;
if ($action == "ngopi"){
if ($current_user->ID != 0){
$ngopiResponse = "Hai..".$current_user->display_name;
}
else{
$ngopiResponse = "Hai..Guest";
}
}
$response = new \stdClass();
$response->speech = $ngopiResponse;
$response->fulfillmentText = $ngopiResponse;
$response->source = "webhook";
echo json_encode($response);
}
else
{
echo "Method not allowed";
}
?>
Please give me a solution for this case or maybe there are other alternative ways that I can do?
Thanks in advance.
I want to get the user ID that is currently logged in. I did it successfully when accessed via a web browser. However, when this function is called via dialogflow as webhook / fulfillment it always returns ID = 0. This is my full code in PHP.
<?php
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', '/var/www/html/mine/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-config.php');
do_action( 'plugins_loaded' );
$current_user = wp_get_current_user();
$method = $_SERVER['REQUEST_METHOD'];
// Process only when method is POST
if($method == 'POST'){
$requestBody = file_get_contents('php://input');
$json = json_decode($requestBody);
$action = $json->queryResult->action;
if ($action == "ngopi"){
if ($current_user->ID != 0){
$ngopiResponse = "Hai..".$current_user->display_name;
}
else{
$ngopiResponse = "Hai..Guest";
}
}
$response = new \stdClass();
$response->speech = $ngopiResponse;
$response->fulfillmentText = $ngopiResponse;
$response->source = "webhook";
echo json_encode($response);
}
else
{
echo "Method not allowed";
}
?>
Please give me a solution for this case or maybe there are other alternative ways that I can do?
Thanks in advance.
Share Improve this question asked Jul 1, 2020 at 3:59 Royhan AuRoyhan Au 31 bronze badge 2- The web browser has a 'session' and cookies that WP can use to determine the user. Calling via another method doesn't have this user session so WP doesn't know what user it is. Which use would you expect it to return when you call it from this other method? – mozboz Commented Jul 1, 2020 at 10:29
- @mozboz, thanks for your reply. I expect when I call the function via webhook as dialogflow callback (dialogflow is integrated with WP Plugin) not through a web browser & will be able to return userID from WP user logged in. Thanks – Royhan Au Commented Jul 2, 2020 at 3:07
1 Answer
Reset to default 0I dont understand where are you calling from. I can give you an alternative solution that may or may not work in your case (since I don't fully understand your case):
This is your ajax-url on your site:
echo admin_url( 'admin-ajax.php' );
Usually we attach it to the JS to run AJAX calls:
wp_localize_script( 'THE_JS_THAT WAS_ENQUEUED', 'PARAMS', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
Even without wp_localize_script you can always print the admin_url( 'admin-ajax.php' ) to screen, and input it in any other process.
Now that you have the AJAX link, you can use AJAX to get the current connected user, and return it, as long as you call it from the same browser (meaning same cookie).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742302062a4418208.html
评论列表(0条)