admin管理员组

文章数量:1122832

I am trying to AJAX in a php file that uses Advanced Custom Fields fields, and get a 500 internal error when making the request. The php file works fine if I just use ‘include’, but I need to have it be dynamic in my JavaScript. My AJAX knowledge is limited but have started with this:

$(this).load('/wp-content/themes/theme-name/template-parts/uploads.php', function() { console.log('success'); });

That works to call basic text on the page, but as soon as I put in an ACF field, I get the 500 error.

I am trying to AJAX in a php file that uses Advanced Custom Fields fields, and get a 500 internal error when making the request. The php file works fine if I just use ‘include’, but I need to have it be dynamic in my JavaScript. My AJAX knowledge is limited but have started with this:

$(this).load('/wp-content/themes/theme-name/template-parts/uploads.php', function() { console.log('success'); });

That works to call basic text on the page, but as soon as I put in an ACF field, I get the 500 error.

Share Improve this question edited Jun 18, 2019 at 0:19 jons asked Jun 17, 2019 at 23:11 jonsjons 1291 silver badge5 bronze badges 2
  • Where do you want to include it? On what page - front-end or back-end? Can we see the code in your PHP file? Have you attempted any AJAX code? – Sally CJ Commented Jun 18, 2019 at 0:14
  • Just added the AJAX that I have tried. It is for a front-end page template. – jons Commented Jun 18, 2019 at 0:19
Add a comment  | 

1 Answer 1

Reset to default 0

Sending an AJAX request to a specific file is not the way to do AJAX in WordPress. This is because WordPress itself is not loaded by your file, so you wouldn't be able to use WordPress functions without an error.

There are two proper ways to handle AJAX requests in WordPress:

  • Using the wp_ajax_ hooks: https://developer.wordpress.org/plugins/javascript/enqueuing/#ajax-action
  • Creating a custom REST API endpoint: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

本文标签: Ajax a php file that has Advanced Custom Fields in it