To update all the user meta fields you listed when the save button is clicked, you’ll need to ensure your form includes inputs for each meta field. Then, use AJAX to send all form data to the server and update the respective user meta fields. Here’s the updated implementation:
<form class="a2n-dash_grid" action="#" method="post">
<div class="form_group">
<h3 class="a2n_notification">
Receive Email notifications...
</h3>
<div class="label_of_notify">
<label>
<input type="radio" name="student_course_subscribed" value="yes" <?php echo $metaVal === 'yes' ? 'checked' : ''; ?>>
Yes
</label>
<label>
<input type="radio" name="student_course_subscribed" value="no" <?php echo $metaVal === 'no' ? 'checked' : ''; ?>>
No
</label>
</div>
</div>
<div class="form_group">
<h4>Course Start</h4>
<input type="checkbox" name="student_course_start" value="1" <?php checked($metaValues['student_course_start'], 1); ?>>
</div>
<div class="form_group">
<h4>Course Retake</h4>
<input type="checkbox" name="student_course_retake" value="1" <?php checked($metaValues['student_course_retake'], 1); ?>>
</div>
<!-- Add similar inputs for all the meta fields -->
<div class="form_submit">
<input type="submit" value="Save" />
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce('update_meta_nonce'); ?>">
</div>
</form>
Capture all form data dynamically and send it via AJAX.
jQuery(document).ready(function ($) {
$('.a2n-dash_grid').on('submit', function (e) {
e.preventDefault(); // Prevent the default form submission
// Serialize the form data into a key-value pair object
const formData = $(this).serialize();
$.ajax({
url: ajax_object.ajax_url, // AJAX URL
type: 'POST',
data: {
action: 'update_user_meta_bulk', // Action defined in PHP
data: formData, // Serialized form data
},
success: function (response) {
if (response.success) {
alert('Settings saved successfully!');
} else {
alert('Error: ' + response.data);
}
},
error: function () {
alert('An unexpected error occurred.');
},
});
});
});
In your functions.php
, process all the fields and update them.
function update_user_meta_bulk_ajax() {
// Check for nonce security
if (!isset($_POST['data']) || !isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'update_meta_nonce')) {
wp_send_json_error('Invalid security token.');
}
parse_str($_POST['data'], $formData); // Parse serialized form data
$user_id = get_current_user_id(); // Get current user ID
if (!$user_id) {
wp_send_json_error('User not logged in.');
}
// List of meta keys to update
$meta_keys = [
"student_course_subscribed",
"student_course_start",
"instructor_course_reset",
"student_course_retake",
"student_course_submit",
"student_course_reset",
"instructor_course_reset",
"instructor_course_retake",
"student_course_evaluation",
"instructor_course_evaluation",
"student_course_badge",
"student_course_certificate",
"student_course_review",
"student_course_unsubscribe",
"student_start_quiz",
"student_quiz_reset",
"instructor_quiz_reset",
"student_quiz_submit",
];
// Loop through the meta keys and update user meta
foreach ($meta_keys as $meta_key) {
if (isset($formData[$meta_key])) {
update_user_meta($user_id, $meta_key, sanitize_text_field($formData[$meta_key]));
} else {
delete_user_meta($user_id, $meta_key); // Optionally delete unchecked/absent values
}
}
wp_send_json_success('All user meta updated successfully.');
}
add_action('wp_ajax_update_user_meta_bulk', 'update_user_meta_bulk_ajax');