wordpress use jquery in plugin

Step 1: Enqueue jQuery in WordPress:

function enqueue_custom_scripts() {
    wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');

Step 2: Add a custom JavaScript file and localize jQuery:

function add_custom_script() {
    wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true);
    wp_localize_script('custom-script', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
}
add_action('wp_enqueue_scripts', 'add_custom_script');

Step 3: Create a custom JavaScript file (custom-script.js) and make AJAX calls:

jQuery(document).ready(function($) {
    // Example AJAX call
    $.ajax({
        url: ajax_object.ajax_url,
        type: 'POST',
        data: {
            action: 'custom_ajax_function',
            parameter: 'example_data'
        },
        success: function(response) {
            console.log(response);
        }
    });
});

Step 4: Handle AJAX requests in your plugin or theme functions file:

function custom_ajax_function() {
    $parameter = $_POST['parameter'];

    // Your processing logic here

    wp_die(); // Always include this to terminate the script properly
}
add_action('wp_ajax_custom_ajax_function', 'custom_ajax_function');
add_action('wp_ajax_nopriv_custom_ajax_function', 'custom_ajax_function'); // For non-logged in users