• Hi,
    I’m developing a custom gallery which uses an XML DB for the purpose.
    Now I would like to synchronize the XML with the Media Library: when deleting an image from the Media Library, the ID of the image stored in the XML file will be deleted too, so:

    function onWpDelete( $targetID ){
    
        $filePath = '../wp-content/plugins/the-plugin/gallery.xml';
        if( file_exists( $filePath ) ){
            $doc = new DOMDocument();
            $doc->load($filename);
            $doc->preserveWhiteSpace = false;
            $doc->formatOutput = true;
    
            $ids = $doc->getElementsByTagName('ID');
            foreach( $ids as $id ){
                if( $id->nodeValue == $targetID ){
    
                    try{
                        $id->parentNode->removeChild($id);
                    } catch(Exception $e){
                        wp_die('DOMDocument: ' . $e->getmessage() );
                    }//try - catch
    
                    break;
                }//if
            }//foreach
        }//if
    }//onWpDelete
    add_action('delete_attachment', 'onWpDelete');

    but I can’t get the delete_attachment hook fired when I delete an image from the Media Library and can’t make it working.
    I even tried:

    add_action('delete_attachment', 'onWpDelete', 1', 1);

    because of the $postid parameter for the hook but it doesn’t work.

    What’s the correct way to do that?

    Thank you

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    add_action('delete_attachment', 'onWpDelete'); is the correct way. I found on my test site this fires for every way one can delete an image from within WP, every time without fail.

    If it does not fire on your site, I would guess either there is a flaw in your callback so you do not see the desired results, or there is a conflict with another plugin or with your theme which prevents it from working.

    Try inserting wp_die('There is a callback fault'); in your code, then delete an image. If you do not see the message then there is a plugin or theme conflict.

Viewing 1 replies (of 1 total)

The topic ‘delete_attachment isn't fired’ is closed to new replies.