Support » Plugin: Media Library Assistant » delete option on front end?

  • Resolved shoopiable

    (@shoopiable)


    Hi there, this plugin is superb, I am really loving the behaviour of [mla_gallery post_mime_type=all] to show my files on my website.

    I am however, wondering if there is a way to provide a delete file option on the front end (so anybody who has this user permission on that file, could delete it)? this would then make the whole gallery a really easy to use tool to showcase files on a webpage

    https://wordpress.org/plugins/media-library-assistant/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for the good words and for your question.

    Adding functions to the “front end” for file manipulations like deleting a file is something I think is outside the scope of MLA. As you imply when you say “anybody who has this user permission“, adding this kind of function means managing logged-in users and so forth. There are other plugins such as BuddyPress and bbPress that support that sort of thing and you might find something like that to be of interest.

    If not, the best way to handle this would be to develop a custom page on your site for actions such as file deletion. It would require a WordPress template file with some PHP code. I could work with you to create an example that you could adapt to your site, if that’s something worth your while.

    If you’d like to pursue that, let me know. I will leave this topic unresolved until I hear back from you. Thanks for your understanding and your interest in the plugin.

    Thread Starter shoopiable

    (@shoopiable)

    Wow David, that is extremely kind of you to offer to help build such a tool.

    But basically my site is a login only site, so nobody can be viewing or uploading media to the site unless they are registered users. And I am not wanting end users to be able to access the wp-admin side of wordpress.

    What I would love to be able to do is have it so that Administrators, as well as Subscribers who uploaded media, can delete that media on the front end. It could be a simple matter of having a list of files – then a delete hyperlink next to the ones they are allowed to delete (for administrators, this is all of them, for subscribers, it is just those that they uploaded themself.

    Maybe there is already a plugin that does this which I am not aware of though, what do you think?

    Again, really appreciate your work on this stuff, your plugin is really comprehensive

    Plugin Author David Lingren

    (@dglingren)

    After a bit of thought I realized that you could use the “MLA Gallery Filters and Actions (Hooks)” to accomplish your goal, without the need to create a separate WordPress template file.
    I have added the code to the simple, stand-alone example plugin: mla-hooks-example.php.txt. You can find instructions for using the example plugin in the Settings/Media Library Assistant Documentation tab.

    With the code added and the example plugin installed and activated you simply need to create a page with an [mla_gallery], such as:

    [mla_gallery ids="2012,2017,2018" my_filter="allow file deletion"]

    You can substitute any item selection parameters for the ids="2012,2017,2018" parameter I used in my testing. You can add any other parameters you need, as long as you include the my_filter="allow file deletion" parameter to trigger the file deletion code.

    When you go to the page you will see a gallery of thumbnails. The caption below each item will have the base file name. Below that will be a “Delete this file” link for the items the user is allowed to delete.

    If you click on one of the links, the page will refresh. A status message will appear above the gallery with the results of the deletion request, and the remaining items will be re-displayed in the gallery.

    Three of the MLA Gallery hooks are required to implement the delete function. I will present them in their logical order; they appear I a different order in the plugin source file. I have removed all of the unnecessary code in the versions shown here; the actual plugin has some additional code for other hook examples.

    First, the mla_gallery_item_values filter is used to add the link(s) to the relevant items:

    public static function mla_gallery_item_values_filter( $item_values ) {
        /*
         * We use a shortcode parameter of our own to apply our filters on a gallery-by-gallery basis,
         * leaving other [mla_gallery] instances untouched. If the "my_filter" parameter is not present,
         * we have nothing to do.
         */
        if ( ! isset( self::$shortcode_attributes['my_filter'] ) ) {
            return $item_values; // leave them unchanged
        }
    
        /*
         * For the fourth example, we compose a URL to allow file deletion and add it to the caption.
         */
        if ( 'allow file deletion' == self::$shortcode_attributes['my_filter'] ) {
            $id = (integer) $item_values['attachment_ID'];
            if ( current_user_can( 'delete_post', $id ) ) {
                // Compose a new caption, adding the deletion link.
                $mla_link_href = "{$item_values['page_url']}?attachment_ID={$id}";
                $item_values['caption'] = sprintf( '%1$s<br><a href="%2$s" title="Click to delete">Delete this file</a>', $item_values['base_file'], $mla_link_href );
            } else {
                $item_values['caption'] = sprintf( '%1$s', $item_values['base_file'] );
            }
        }
    
        return $item_values;
    } // mla_gallery_item_values_filter
    • The “allow file deletion” test sets things in motion.
    • The current_user_can() function ensures that Subscribers and Authors can only delete files they own.
    • The $mla_link_href = "{$item_values['page_url']}?attachment_ID={$id}"; code builds a URL that returns to the current page with an additional query argument containing the ID of the item to be deleted.
    • The $item_values['caption'] variable is used in the default MLA Gallery markup template.

    Second, the mla_gallery_raw_attributes filter does the actual work when the page is re-displayed:

    public static function mla_gallery_raw_attributes_filter( $shortcode_attributes ) {
        /*
         * For this example, we delete the selected file.
         */
        if ( isset( $shortcode_attributes['my_filter'] ) && 'allow file deletion' == $shortcode_attributes['my_filter'] ) {
            if ( isset( $_REQUEST['attachment_ID'] ) ) {
                $id = (integer) $_REQUEST['attachment_ID'];
                if ( current_user_can( 'delete_post', $id ) ) {
                    $result = wp_delete_attachment( $id );
                } else {
                    $result = false;
                }
    
                if ( ( false === $result ) || ( NULL === $result ) ) {
                    $shortcode_attributes['gallery_open_message'] = "Could not delete attachment_ID '{$id}'.";
                } else {
                    $result = (array) $result; // Some wp_delete_attachment calls return an object
                    $shortcode_attributes['gallery_open_message'] = "Attachment '{$result['post_title']}' (ID {$id}) has been deleted.";
                }
    
                unset( $_REQUEST['attachment_ID'] );
            }
        }
    
        return $shortcode_attributes;
    } // mla_gallery_raw_attributes_filter
    • The code runs when my_filter is set and contains “allow file deletion”, and the attachment_ID query argument is present.
    • Another current_user_can() check ensures that the deletion is authorized, because anyone can add the URL query argument.
    • wp_delete_attachment() does the dirty work.
    • The “gallery_open_message” parameter is added to the $shortcode_attributes list to communicate the results of the operation.

    Finally, the mla_gallery_open_template filter gives us a convenient place to report the results.

    public static function mla_gallery_open_template_filter( $open_template ) {
        /*
         * Check for a display message
         */
        if ( isset( self::$shortcode_attributes['gallery_open_message'] ) ) {
            $open_template = '<p><strong>' . self::$shortcode_attributes['gallery_open_message'] . '</strong></p>' . $open_template;
        }
    
        return $open_template;
    } // mla_gallery_open_template_filter

    If there’s a message to display it gets pre-pended to the gallery display < DIV >. That’s it; we’re done.

    You can provide an extra measure of safety by activating the WordPress “Media Trash” feature, which works like “Trash” for posts and pages. Just add this to your wp-config.php file:

    /**
     * Enable the Trash feature for attachments.
     *
     * Set this to true to enable the Trash
     * Set this to false (the default) to disable the Trash
     */
    define ('MEDIA_TRASH', true);

    I will add this code to the example plugin in my next MLA version. If you would like a complete copy of the example plugin with the added code before the next version goes out, let me know. You can give me your e-mail address and other contact information by visiting the Contact Us page at our web site:

    Fair Trade Judaica/Contact Us

    I am marking this topic resolved, but please update it if you have any problems or further questions on the approach I’ve outlined. Thanks for an interesting question and for your interest in the plugin.

    Thread Starter shoopiable

    (@shoopiable)

    oh my goodness, you are quite extraordinary, thank you so much david for your efforts with this. I am looking forward to trying it out, so will try to contact you to get a hold of the example plugin to test it out for you.

    will also be making a donation, far beyond the support I was expecting 🙂

    Plugin Author David Lingren

    (@dglingren)

    Thanks for following up with your contact information, and especially for your donation in support of our work!

    I have sent you a copy of the complete example plugin that includes the file deletion code I posted above.

    Please let me know if you have any problems or further questions on the topic. Thanks again for you interest and for inspiring a new example that other people may find useful.

    Thread Starter shoopiable

    (@shoopiable)

    no additional questions, brilliantly done, your plugin basically can be used nearly like a front end media manager, that is quite extraordinary.

    now to figure out how a user role of subscriber can allow delete that media that they own.

    also if you felt useful, having an option to ask “Are you sure you want to delete the file?” with an OK or cancel option might be handy.

    But either way, well done and I am extremely grateful for your efforts.

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update and the positive feedback. I agree that this basic approach can be used for a wide variety of media management purposes, which is why I like working out solutions to questions like yours.

    If you look through the WordPress Codex article on Roles and Capabilities you can see that Subscribers and Contributors do not have the “upload_files” capability, so how can they “own” a media item?

    Of course, you can add code to compare the item author to the current user and allow a delete if they match, or any other rules you’d like to enforce.

    The “are you sure?” confirmation step could be handled by introducing another, intermediate query argument and then using it to construct a final URL for the actual deletion. You would put the intermediate code in the mla_gallery_open_template filter in place of the results message. Anything more sophisticated would require some PHP/JavaScript code beyond the scope of the filters-based approach. If you get a solution you like, post it here for everyone to enjoy.

    Good luck with your application!

    Thread Starter shoopiable

    (@shoopiable)

    Yeah, since I am creating a site which is a login only site, and users sign up with subscriber roles, I decided to provide subscriber level users the ability to upload_files themselves, so then they become the “Author” of that uploaded file. Those are the only files that they should be able to delete, but don’t see that is possible with the current Roles and capabilities unfortunately. You seem to be able to just be able to delete all, or delete none. yeah, perhaps matching an item author to the file is the way to go, will see if I can figure that out.

    Will also see if I can get have a go at the “are you sure” confirmation as well.

    thanks so much for your help on this one

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘delete option on front end?’ is closed to new replies.