I have created a preview button, I use JS to create this button. But I have issue when I click on the button, it says that :
admin.php?page=wooster-partner:1331
GET http://wordpressoceane.local/wp-admin/get_preview_link.php?post_id=11130 404 (Not Found)
admin.php?page=wooster-partner:1331
GET http://wordpressoceane.local/wp-admin/get_preview_link.php?post_id=11131 404 (Not Found)
For each button , I have the Id of the page created, so it seems to well retrieve the id of the page when I click on the preview button. But it will not redirect to the page.
First I created the ajax to create a row and add the element
const actionsCell = document.createElement('td');
actionsCell.classList.add('actions'); // Add a class for styling
const viewButton = document.createElement('button');
viewButton.classList.add('viewBtn');
viewButton.innerHTML = '<i class="fas fa-eye fa-lg"></i>';
viewButton.addEventListener('click', () => {
// Assuming data.id contains your post ID
const postId = data.id; // Replace data.id with your actual post ID variable
// Make an AJAX request to get the preview link
fetch('get_preview_link.php?post_id=' + postId)
.then(response => response.text())
.then(previewUrl => {
// Redirect to the preview page of the post
window.location.href = previewUrl;
});
});
Then in the back , I added a function to retrieve the id
function get_preview_link_callback() {
// Check if the post ID is set in the AJAX request
if (isset($_GET['post_id'])) {
// Get the post ID from the AJAX request
$post_id = $_GET['post_id'];
// Get the preview link for the post
$preview_url = get_preview_post_link($post_id);
// Send the preview URL as the AJAX response
echo $preview_url;
}
// Always remember to terminate the script
wp_die();
}