Why does not ajax work in my WordPress plugin?

When I run the click, the server returns a 0.

I have already registered the JS file, and in the RUN function I have added the ajax add_action. I used https://wppb.me/ to generate the file structure.

file: odhkoolincludesclass-odhkool.php

        private function define_admin_hooks() 
$plugin_admin = new Odhkool_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' );
$plugin_basename = plugin_basename( plugin_dir_path( __DIR__ ) . $this->plugin_name . '.php' );
$this->loader->add_filter( 'plugin_action_links_' . $plugin_basename, $plugin_admin, 'add_action_links' );

public function run() {     
add_action( 'wp_ajax_check_in', array( $this, 'check_in' ) );      

file: odhkoolincludesclass-odhkool-admin.php

    function check_in(){
    $ret= $_POST['variable'];
    echo $ret;

public function enqueue_scripts() {
 wp_enqueue_script( 'odhkool-admin', plugin_dir_url( __FILE__ ) . 'js/odhkool-admin.js', array('jquery'), false, true );        

file: odhkoolincludesadminjsodhkool-admin.js

    $("#btntest2").click(function() {       
        type: "POST",
        url: 'http://'+window.location.hostname+'/wp-admin/admin-ajax.php',
        data: {
            'action': 'check_in',
            'variable': 45 
        success: function (response) {
            alert('Respuesta: ' + response);
        error: function(msg){

Read more here: Why does not ajax work in my WordPress plugin?

Leave a Reply

Your email address will not be published. Required fields are marked *