Reputation: 7
I have a working AJAX function that sends a variable to a page I made within the template directory. I am using Advanced Custom Fields and execute the get_field($name, $id)
function, but I get a 500 server error. I looked in my error log (Apache) and saw that it got an undefined function error. That's strange, because when I use it on front-page.php it actually works.
[Fri May 09 11:56:19.336469 2014] [:error] [pid 2235] [client 192.168.1.111:54118]
PHP Fatal error: Call to undefined function get_field()
in /var/www/html/wp-content/themes/Custom Theme/setphoto.php on line 4,
referer: 192.168.1.115/
PHP
<?php
$p = $_GET['p'];
$url = get_field("nieuwsveld", $p);
echo $url;
?>
JavaScript
$(".a_item").mouseover(function(){
// Krijg url + id van href
var href = $(this).attr("href");
var p = href.replace("http://192.168.1.115/?p=", "");
var xhr = new XMLHttpRequest();
xhr.open("GET", "wp-content/themes/HSV Saints/setphoto.php?p="+p, true);
xhr.send();
xhr.onload = function(){
console.log(xhr.responseText);
}
// $("#photo_news_photo").attr("src", img_url);
});
</script>
How can I enable WordPress to use get_field in an external file within my template directory?
Upvotes: 0
Views: 5211
Reputation: 11
I just had this problem and fixed it by including acf.php at the top of the php file that handled the ajax request.
So for your code it would be
<?php
include_once(__DIR__.'path/to/file/acf.php');
$p = $_GET['p'];
$url = get_field("nieuwsveld", $p);
echo $url;
?>
Upvotes: 1