search

Home  >  Q&A  >  body text

How to access value in JS file stored using PHP function

<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粉590929392P粉590929392506 days ago563

reply all(1)I'll reply

  • P粉785957729

    P粉7859577292023-07-29 17:02:15

    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.

    reply
    0
  • Cancelreply