I'm building a custom plugin for a chatbot in WordPress, where I've written some logic in a JS file to get the API and responses. As request body I am sending two parameters. One of the parameters is stored in the wp database using a PHP function (my core plugin file). So I want to access that function/that value in my JS file so I can send it in the API request.
JS function -
fetch('https://whatgpt.up.railway.app/api/query-train-gpt', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: userMessage, number: phoneNumber, }), })
Backend PHP functions -
function get_admin_phone_number() { // Retrieve the stored admin phone number from the database. $admin_phone_number = get_option('admin_phone_number', ''); // Provide a default value if the option is not set. // Return the admin phone number. return $admin_phone_number; }
You can set the output value of the function to $_SESSION['admin_phone_no'] and then use the value from the session in your JS file.