Skip to content

Instantly share code, notes, and snippets.

@fitodac
Last active November 11, 2019 09:43
Show Gist options
  • Save fitodac/aca2be518381c1b5e92695468f4899eb to your computer and use it in GitHub Desktop.
Save fitodac/aca2be518381c1b5e92695468f4899eb to your computer and use it in GitHub Desktop.
Enqueue scripts
functions.php
function smpx_scripts(){
wp_enqueue_script( 'bolsacom', get_template_directory_uri().'/assets/js/bolsacom.js', array('jquery'), rand(), true );
// PRODUCT VALORATIONS
wp_localize_script('bolsacom', '$ajax_vars', array(
'url' => admin_url( 'admin-ajax.php' ),
'action' => 'store-product-ranking',
'nonce' => wp_create_nonce( 'my-ajax-nonce' )
) );
}
add_action( 'wp_enqueue_scripts', 'smpx_scripts' );
---------------------------------------------------------------
Function returned on ajax success
functions.php
function bolsa_store_product_ranking(){
echo 'What ever you need to return here';
}
add_action( 'wp_ajax_nopriv_store-product-ranking', 'bolsa_store_product_ranking' );
add_action( 'wp_ajax_store-product-ranking', 'bolsa_store_product_ranking' );
---------------------------------------------------------------
Ajax
YOUR_OWN_JAVASCRIPT_FILE.js
function productValoration ($groupID, $val) => {
jQuery.ajax({
type: "post",
url: $ajax_vars.url,
data: {
action: $ajax_vars.action,
nonce: $ajax_vars.nonce,
groupID: $groupID,
ranking: $val
},
success: function($result){
console.log($result);
}
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment