it needs a full URL because even though it may be the right location relatively on the back end, it’s not on the same on the front end, at the URL where your page is ultimately served.
you’re on the right path with wp_localize_script
. you want to enqueue your ajax script, then pass the admin ajax url to wp_localize_script
:
function my_init_method(){
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/my_ajax_script.js', array( 'jquery' ) );
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('init', 'my_init_method');
now within your ajax script you can refer to MyAjax.ajaxurl
for the URL.
Check this post for a great writeup on properly using ajax in plugins and themes, this is what the WP Codex links to as an example.
Related Posts:
- ajaxurl not defined on front end
- Using Ajax call in jQuery doesn’t work in widget
- Admin-ajax.php appending a status code to ajax response
- how to search users by ajax live search
- jquery & ajax sending data to php
- Ajax submit result opens in admin-ajax.php
- insert query on a custom table using ajax with jQuery plugin Jeditable
- Ajax: Populate with content from a post’s ID not working – duplicating current page html instead
- Data not insert and update through ajax and jQuery in admin page?
- WP ajax requests not stacking?
- ajax working when function is on child theme but not in plugin page
- Ajax +wordpress onClick link redirect to new page and create html content
- Get cat parameter from admin-ajax
- Ajax on the Administration Side of plugin – returns 0
- Jquery php request is returning a weird result
- WordPress Ajax not returning Response
- wp_ajax add_action fuction won’t fire on custom jQuery action
- Filterable posts using categories
- How to get values from Tinymce visual editor popup?
- WordPress function is not called and ajax return 0
- Ajax call not working with
- Page reload occurs before request finishes
- Why does WordPress use outdated jQuery v1.12.4?
- How to add a javascript snippet to the footer that requires jQuery
- Why does WordPress add 0 (zero) to an Ajax response?
- How to include jQuery and JavaScript files correctly?
- How Do I Use jQuery UI In My Plugin
- Saving data-URI to media library
- Remove profile picture option (and other things) from profile.php (in admin)
- Nonces can be reused multiple times? Bug / Security issue?
- How can I run AJAX on a button click event?
- How-to implement admin Ajax inside an admin WP_List_Table?
- Test to see if jQuery or Prototype is queued by another plugin?
- What is nonce and how to use it with Ajax in WordPress? [duplicate]
- Empty POST data on server on AJAX request using Angular $http
- jQuery in header or footer
- Do I require the use of nonce?
- Hook the Keydown Event in the TinyMCE Post Editor
- Using AJAX in FrontEnd with WordPress Plugin Boilerplate (wppb.io)
- Build path for a custom portfolio plugin
- Add a jQuery function to admin pages
- How to Add a .js file Only in one specific Page Dynamically to Head
- Using AJAX in a plugin to submit form – REALLY confused
- wp_localize_script $handle
- Help adding image upload functionality to widget
- How to enable users to down-vote in this simple voting counter (that uses the post meta)?
- How to trap “Publish” button to check for meta box validation?
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- Adding callback function for wp_ajax_ has no effect
- get all products of one category
- Use js script from one plugin in another plugin
- Get returned variable from a function to add_shortcode function
- Plugin Settings not Saving on Ajax re-ordered table
- How to post form in ajax mode and handle it in wordpress
- Why is my ajax call refreshing the page?
- wp_enqueue_style built in styles
- WP_LOCALIZE_SCRIPT doesn’t work
- What is the difference between these two methods of writing $ instead of jQuery in WordPress [closed]
- Timeout on Admin-Ajax?
- Admin-ajax.php is dying “0” without processing function
- Plugin admin page meta_box toggle and order state not saving
- WordPress plugin options and jQuery
- Comment `Reply` link doesn’t work if comments are loaded from ajax
- Problem with WordPress Ajax form
- How to create popup same as wordpress popup feature
- Only execute jQuery function(on document ready) on the page has shortcode from plugin [duplicate]
- Cannot search post by taxonomy
- WordPress Ajax callback function from plugin – OOP
- WP AJAX is not working, always returns 0
- Ensure function has completed before allowing another Ajax call
- Mixing Regular Javascript With jQuery in a Plugin
- Loading jQuery and jQuery plugin script files correctly [duplicate]
- Check if email address exists front end with AJAX in a plugin
- jquery document ready function not being called [closed]
- Frontend Ajax call not working using wp_ajax, wp_enqueue_script and wp_localize_script
- Fetching the value of forms in WordPress AJAX
- wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated
- Add a row on clicking each post featured image
- how to add Jquery script to one page?
- include wp-blog-header not working on MAMP
- Any problem in using native jquery ajax style instead of using admin-ajax.php?
- How to test nonce with AJAX – Plugin development
- Slow WP_query due to nested wp_query. Need Suggestions
- Show special field when correct shipping is chosen
- Is there an event or an other method that tells me the preview is loaded?
- .mo translation strings not loading in PHP scripts that handle AJAX calls
- How can I pass get_the_author_meta(‘user_email’) through the REST API?
- Woocommerce checkout update totals with datepicker
- Including the necessary functions for a custom ajax registration form
- Are there any scripts, classes, and/or functions built-in to WP for a plugin to export/import its saved data from wp_options?
- How can I rewrite a URL to pass requests to a custom method via AJAX? (I can’t use admin-ajax.php)
- How to localize admin.php only once
- get post attachment using ajax
- Dashboard – get status and position of metaboxes and pass them to ajax method
- WordPress ajax doesn’t display object method on jQuery .change() function
- Create a new post using rest api and save featured image using an external image url
- Ajax contact form widget plugin data not insert in database
- jQuery selectors for editor elements safe to use?
- wp.template() returns tags in Ajax response
- How to get Metabox custom field to show checked if value is updated using post meta query?