Added Demo posts inserter
This commit is contained in:
33
assets/js/demo-posts-import.js
Normal file
33
assets/js/demo-posts-import.js
Normal file
@ -0,0 +1,33 @@
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
// Check if the button exists before adding the event listener
|
||||
const importButton = document.getElementById('import-demo-posts');
|
||||
if (importButton) {
|
||||
importButton.addEventListener('click', function () {
|
||||
const formData = new FormData();
|
||||
|
||||
// Append action and nonce to the form data
|
||||
formData.append('action', 'lcp_import_demo_posts');
|
||||
formData.append('lcp_import_nonce', lcp_ajax_obj.nonce); // Add the nonce passed by wp_localize_script
|
||||
|
||||
// Send the AJAX request
|
||||
fetch(lcp_ajax_obj.ajax_url, {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert(data.data); // Success message
|
||||
} else {
|
||||
alert('Error: ' + (data.data || 'Unknown error')); // Error message
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('An error occurred while processing your request.');
|
||||
});
|
||||
});
|
||||
} else {
|
||||
console.warn('Import button not found.');
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user