I'm using the following jQuery code to load contents from a template part into another on click. Without the ACF PHP tags, it works. However, when I add these tags in <?php the_field('field_name'); ?> it throws a 500 (Internal Server Error) error. Does anyone have a solution for this, please?

    var baseUrl = "http://projectname:8888/wp-content/themes/custom-theme"

    $(".cross__functional").click(function (e) {
        console.log('cross functional Clicked')
        $("#keContent").load(baseUrl + "/template-parts/components/filename.php");
  • You should not make direct requests to PHP files in a theme or plugin, register an endpoint so you have a URL you can make the request to instead. There are major security and reliability problems with doing it that way.
    – Tom J Nowell
    Sep 15, 2020 at 11:51
  • @TomJNowell Thanks, man. I did it this way cos its the only way I know.
    – jeno
    Sep 16, 2020 at 8:16

1 Answer 1


You are making direct calls to a PHP file in your theme, this is why you have the problem. Because WordPress didn't handle the request, your file did, none of WordPress is loaded, and none of the plugins. There are also lots of security issues with this approach.

If you need to make an AJAX request, make it to a REST API endpoint.

For example, here I register an endpoint that returns Hello World:

add_action( 'rest_api_init', function () {
    register_rest_route( 'jeno/v1', '/helloworld/', [ 'callback' => 'helloworld' ] );
} );

function jeno_helloworld( $data ) {
    return "Hello World";

After placing that in a plugin or functions.php, then I can visit this URL to get the response:


"Hello World"

Here's a snippet of code

    url: "https://example.com/wp-json/jeno/v1/helloworld"
}).done(function( data ) {
    // do something with data
    console.log( data ); // prints hello world in the dev tool console

If you need any parameters, use the $data variable. REST API's return their response as JSON, so you can JSON decode in javascript to get more complex results.

  • Thanks a lot, Tom for your detailed explanation. Is there a way to return JSON data for fields defined within a field group/field groups? The fields I'm trying to pull in are all defined using ACF. Again, thanks for all you do.
    – jeno
    Sep 16, 2020 at 8:15
  • You'd need to look at the ACF docs for how to do that, but I imagine you just retrieve the fields the same way, there's no special REST specific method here. Just retrieve it as a string instead of using echo, same as in shortcodes. Just keep in mind that ACF is offtopic on this site
    – Tom J Nowell
    Sep 16, 2020 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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