Title: Plugin uses ajax to call php function
Last modified: August 30, 2016

---

# Plugin uses ajax to call php function

 *  [markcoker](https://wordpress.org/support/users/markcoker/)
 * (@markcoker)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/plugin-uses-ajax-to-call-php-function/)
 * Hi I’m just wondering the best way to do this. External php is ‘Hacky’ (explained
   after code) in WordPress but works (see snippet below), I’m assuming by the nature
   of php i can’t call a function inside the UI file (below):
 *     ```
       <input type="text" placeholder="function search" onkeyup="search(this.value)">
       <script>
           xmlhttp.onreadystatechange = function() {
                          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                              document.getElementById(\'search\').innerHTML = xmlhttp.responseText;
                          }
                       var locate = \''.plugins_url( 'locate.php', __FILE__ ).'\';
                       xmlhttp.open("GET", locate+"?search="+searchInput);
                       xmlhttp.send();
       </script>
       <textarea rows="15" cols="50" id="search"></textarea>
       ```
   
 * i have to import wp files to check for admin status/get_home_path() etc, which
   requires a static ‘../../../’ . This is because wp functions aren’t loaded by
   2nd. Will looking [Hooks, Actions and Filters](https://codex.wordpress.org/Plugin_API)
   solve this issue? if so could you point me to the function/s i should use. (yep,
   i plan i learn this in great detail as soon as i get some sleep)
 * Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [markcoker](https://wordpress.org/support/users/markcoker/)
 * (@markcoker)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/plugin-uses-ajax-to-call-php-function/#post-6748430)
 * yeah, i think silly question sorry guys. will confirm later but i believe add_action
   will do this.
 * i really gotta stop hacking up code < 2hrs of sleep in 2days.
 * If someone could confirm as well that would be great, tips are also encouraged!
   🙂
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/plugin-uses-ajax-to-call-php-function/#post-6748468)
 * Close but not quite I think. AJAX is fine, but it must be done properly. The 
   AJAX request must go through admin-ajax.php. By doing that, you need to add an
   action callback, so your inclination is correct, but the way to go about it is
   not what you imagined I think. More on proper AJAX calls:
    [https://developer.wordpress.org/plugins/javascript/ajax/](https://developer.wordpress.org/plugins/javascript/ajax/)
 * Never use relative (or absolute for that matter) references to include core files,
   you end up with non-portable code. By going through admin-ajax.php there is no
   need, it’s taken care of for you.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Plugin uses ajax to call php function’ is closed to new replies.

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)
 * [correct](https://wordpress.org/support/topic-tag/correct/)
 * [external](https://wordpress.org/support/topic-tag/external/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [internal](https://wordpress.org/support/topic-tag/internal/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/plugin-uses-ajax-to-call-php-function/#post-6748468)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
