0

I have two php files; one is called page-my-template.php, and in it, I have a <div></div> which I would like to contain all the code from another php file called my-content.php. I thought this would be simple but it is proving to be a huge headache.

Before I continue, I am not using JSON, I am not using any forms or passing data, and I am not writing a plugin. I simply have a template file that only is used once on my page called page-my-template.php, and I need to dynamically call the code from my-content.php in a div. It seems natural to use jQuery.load(), but no, please read below.

Inside the file my-content.php, I am using many core functions such as get_option(), WP_Query(), etc. Of course these are causing 500 errors if I use jQuery.load("my-content.php"). I realize I need to go through admin-ajax.php in order to do this the proper way.

I've spent a lot of time reading tutorials about how to use ajax with Wordpress. All the tutorials I see on how to use ajax with Wordpress are very complicated and cover far more than what I am seeking, such as creating custom hooks, using forms, and writing plugins. Is there a simple way to include the functionality of admin-ajax.php, so that I have access to the Wordpress core methods as I call the my-content.php file? If so, please tell me what the code would be to include it, and exactly where to put that code.

2

Using AJAX in WordPress is fundamentally pretty simple.

You need a function that handles the AJAX request, and a couple of add_action calls to map that function to the action you pass to admin-ajax.php, in this case, some_action:

function wpd_ajax_function() {
    get_template_part( 'my-content' );
    wp_die();
}
add_action( 'wp_ajax_some_action', 'wpd_ajax_function' );
add_action( 'wp_ajax_nopriv_some_action', 'wpd_ajax_function' );

This would go in your theme's functions.php file. You can see that all the function does is load your my-content.php file, and then exits execution.

Once you have that in place, you can test this by visiting admin-ajax.php in your browser with your some_action action appended:

http://yourdomain.com/wp-admin/admin-ajax.php?action=some_action

and you should see your output. If you hit that URL with your jQuery.load() code, you should have everything you need.

  • Great, I'll try that out at work tomorrow. Looking forward to it. – Mark Dec 20 '16 at 2:34
  • This worked beautifully, but what if I need to do this for more than one file that will be called via ajax, such as elsewhere in the site? – Mark Dec 20 '16 at 15:22
  • 1
    You could pass an extra parameter with the request that indicates different files you want to load, or you could simply add additional functions and map them to a different action. – Milo Dec 20 '16 at 16:06
  • So would it be: function wpd_ajax_function($num) { get_template_part( "my-content"$num ); ... Then, yourdomain.com/wp-admin/… (etc.) – Mark Dec 20 '16 at 16:16
  • 1
    You could do it that way, though I suggest validating the passed value in some way, depending on what it is, make sure it is what you expect so you're not just including arbitrary filenames you don't intend to. – Milo Dec 20 '16 at 16:33

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.