Does this has something to do with my functions being inside classes?
It seemed to me thatSo the problem lies here,AJAX action defined in myplugin_ajax_script.js
: actionMyplugin: 'myplugin_ajax_function':define_admin_hooks()
— shouldn'thas been corrected (it's just a typo in the AJAXquestion).
But there's another issue in your code, also in the above function, where the actionsecond parameter for $this->loader->add_action()
should actually be myplugin_ajax_script$plugin_admin
? (which is an object and an instance of the Myplugin_Admin
class) and not wp_ajax
:
Incorrect:
$this->loader->add_action( 'wp_ajax_myplugin_ajax_function', 'wp_ajax', 'myplugin_ajax_function' );
Correct:
$this->loader->add_action( 'wp_ajax_myplugin_ajax_function', $plugin_admin, 'myplugin_ajax_function' );
BecauseAnd I actually noticed another issue — in MypluginMyplugin_Admin::define_admin_hooksmyplugin_ajax_function()
, that's the actionI believe you registered. And shouldn't that line, which registerswanted to call the actionmyplugin_do_magic()
method/function in the Myplugin_Admin
class, look like so: you should use $result = $this->myplugin_do_magic($url);
(note the $this->
) because otherwise, then you would end up with a 500 internal server error!
$this->loader->add_action( 'wp_ajax_myplugin_ajax_script', 'wp_ajax', // wrapped for brevity
array( $plugin_admin, 'myplugin_ajax_function' ) );
Because I don't see where you definedUnless of course if for example there's a global PHP function named myplugin_ajax_scriptmyplugin_do_magic
defined in the global scope.
So make sure that you call a class method properly. =)