I am developing plugin to called Ajax so I do in the plugin and enqueue js in my main file in js file add action in the data section and that data called another .php file add same action name function add add_action but that after Ajax called gave me error add_action is not defined so please suggest me how to called that Ajax in my plugin.

===js file ==
var ajaxurl = myScript.ajaxurl; (Custom File Ajax URL)
jQuery("#idForm").on('submit', function(){
    var searchIDs = jQuery("#idForm input:checkbox:checked").map(function(){            
        var value = jQuery(this).val();
        var name = jQuery(this).attr('name');
        var rel = jQuery(this).attr('rel');
        var stuff = {'name':name,'value':value,'rel':rel,};
        return stuff;
    var tmp = searchIDs;
            type: "POST",
        url: ajaxurl, //Relative or absolute path to response.php file
            data: {
                action     : 'my_ajax_action', // load function hooked to: "wp_ajax_*" action hook
                search_val : JSON.stringify(searchIDs),           // PHP: $_POST['first_name']
                //last_name  : 'Cena',           // PHP: $_POST['last_name']

            //data: {search_val:JSON.stringify(searchIDs)},
            success: function(data){
==========AJax Own File========
add_action( 'wp_ajax_my_ajax_action', 'my_ajax_action_callback' );
function my_ajax_action_callback(){
//Code Here
  • How did you implement your JavaScript, and which file are you targeting with your AJAX call? Can you share some code you use? – Milan Petrovic Sep 13 '17 at 7:52
  • @MilanPetrovic Check My code and let me know what was a problem going on.. – Vishit Sep 13 '17 at 8:38

You should not use your own AJAX handler PHP file. Your problem is that your file calls add_action() function and that function is part of WordPress, your file is not loaded in the WordPress context, so that function is not loaded. You need to load WP in your file first.

But, that is not a good way to do this. WordPress already has a dedicated AJAX handler file and you should be using that, and not your own file that might be a security risk.

This is a good article on using AJAX in plugins and it is on official WordPress Codex: AJAX in Plugins.

  • can you please update my code and let me know I can't understand Ajax? – Vishit Sep 13 '17 at 9:42
  • I create my main plugin file but does not work. and i chnage admin_ajax url in js file add_action('wp_ajax_ajax-script_js', 'makeBooking'); function makeBooking(){ die('test');} But Output comes 0 in popup. – Vishit Sep 13 '17 at 9:45
  • The article I listed contains several examples you can use. I can't fix your code because it is not complete, I have no idea how it works and what file you have. – Milan Petrovic Sep 13 '17 at 9:52
  • I have 3 files 1) Plugin Mail File 2) Js File 3) My Ajax URl File – Vishit Sep 13 '17 at 9:53
  • How can I manage my code in 2 files? – Vishit Sep 13 '17 at 9:54

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.