1

I want to solve this problem as I am sending ajax request from on plugin page to another file verifyapi.php but it gives me error like below.

Fatal error: Uncaught Error: Call to undefined function get_option() in verifyapi.php

But when adding below line

require_once('../../../../../wp-config.php');

on top of verifyapi.php it works fine but I don't want it as WordPress don't allow you to add any WordPress core file in your function so is there any other way to do it.

Here is the js code of admin-ui.php page which sends data to verifyapi.php and then get a response back from it.

function savedata()
{
  var keyflag=document.getElementById("key_box_flag").value;
  if(keyflag=1)
  {
    var key=document.getElementById("key_box").value;
  jQuery.ajax({
   url:"<?php echo plugins_url( '/', __FILE__ ); ?>verifyapi.php",
   type:"POST",
   async:true,
   data:{"key":key,
       "save":1 },
   success: function(data)
   {
    location.reload();
   }

  });

  }
  else {

  }
}
1
  • Your second question is not related to the first one. Please consider asking it in a new question. You can just copy-paste it to a new one.
    – Johansson
    Aug 1, 2017 at 11:35

1 Answer 1

0

Instead of doing this, simply write your own function inside the verifyapi.php and then include it in your plugin's main file, by using this:

require_once('verifyapi.php');

This way, you have access to WordPress's function inside your verifyapi.php file.

Now, inside your verifyapi.php file, create an Ajax handler (however I would recommend using a REST endpoint) :

add_action('wp_ajax_your_', 'handler_function');
add_action('wp_ajax_nopriv_your_handler_name', 'handler_function');
function photogram_ajax_handler() {
    // Your verification here
}

So far so good. One last step is to change the URL to admin-ajax.php. To do so, use this:

   ...
   url:"<?php echo site_url('/wp-admin/admin-ajax.php'); ?>",
   ...

All set.

5
  • Hey, it's not working for me can you explain a little bit more? Aug 1, 2017 at 10:24
  • Hi. Please elaborate what does "not work" mean. It's not possible to solve the issue without knowing the details.
    – Johansson
    Aug 1, 2017 at 10:29
  • The thing is that now there is no need of require_once('../../../../../wp-config.php'); but the data that i am sending to verifyapi.php ie data:{"key":key, "save":1 }, is not going there. there are multiple if(isset($_POST['verify'])) funtion in verifyapi.php so i enclosed all of them in on function that is function photogram_ajax_handler() Aug 1, 2017 at 10:48
  • Never mind the issue is fixed a very thanks to you. Aug 1, 2017 at 11:15
  • Sure you can. If it's about this question, you can ask it in comments or update your question. If not, please ask a new question.
    – Johansson
    Aug 1, 2017 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.