Skip to content

Instantly share code, notes, and snippets.

@curtismchale
Created August 20, 2014 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curtismchale/4204523b9d7a533c5ceb to your computer and use it in GitHub Desktop.
Save curtismchale/4204523b9d7a533c5ceb to your computer and use it in GitHub Desktop.
Basic AJAX request set up
<?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' ),
) );
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