On w3schools or anywhere else I've seen usage of php file in link or form. But after about over 9000 times that I tried to create link with get request to php file, Wordpress did never let me make it. That's what I'm doing:

  1. This is the part of Wordpress template where I'm taking values from DB and creating thumbnails with links for each value:

    global $wpdb;
    $wp_albums = $wpdb->get_results("SELECT * FROM mngr_albums ORDER BY Releasedate");
     foreach($wp_albums as $a) :
     echo '<a href="javascript: loadDoc()">' ?>
    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4" name="albums">
    <div class="thumbnail col-xs-12 col-sm-12 col-md-12 col-lg-12" name="albums">
    echo '<img src="' . $a->Image . '" width="320" height="320" alt="'. $a->Image .'">';
    echo '<br><p style="text-align: center" id="artist" name="'.$a->Artist.'">'.$a->Artist.'</p>';
    echo '<p style="text-align:center; font-size:18px" id="album" name="'.$a->Album.'">'.$a->Album.'</p>'; ?>
    <?php endforeach; ?>
  2. After links were created, I'm making function with AJAX request in same template:

    function loadDoc() {
     var xhttp = new XMLHttpRequest();
     xhttp.onreadystatechange = function() {
     if (this.readyState == 4 && this.status == 200) {
      document.getElementById("container").innerHTML = this.responseText;
     var artist = document.getElementById("artist").getAttribute("name");
     var album = document.getElementById("album").getAttribute("name");
     xhttp.open("GET", "?artist=" + artist + "&album=" + album, true);
  3. So, before get request ?artist=" + artist + "&album=" + album I need to put link to file with this script:

    $artist = $_REQUEST["artist"];
    $album = $_REQUEST["album"];
    $tracks = $wpdb->get_results("SELECT * FROM mngr_tracks WHERE Artist = '".$artist."' AND Album = '".$album."'"); 
    echo '<ul id="list" style="z-index: 1000">';
    foreach($tracks as $t) {
    echo '<li>';
    echo '<a href="javascript: track()" id="track" name="'.$t->FileName.'">'.$t->Track.'</a>';
    echo '</li>';
    echo '</ul>'; 
    if($_REQUEST['file'] != '') {
        $file = $_REQUEST['file'];
        echo $file;
        $p = $wpdb->get_results("SELECT FileDir FROM mngr_tracks WHERE FileName='". $file ."'"); 
        foreach ($p as $pat) {
        $path = $pat->FileDir;

Let's call script in step 3 file.php. Where I need to put it to make get request possible? I have tried to save it in C:\wordpress\htdocs\wp-content\themes\this-theme and access to it through\wordpress\wp-content\themes\this-theme\file.php?album=Album&artist=Artist -- it never worked. Also I tried to put code from step 3 into functions.php and make request to it just like \functions.php?album=Album&artist=Artist -- nothing.

I thought that maybe file.php is not processed by PHP on server, but it always shows me blank page instead of code when I open\wordpress\wp-content\themes\this-theme\file.php in browser.

Where do I need to put the file.php? How do I need to access to it from client side when making get request?


This is not recommended, but if you want to do it, do it this way:

Save your file in your WordPress's root folder, right next to index.php. Then, require WordPress's core files in it, by this code:

define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;

Now, even though the file is not actually a part of WordPress, it can be treated as one, and you can do anything you want with it.

However this might cause security issues, so i recommend you only try it for educational purposes.

To communicate with WordPress using AJAX, use Rest-API instead. You can find an easy solution in this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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