Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am writing a plugin for Wordpress and am wondering how to add an extra button to the media uploader that says something like "add to album". I can't seem to find info on how other developers have managed to get this done. Thanks :)

share|improve this question
add comment

3 Answers

When i needed to do something similar i used the admin_print_scripts-media-upload-popup hook to add my own js/Jquery code to insert a button and handled it's click event with ajax.

something like this:

add_action('admin_print_scripts-media-upload-popup','add_my_media_button');
function add_my_media_button(){
?>
<script>
jQuery(document).ready(function() {
    //loop over all attachments
    jQuery(".savesend").each(function(i,v){
        if (jQuery(this).next().attr('id') != ""){
            //get attachment id
            var att_id = jQuery(this).next().attr('id');
            att_id = att_id.replace("send[", "");
            att_id = att_id.replace("]", "");
            //insert button
            jQuery(this).append('<a href="#" class="button add_to_album" rel="'+ att_id +'" id="add_to_album['+att_id+']" name="add_to_album['+att_id+']">add to album</a>');
        }
    });

    //add handler for click
    jQuery(".add_to_album").live("click", function(){
        //do  your thing here
        alert("add to album click");
    )};
)};
</script>
<?php
}
share|improve this answer
    
Thanks, that works like a charm. Do you know any way to use this button to close the thickbox? tb_remove doesn't work (i guess it's not loaded in the iframe). The rest of the data is already saved by then (i'm using the uploader just to upload attachments). –  Gerben Van Dijk Dec 15 '11 at 12:16
    
I added the ajax functionality(the button click) handler from admin_footer at the page that calls the media uploader. –  Bainternet Dec 15 '11 at 12:20
    
You're talking about this code right? //add handler for click jQuery(".add_to_album").live("click", function(){ //do your thing here alert("add to album click"); )}; I can get this to work just fine, but I want to close the thickbox in this handler and can't get it to work. –  Gerben Van Dijk Dec 15 '11 at 12:30
    
yes that is what I'm talking about, this function i add using admin_footer hook on the page itself , before i even call the media uploader and if you use it there you will be able to use tb_remove(); –  Bainternet Dec 15 '11 at 12:35
    
Ah, thanks that makes sense. Works :) –  Gerben Van Dijk Dec 15 '11 at 12:37
add comment
up vote 1 down vote accepted

I managed to get it done this way:

Adding JS to the media uploader:

function mediabutton(){

    wp_register_script( 'mediabutton', ''.WP_PLUGIN_URL.'/magic-gallery/js/mediabutton.js', null, null);
    wp_enqueue_script( 'mediabutton');

}

add_action('admin_print_scripts-media-upload-popup','mediabutton'); // Adding insert button

And then using this JS:

jQuery(document).ready(function() {

    jQuery('<a href="#" id="insert_gallery" class="button">Return to gallery</a>').insertAfter('.ml-submit');   

    jQuery('#insert_gallery').live('click',function() {

        self.parent.tb_remove(); // This closes the thickbox

    });

});

Then I hooked into tb_close like this:

tb_remove = function () { 

    // Your code here
    alert('closing thickbox');

    // Original tb_remove code
    jQuery("#TB_imageOff").unbind("click");
    jQuery("#TB_closeWindowButton").unbind("click");
    jQuery("#TB_window").fadeOut("fast",function(){jQuery('#TB_window,#TB_overlay,#TB_HideSelect').trigger("tb_unload").unbind().remove();});
    jQuery("#TB_load").remove();

    if (typeof document.body.style.maxHeight == "undefined") {//if IE 6

        jQuery("body","html").css({height: "auto", width: "auto"});
        jQuery("html").css("overflow","");

    }

    jQuery(document).unbind('.thickbox');

    return false;
}
share|improve this answer
add comment

Or you can add a new tab, containing whatever you want .

<?php
function my_media_menu($tabs) {
$newtab = array('tabname' =&gt; __('TextInMenu', 'yourname'));
return array_merge($tabs, $newtab);
}
add_filter('media_upload_tabs', 'my_media_menu');
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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