Add new pop-up after Deleting a listing

Hi everyone,

Do you know the pop-up that appears when clicking on the “Delete” button of a listing, the one saying “Are you sure you want to permanently delete this listing”?.

I am currently trying to create a similar pop-up that would appear after the listing is deleted (so preferably after the user clicks on “Yes” in the first, default pop-up), but I can’t target the right action to get it triggered. This is the code I tried so far as a test:

add_action('wp_footer', function() {
    ?>
    <script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function() {
            // Select the delete buttons inside listings
            document.querySelectorAll(".hp-listing__delete").forEach(function(button) {
                button.addEventListener("click", function(event) {
                    event.preventDefault(); // Stop the default delete action for testing
                    
                    alert("Delete button clicked!");

                    // Uncomment the next line in the final version to allow deletion
                    // this.closest("form").submit();
                });
            });
        });
    </script>
    <?php
});

However, it fails to display when a listing is delete, so I can only assume it’s not targeting the right thing. Any ideas what selector would be right in this case?

Also, if this doesn’t work, can you tell me if it would be possible to modify the default pop-up using the child-theme (so it’s resistant to updated)?

Thanks a lot!

Hi,

Unfortunately there’s no easy way to do this because there’s a redirect after the listing is deleted, with no URL parameters to detect the previous action. Please consider sending an email instead if you need to notify user about something after the deletion (this would be much easier, e.g. with the hivepress/v1/models/listing/update_status hook, without the JS code).

Hello ihor,

Thanks for the advice - you were right, that redirect makes it really hard to achieve.
However, after a few sleepless nights, I managed to pull it of, and now it works fine. I won’t copy the code in here as it’s heavily customized, but will shortly explain the logic in case anyone else needs it.

Basically, after a few failed attempts to anchor the previous state before the redirect, I managed to do it by saving the initial number of listings in a local variable and setting a flag variable when the “delete” button is pressed. Then, once the user gets redirected, the script remembers checks the flag variable, and if it’s set to “True” (so a delete happened), it re-counts the number of listings - if it’s less than they were previously (which means a listing was deleted), then it displays the pop-up and resets the flag.

Quite some work, but gets the job done.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.