How do I properly enqueue JavaScript for AJAX in WordPress?

Responsive Ad Header

Question

Grade: Education Subject: Support
How do I properly enqueue JavaScript for AJAX in WordPress?
Asked by:
59 Viewed 59 Answers

Answer (59)

Best Answer
(362)
Use `wp_enqueue_script()` to enqueue your JavaScript file. Pass the script handle, source URL, dependencies (if any), version (usually `null`), and `true` to load the script in the footer. Within the same function, use `wp_localize_script()` to pass data (like the `admin_url`) to your script. This is crucial for accessing `admin-ajax.php` from your JavaScript.