0

Can someone help me, I want to destroy my previous media uploder frame and remove all elements they have created to change the media file type but I cant do that, In last of my search, I have found in comments of media-editor.js and media-view.js from wordpress folder, they destroy the frame with dispose function, but after I copy that and follow it, the dispose do nothing. I think, dispose can run if some condition are met, but I dont know what is it, can someone help me? Thank you..

This is my code to make media uploder frame

var $j = jQuery.noConflict();
function call_media_library() {
    var media_uploader;
    $j(document).on('click', '.video_media_btn', function(e){
        e.preventDefault();
        var btn_uploader = $j(this);
        //change media type
        var med_type;
        if(btn_uploader.hasClass('mp4-media-btn')){
            media_type='video/mp4';
        }else if(btn_uploader.hasClass('webm-media-btn')){
            media_type='video/webm';
        }else if(btn_uploader.hasClass('ogg-media-btn')){
            media_type='video/ogv';
        }else{
            media_type='video';
        }
        //after I have call media uploader in twice the previous media_uploader cant destroy
        if (media_uploader) {
            //this is not working
            media_uploader.dispose();
        }
        media_uploader = wp.media({ 
            title: 'Upload media',
            library : { type : media_type},
            multiple: false
        }).on('select', function(e){
            // This will return the selected media from the Media Uploader, the result is an object
            var uploaded_media = media.state().get('selection').first();
            // Convert uploaded_media to a JSON object to make accessing it easier
            var media_url = uploaded_media.toJSON().url;
            // Assign the url value to the input field
            if(btn_uploader.hasClass('mp4-media-btn')){
                btn_uploader.parent().closest('.input-group.input-group-xs').siblings( '.input-media-display' ).find('.mp4-media-src').attr("src", media_url);
            }else if(btn_uploader.hasClass('webm-media-btn')){
                btn_uploader.parent().closest('.input-group.input-group-xs').siblings( '.input-media-display' ).find('.webm-media-src').attr("src", media_url);
            }else if(btn_uploader.hasClass('ogg-media-btn')){
                btn_uploader.parent().closest('.input-group.input-group-xs').siblings( '.input-media-display' ).find('.ogg-media-src').attr("src", media_url);
            }
            btn_uploader.parent().closest('.input-group.input-group-xs').siblings( '.input-media-display' ).find('video')[0].load();
        }).open();
    });
}

and this is my wp enqueue in function.php

add_action( 'wp_enqueue_scripts', 'insert_script_to_my_plugin' );
function insert_script_to_my_plugin(){
    wp_enqueue_media();
}

and this is code from wp-includes/js/media-editor.js

// Destroy the previous gallery frame.
                if ( this.frame ) {
                    this.frame.dispose();
                }
// Store the current frame.
                this.frame = wp.media({
                    frame:     'post',
                    state:     state,
                    title:     this.editTitle,
                    editing:   true,
                    multiple:  true,
                    selection: selection
                }).open();
2
  • I still waiting for answer, pls help me and thank you for ur help
    – Manyang
    Feb 27, 2017 at 1:51
  • You don't get an answer, because nobody can figure out the relation between your js code and what you are trying to enqueue. You probably need to take a look at how the wp_enqueue_scripts works. You are trying to call a PHP function wp_enqueue_media() in your wp_enqueue_scripts callback than enqueue a javascript file.
    – hcheung
    Feb 27, 2017 at 11:15

1 Answer 1

0

If frame is an instance of wp.media, frame.detach() does the job for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.