• 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 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

    (@markcoker)

    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

    (@bcworkz)

    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/

    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.