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.

Is there a way to insert wp gallery shortcode into custom metabox textarea ? I would like to have something like this:

New metabox in post/page with textarea and below textarea there are a button to open wp browse media gallery lightbox. then when we have done to select few images as gallery, click the "insert gallery" button will insert the shortcode to the textarea custom field.

is it possible ?


====== Update: ======

I was able to show media library, now I don't have idea how to insert shortcode to the custom textarea when click button "insert gallery"

Here my code so far:


<div class="uploader">
 <textarea name="settings[_cs_shortcode_gallery]" id="_cs_shortcode_gallery"></textarea>
 <input class="button tf-browse-btn" name="_cs_shortcode_gallery_button" id="_cs_shortcode_gallery_button" value="Browse Gallery"/>


var _custom_media = true,
      _orig_send_attachment = wp.media.editor.send.attachment;
  $('.tf-browse-btn').live('click', function(e) {
    var send_attachment_bkp = wp.media.editor.send.attachment;
    var button = $(this);
    var id = button.attr('id').replace('_button', '');
    _custom_media = true;
    wp.media.editor.send.attachment = function(props, attachment){
      if ( _custom_media ) {
      } else {
        return _orig_send_attachment.apply( this, [props, attachment] );
    return false;
  $('.add_media').on('click', function(){
    _custom_media = false;

What I want to achieve now are:

  • How to set Create Gallery Tab as default tab when open the modal
  • Insert gallery shortcode to the custom textarea

Please see the screenshot below for details



share|improve this question
Much probably, yes, it is possible. What have you tried? What code you already have? –  brasofilo Jan 27 '13 at 19:44
I just done made the metabox, textarea field and the button. I need to know how to open the wp media library using that custom button and can return output shortcode to the textarea. any ideas to achieve it ? it should work like we use media library button and the shortcode gallery inserted into wp main editor –  resa.r Jan 27 '13 at 19:55
add comment

1 Answer

I'm sure it's possible, but will require some work with both PHP and jQuery. This plugin by Thomas Griffin to insert an image into a meta box would be a very good starting point.

You'll need to do some digging around in the js files in wp-admin/js and in wp-admin/includes/media.php.

share|improve this answer
thanks. I'll look on that plugin. –  resa.r Jan 27 '13 at 22:08
I've update the question. any idea what's next ? I found it very hard to insert gallery shortcode to the textarea –  resa.r Jan 29 '13 at 6:25
i've just come across the advanced custom fields plugin which allows galleries to be inserted via the 3.5 media uploader (support.advancedcustomfields.com/discussion/4486/…), perhaps that could be useful? –  Simon Blackbourn Jan 29 '13 at 20:00
add comment

Your Answer


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.