Answer the question
In order to leave comments, you need to log in
Wordpress: custom ajax registration?
Good evening everyone!
I ask for help to solve the following problem:
I am writing a plug-in of additional modules for the theme. One of the modules is custom users, written from scratch, as the standard users module in WP does not meet the requirements of the tasks.
I switched to WP not so long ago (I didn’t use ajax before, because I only recently learned a little about it).
So, in the includes folder there is a class that manages all modules and it has a registration handler, this is how adding a hook for an ajax request looks like in the constructor:
$loader->add_action( 'wp_ajax_nopriv_register_ajax_handler, $this, 'register_ajax_handler' );
This is how the register_ajax_handler function looks like:
public function register_ajax_handler {
// Here I just made a test output
echo 'WORK!';
}
In the js file, which is located in js/cmt-plugin.js, there is a handler on the form that checks everything and if everything is ok, then the following script is launched:
var data = {
'action': 'register_ajax_handler'
};
$.post( ajaxurl, data, function( response ) {
$( '.#form-content' ).html( response );
});
}
as a result, in the console it simply gives out that ajaxurl was not found.
I have no idea why this is so, and in general I understand all this at an abstract level so far.
Please tell me what am I doing wrong? My goal is to understand how you can call an ajax request from a class method, something like what I described came out. Thank you.
Answer the question
In order to leave comments, you need to log in
1. If native users are not suitable, they can do it class My_User extends WP_User
2. Slightly incorrect registration of the action in the OOP format, it is necessary
$loader->add_action( 'wp_ajax_nopriv_register_ajax_handler', array( $this, 'register_ajax_handler') );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question