add ajax url in wordpress

function dbk_enqueue_scripts() {

wp_enqueue_script( ‘debugger_custom_js’, plugins_url( ‘js/debugger-kanha.js’, __FILE__ ), array() );

//load jquery
wp_enqueue_script( ‘jquery’ );

//add ajax url
wp_localize_script( ‘debugger_custom_js’, ‘dbk’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ )));
}
add_action( ‘admin_enqueue_scripts’, ‘dbk_enqueue_scripts’ );

//for ajax call
add_action( ‘wp_ajax_dbk_ajax’, ‘dbk_ajax_subscribe’ );

output function

function dbk_ajax_subscribe(){
echo ‘this is ajax subscribe form’;
}

<script>
jQuery(document).ready(function() {

// var postDatas = {
// ‘pname’ : “kanha”,
// ‘lname’ : “swain”,
// ‘action’ : ‘dbk_ajax’
// };

jQuery(‘#mailchimp_form’).click(function() {
var mailchimpform = jQuery(‘#mailchimp’).serialize();
mailchimpform = mailchimpform+“&action=dbk_ajax”;

jQuery.ajax({
url: dbk.ajaxurl,
type: ‘POST’,
data: mailchimpform,
success: function(data) {
alert(data);
}
});
return false;
});
});

</script>

Leave a Comment

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