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
1 Answer
Reset to default 0Sending 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
版权声明:本文标题:Ajax a php file that has Advanced Custom Fields in it 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302924a1931704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论