0

Eight months ago I asked nearly the same question over there. But the Problem is still not solved so far.

But I have an additional ideaa why it is failing. In the meantime my code advanced. I have multiple classes which all have the same structure:

<?php
class McCustomPost { ... }
$wpMcCustomPost = new McCustomPost();
?>
...
<?php
class McAdmin { ... }
$wpMcAdmin = new McAdmin();
?>

and then I have my "starter" file which requires all files and has the plugin header:

<?php
/*
Plugin Name: MC 2.0
Plugin URI: ...
Description: ...
Version: 0.0.75
Author: ...
Author URI: ...
License: GPL2
*/

define( 'MC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

require_once( MC_PLUGIN_DIR . 'class.mc.php'               );
require_once( MC_PLUGIN_DIR . 'class.mc-admin.php'          );
...
require_once( MC_PLUGIN_DIR . 'class.mc-custom-post.php' );

register_activation_hook( __FILE__, array( 'Mc', 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( 'Mc', 'plugin_deactivation' ) );

$wpMc = new Mc();
?>

During the require_once the admin and custom post classes getting properly instantiated. But when I fire an ajax request from the admin class the request is newer returned. It reaches the JS file but after that the callback is not called, I guess it's not found.

<?php
class McAdmin {
    protected $pluginPath;
    private $dbHandler;  

    public function __construct()  {
        $this->pluginPath = dirname(__FILE__);

        add_action( 'admin_enqueue_scripts', array( $this,'add_admin_scripts') ); 
        add_action( 'wp_ajax_blubb', array( $this, 'add_vehicle' ) );  
    }

    public function add_admin_scripts( ){
        wp_enqueue_script( 'admin_scripts', plugins_url( 'js/functions.admin.js', __FILE__ ),   array('jquery') );

        // wp_localize_script('admin_scripts', 'ajax_var', array(  
        // 'nonce' => wp_create_nonce( 'ajax-nonce' )  
        // ) );
    }

    public function add_vehicle()  
    {  
        // $nonce = $_POST['nonce'];  

        // if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )  
        //     die ( 'Busted!');

        $title = $_POST['id'];
        wp_die("Post Id: " . $title);
        exit;  
    }
...
}
$wpMcAdmin = new McAdmin();
?>

My JS file - the success callback is not executed...

$('.add-vehicle').click(function(){
        var data = {
            action: "blubb",
            // nonce: ajax_var.nonce,
            id: 1
        };

        $.post(
            ajaxurl,
            data,
            function( response ) { //on success
                alert('Server response from the AJAX URL ' + response);
        });
});

I assume, that during the institution of McAdmin() class inside Mc() the callback don't know where to go back. How can I get my callback in the correct class instance (McAdmin())?

Thanks & BR,

mybecks

2 Answers 2

1

Your McAdmin seems fine - the problem is likely one of two reasons:

  1. Another AJAX hook for blubb is terminating the script before McAdmin::get_vehicle
  2. You need to add the same hook for wp_ajax_nopriv_blubb - for non-logged in AJAX requests (otherwise it fails silently).

If neither is the cause, you need to do a little HTTP inspecting - I use Firebug or HttpFox - and debug the server response for the AJAX request.

1
  • tried your hints and the http inspecting drove me insane. The data always gets properly transfered. I often got the result of 0 that the callback function could not be found. At the end it was as I described it in my answer below this post. Nevertheless thank you for your help
    – mybecks
    Commented Aug 28, 2014 at 12:16
0

I finally solved it, my problem was that I forget to send back the results to JS file. I did it now with:

public function add_vehicle()
{
        ....
        $response = json_encode( $POST ); //as sample

        // response output -> sent back to javascript file
        header( "Content-Type: application/json" );
        echo $response;

        die();
}

Also I have forgotten the die(); at the end of the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.