Calling Ajax From Plugin – Best Practices

I am learning how to create a plugin which using Ajax. I followed some guides and wrote codes. I really want to learn best practices from the get go so I thought about asking for opinions an suggestions here.

So if you have any suggestion for better way to write and improve this code I appreciate if you could share.

This is my current code:


defined( ‘ABSPATH’ ) or die( ‘No script kiddies please!’ );

function gear_guide_product_view() { ?>
<button type=”button” id=”next-product”><</button>
$file_path = plugins_url( ‘/assets/js/test.js’, __FILE__ );

wp_enqueue_script( gg_loadProductInfo, $file_path, array( ‘jquery’ ));
wp_localize_script( gg_loadProductInfo, gg_ajax_obj, array(
‘ajax_url’ => admin_url( ‘admin-ajax.php’ )));
add_shortcode( ‘gear-guide’ , ‘gear_guide_product_view’ );


function gg_loadProductInfo() {
jQuery(‘#next-product’).on(‘click’, function () {
console.log(“Should do Ajax call”);, {
action: “gg_get_product”,
title: this.value
}, function (data) {


Read more here:: Calling Ajax From Plugin – Best Practices

Leave a Reply

Your email address will not be published. Required fields are marked *