Timeline for Can't get AJAX call working in custom plugin
Current License: CC BY-SA 4.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 12, 2022 at 15:26 | vote | accept | DeltaG | ||
Jul 12, 2022 at 15:26 | comment | added | DeltaG | You are a real life saver here! I fixed both of the issues you've pointed out in your revised answer. It works correctly now! Also thank you for your explanation regarding the use of Classes and how to call the functions inside them. Again, you saved my day! | |
Jul 12, 2022 at 14:17 | comment | added | Sally CJ | I believe you get that 500 error because WordPress/PHP tried to run your callback and yet, it called a function that didn't exist.. Nonetheless I've completely revised my answer, so check it out and let me know. | |
Jul 12, 2022 at 14:07 | history | edited | Sally CJ | CC BY-SA 4.0 |
Better answer
|
Jul 12, 2022 at 13:12 | comment | added | DeltaG |
When I use $this->loader->add_action( 'wp_ajax_myplugin_ajax_function', $plugin_admin, 'myplugin_ajax_function' ); I get a 500 error on the admin-ajax.php file in the console. When I use $this->loader->add_action( 'wp_ajax_myplugin_ajax_function, 'wp_ajax', array( $plugin_admin, 'myplugin_ajax_function' ) ); I get the same result (0) as I did before.
|
|
Jul 12, 2022 at 13:04 | comment | added | Sally CJ |
Or use $this->loader->add_action( 'wp_ajax_myplugin_ajax_function', $plugin_admin, 'myplugin_ajax_function' ); ? (note the 2nd parameter)
|
|
Jul 12, 2022 at 12:43 | comment | added | Sally CJ |
Actually, there were 2 issues in your original code in question - 1) the AJAX action, and 2) the AJAX callback (the 3rd parameter for $this->loader->add_action() ); and now you've corrected the 1st issue, but the 2nd one is still there - use array( $plugin_admin, 'myplugin_ajax_function' ) like in my answer, and not just 'myplugin_ajax_function' . Or was that also another typo in the question only? 🤔
|
|
Jul 12, 2022 at 12:16 | comment | added | DeltaG | Yes, you are right about that mistake. Thanks for pointing it out. I think I've copied it wrongly in here, in my live code it was indeed the function name instead of the name of the script. I've updated my question with the correct action name. | |
Jul 11, 2022 at 15:35 | history | answered | Sally CJ | CC BY-SA 4.0 |