setting up ajax data as php class variable in wordpress
-
ajax part:
$.ajax( { method : 'POST', dataType : 'json', url : my_var.ajaxurl, data : { foo : foobar, _wpnonce : my_var.nonce, action : 'my_php_ajax_function' } } ) .done( function( data ){ console.log(data); } );
WordPress part:
add_action( 'wp_ajax_nopriv_my_php_ajax_function', 'my_php_ajax_function' ); add_action( 'wp_ajax_my_php_ajax_function', 'my_php_ajax_function' ); add_action('wp_enqueue_scripts', 'my_enqueue2'); function my_enqueue2($hook) { wp_enqueue_script( 'ajax-script', plugins_url( '/js/my-jquery.js', __FILE__ ), array('jquery'), false, true ); $rest_nonce = wp_create_nonce( 'wp_rest' ); wp_localize_script( 'ajax-script', 'my_var', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => $rest_nonce, )); }
function my_php_ajax_function(){ if ( wp_verify_nonce( $_POST['_wpnonce'], 'wp_rest' ) ){ echo json_encode( array( 'youSent' => $_POST['foo'] ) ); exit; } else { echo 'nonce check failed'; exit; } }
How can i set up ajax sending data as variable to the another function.. such as
$this->yousend = $_POST[‘foo’] // ajax sending data$this->yousend not working or setting up as null value..
Viewing 15 replies - 1 through 15 (of 15 total)
Viewing 15 replies - 1 through 15 (of 15 total)
- The topic ‘setting up ajax data as php class variable in wordpress’ is closed to new replies.