Created
August 20, 2014 20:59
-
-
Save curtismchale/4204523b9d7a533c5ceb to your computer and use it in GitHub Desktop.
Basic AJAX request set up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CT_Config_Ajax{ | |
function __construct(){ | |
add_action( 'wp_ajax_ct_get_sku', array( $this, 'get_sku' ) ); | |
add_action( 'wp_ajax_nopriv_ct_get_sku', array( $this, 'get_sku' ) ); | |
} // __construct | |
public function get_sku(){ | |
check_ajax_referer( 'ct_configurator_nonce', 'security' ); | |
wp_send_json_success( 'something' ); | |
} // get_sku | |
} // CT_Config_Ajax | |
new CT_Config_Ajax(); | |
wp_enqueue_script('script_slug', plugins_url( '/$plugin_folder/js/ct-configurator-frontend-scripts.min.js' ), array('jquery'), '1.0', true); | |
wp_localize_script( 'script_slug', 'CTConfig', array( | |
'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
'ct_configurator_nonce' => wp_create_nonce( 'ct_configurator_nonce' ), | |
) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = { | |
action: 'ct_get_sku', | |
product_id: selected_product_id, | |
security: CTConfig.ct_configurator_nonce | |
}; | |
$.post( CTConfig.ajaxurl, data, function( response ) { | |
console.log(response); | |
if ( response.success === true ){ | |
// deal with true | |
} | |
if ( response.success === false ){ | |
// deal with false | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment