Start Ajaxing

  • Ajax with ease in WordPress by adding custom code to add ajax in your theme functions or anywhere else desirable.
				
					function my_simple_ajax() {
    // Magic happens here.
    echo json_encode( 'Welcome to Tori Ajax' );
}

if ( function_exists( 'toria_add_ajax' ) ) {
    toria_add_ajax(
        'simple',
        'my_simple_ajax',
        get_stylesheet_directory_uri() . '/inc/my_custom_ajax/toria_ajax.js'
    );
}
				
			
  • Create a javascript file in the inc/my_custom_ajax/toria_ajax.js.
  • Add Javascript code. 
				
					function tori_ajax() {
    jQuery.ajax({
        type: "post",
        url: toria.ajax_url, // admin-ajax.php path,
        data: {
            action: toria.action, // action
            nonce: toria.nonce,   // pass the nonce here
        },
        success: function (data) {
            console.log(data.trim());
            //alert(data.trim());
        },
        error: function (errorThrown) {
            console.log(errorThrown);
        }
    });
}
tori_ajax();// call the function.
				
			

Quick Preview