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