wordpress ajax call php function – How to send AJAX request in WordPress?

wordpress ajax call php function – You can use the admin_url( ‘admin-ajax.php’ ) function of WordPress to get this url. set up a PHP function to handle the AJAX request.

How to Make a WordPress AJAX Call – wordpress ajax call php function

Paste below source code in header.php or footer.php WordPress ajax call PHP function with parameters.

jQuery.ajax({
	type: “POST”,
	url: “<?php echo admin_url( ‘admin-ajax.php’ ); ?>”,
	data:{
		action: “my_action”,
		“date”: date,
		“month”: month,
		“year”: year
	},
}).success(function(result){
alert(result);
});


/* Paste below code in functions.php */

add_action( ‘wp_ajax_my_action’, ‘user_calling_make’ );
add_action( ‘wp_ajax_nopriv_my_action’, ‘user_calling_make’ );

function user_calling_make(){
	global $wpdb;
	$events = $wpdb->get_results(“SELECT ID, title FROM table_name”);
	$data = “<ul>”;
	foreach($events as $evt){
		$data .= “<li>”;
		$data .= ‘<a href="”#”" target="”_blank”" rel="noopener">’.$evt->title.'</a>’;
		$data .= “</li>”;
	}
	$data .= “</ul>”;
	echo $data; exit;
}

Don’t Miss : WordPress AJAX URL GET POST Call PHP Function

Ajax call in wordpress functions.php using jQuery

Client Action

jQuery(document).ready({
  var data = {
    'action': 'my_ajax_request',
    'post_type': 'POST',
    'name': 'Simple jQuery Example'
  };

  jQuery.post("", data, function(response) {
    console.log( response );
  }, 'json');
});

Server Action

  add_action( 'wp_ajax_my_ajax_request', 'user_calling_make' );
  add_action( 'wp_ajax_nopriv_my_ajax_request', 'user_calling_make' );
  function user_calling_make() {
    $name	= isset($_POST['name'])?trim($_POST['name']):"";
    $response	= array();
    $response['message']	= "Successfull Request";
    echo json_encode($response);
    exit;
  }

I hope you get an idea about wordpress ajax call php function.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.