• Resolved roryfunction28

    (@roryfunction28)


    Hi,

    I employed this hook to handle a different file, and it’s entirely stopped working. It’s supposed to grab the post_id, find that post_id in a .json file and delete it from there too. But it suddenly stopped working. Haven’t changed anything in functions.php

    I’ve stripped it back to just:

    
    add_action('before_delete_post', 'delete_handler');
    function delete_handler($postid){
        echo "<script>console.log('" . $postid ."')</script>";
        echo "<script>console.log('deleted')</script>";
        $txt = "Some text";
        $myfile = file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND);
    }

    Just trying to get something to happen, and nothing happens. It’s like it’s ignoring the entire hook.

    I’m seeing nothing added to the console, no errors there either, nothing added to logs.txt, and nothing added in my error log either.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Did you test your code while trashing a post or deleting permanently? “Trashing” a post is not really deleting it. Deleting permanently from the trash listing is deleting. For various reasons, none of your verification code may be 100% effective. Either the browser is not expecting output at the time or permissions prevent writing to logs.txt. For reliable verification, either use wp_die() or error_log().

    The action fires on my site when deleting permanently from the trash listing. I used your code, but none of your methods were effective for me. I added in an error_log() call as well. That one did work.

    Thread Starter roryfunction28

    (@roryfunction28)

    Hi,

    Thanks for your response.

    I was deleting permanently.

    However, I followed your advice of using wp_die() as my way of checking execution, and I actually started getting a response. Thanks very much for that suggestion. I used this to debug my issue. Issue turned out to be my side in the end. As I worked my way through it, it turned out to be stupidly simple: it wasn’t accessing the json file! It was supposed edit the one I’d set, but it was supposed to iterate over another one too, and it wasn’t doing that because… I didn’t tell it to!

    Wouldn’t have been able to get there without your wp_die() suggestion. I was trying to debug it to find the failure point by writing to console, but yeah, as you suggested, the browser wasn’t expecting output. My echo’d console.log didn’t fire until wp_die() was invoked, at which point using the console was redundant.

    I wasn’t aware of wp_die() before this. This is a far better way for me to test and debug, since it prevents the final execution, meaning I don’t have to reset the conditions (e.g. creating and deleting posts).

    Cheers!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! It’s almost always something stupid simple 🙂 But finding it can be far from simple. I find debugging PHP very challenging, especially background tasks. Before PHP, I had gotten used to IDEs where we can easily set break points, add watches, step through code, etc. Nowadays there is Xdebug, but when I started with PHP, what was available was very crude and I learned to do without.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘before_delete_post not working’ is closed to new replies.