Created
March 12, 2014 09:41
-
-
Save Clorith/9503789 to your computer and use it in GitHub Desktop.
Use the WordPress media modal to add gallery shortcodes to custom meta boxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function ($) { | |
// Set the original handler, since we override Core, we need to fall back to core if we're not doing stuff our selves | |
var _custom_media = true, | |
_orig_wp_media_insert = wp.media.editor.insert; | |
// Any input field wit hthe class 'theme_custom_media' will trigger our custom behavior | |
$(".theme_custom_media").click(function (e) { | |
var button = $(this); | |
var id = button.attr('id').replace('_button', ''); | |
_custom_media = true; | |
// We take control of the media managers 'insert' function (it only sends the html intended for the editor) | |
wp.media.editor.insert = function( html ) { | |
// If we are doign a custom media handling, do this | |
if ( _custom_media ) { | |
$("#" + id).val(html); | |
} else { | |
// If we arent' doing anythign custom, we return the original Core function and all is well | |
return _orig_wp_media_insert( this, [html] ); | |
} | |
}; | |
wp.media.editor.open(button); | |
return false; | |
}); | |
$(".add_media").on('click', function() { | |
_custom_media = false; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment