Skip to main content
Better answer
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

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. =)

Does this has something to do with my functions being inside classes?

It seemed to me that the problem lies here, in myplugin_ajax_script.js: action: 'myplugin_ajax_function' — shouldn't the AJAX action be myplugin_ajax_script?

Because in Myplugin::define_admin_hooks(), that's the action you registered. And shouldn't that line, which registers the action, look like so:

$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 defined a global PHP function named myplugin_ajax_script.

So the AJAX action defined in Myplugin::define_admin_hooks() has been corrected (it's just a typo in the question).

But there's another issue in your code, also in the above function, where the second parameter for $this->loader->add_action() should actually be $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' );

And I actually noticed another issue — in Myplugin_Admin::myplugin_ajax_function(), I believe you wanted to call the myplugin_do_magic() method/function in the Myplugin_Admin class, 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!

Unless of course if for example there's a function named myplugin_do_magic defined in the global scope.

So make sure that you call a class method properly. =)

Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

Does this has something to do with my functions being inside classes?

It seemed to me that the problem lies here, in myplugin_ajax_script.js: action: 'myplugin_ajax_function' — shouldn't the AJAX action be myplugin_ajax_script?

Because in Myplugin::define_admin_hooks(), that's the action you registered. And shouldn't that line, which registers the action, look like so:

$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 defined a global PHP function named myplugin_ajax_script.