wordpressmeta-boxes

Creating a delete confirmation for images using the Wordpress meta box plugin


I am using the Meta Box plugin for Wordpress. I can successfully create fields in the cms for users to upload images. I would like to extend this in two ways:

First, I would like a delete confirmation when users remove an image from the image gallery

Here is the code:

$meta_boxes[] = array(
    'id' => 'project_media',                  
    'title' => 'Project Media',    
    'pages' => array( 'project' ),       
    'context' => 'normal',                
    'priority' => 'high',                

    'fields' => array( 
     array(
      'name' => 'Media Gallery',
      'desc' => 'Images should be sized to 983px x 661px',
      'id' => $prefix . 'project_media_gallery',
      'type' => 'image'
    )
);

This creates upload functionality in the custom post type where users can add images to a slideshow. The problem is if the user accidentally clicks the delete button, there is no confirmation to make sure it is deleted. Can I somehow extend the plugin through functions and call an alert when this button is clicked? Something that does not involve editing the WP core?

Second, the base functionality requires the user to upload an image from their local machine. Is there a way to tap into the Media Library for this?

No idea how to even start tackling this one.


Solution

  • To answer the first question

    First, I would like a delete confirmation when users remove an image from the image gallery

    You can do that by calling a custom script file from the functions.php.

    function alert_delete() {
        if(is_admin()){
            wp_register_script( 'alert_delete', get_bloginfo('template_url'). '/js/alert_delete.js', array('jquery'));
            wp_enqueue_script('alert_delete');
        }   
    }
    

    and create a file named alert_delete.js in the js directory of your theme.

    alert_delete.js:

    // admin delete check
    
    jQuery(document).ready(function(){
        jQuery(".rwmb-delete-file").click(function() {
            if (!confirm("Are you sure? This process cannot be undone.")){
                return false;
            }
        });
    });