WordPress.org

Ready to get started?Download WordPress

Forums

Black Studio TinyMCE Widget
Delete Media With Widget (2 posts)

  1. Howdy_McGee
    Member
    Posted 11 months ago #

    So I have this nifty little function that deletes media when a post gets deleted:

    /** Delete Media Attahced to Post **/
    function delete_associated_media($id) {
        $media = get_children(array(
            'post_parent' => $id,
            'post_type' => 'attachment'
        ));
    
        if (empty($media)) return;
    
        foreach ($media as $file) {
    		wp_delete_attachment($file->ID);
        }
    }
    add_action('before_delete_post', 'delete_associated_media');

    I was wondering if I could also hooking into something before the widget gets deleted so I can then get the associated media and delete it also.

    http://wordpress.org/plugins/black-studio-tinymce-widget/

  2. Marco Chiesi
    Member
    Plugin Author

    Posted 10 months ago #

    Hello Howdy_McGee,
    unfortunately I think it would be hard to get the behavior you want. This is because of the lack of relationships between the media and the widgets where they were originally uploaded. For post/pages this relationship is saved in the database using the post_parent field, and this allows to get all the media associated with a particular post/page in a simple way. On the other hand, media uploaded through a widget do not have a parent associated, so they're not distinguishable from media uploaded straightly from the media library.
    The only solution I can think of, is to analyze the (being deleted) widget content and search for the presence of media files, then search for them using their URL and finally delete them.
    By the way, it looks like actually there's not a specific hook for widgets being deleted, but there's a workaround for this (see article).

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.