How to access value in JS file stored using PHP function
P粉590929392
P粉590929392 2023-07-29 00:04:23
0
1
575
<p>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. </p><p>JS function -</p><p><br /></p> <pre class="brush:php;toolbar:false;">fetch('https://whatgpt.up.railway.app/api/query-train-gpt', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: userMessage, number: phoneNumber, }), })</pre> <p>Backend PHP functions -</p> <pre class="brush:php;toolbar:false;">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; }</pre> <p><br /></p>
P粉590929392
P粉590929392

reply all(1)
P粉785957729

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template