name}_action", array( $this, 'ajaxCb' ) ); # Guests: add_action( "wp_ajax_nopriv_{$this->name}_action", array( $this, 'ajaxCb' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'scriptsLocalize' ) ); } public function scriptsRegister( $page ) { $file = 'YourAJAXHandlingFile.js'; wp_register_script( $this->name, plugins_url( "assets/scripts/{$file}", __FILE__ ), array( 'jquery', ), filemtime( plugin_dir_path( __FILE__ )."assets/scripts/{$file}" ), true ); } public function scriptsEnqueue( $page ) { wp_enqueue_script( $this->name ); } public function scriptsLocalize( $page ) { $this->nonce = wp_create_nonce( "{$this->name}_action" ); wp_localize_script( $this->name, "{$this->name}Object", array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), '_ajax_nonce' => $this->nonce, 'action' => "{$this->name}_action", 'post_type' => get_current_screen()->post_type, # etc. ) ); } public function renderForm() { wp_nonce_field( "{$this->name}_action", $this->name ); # @TODO Build form # @TODO Hook somewhere } public function ajaxCb( $data ) { $data = array_map( 'esc_attr', $_GET ); check_ajax_referer( $data['action'] ); # @TODO Handle processing of data in here # @TODO Validate data with absint(), esc_*(), etc. # @TODO Check if we got an error and if so, send it # @example #1) ! wp_verify_nonce( $this->nonce, "{$this->name}_action" ) AND wp_send_json_error(); # @example #2) if ( ! $data['foo'] ) wp_send_json_error(); wp_send_json_success( array( #'foo' => 'bar', ) ); } }