I’m creating a custom shortcode in WordPress and I’d like to allow the user select or upload some media using the media modal window as part of the process.
What is the best method to invoke the add media modal?
function CheesyEmbed_func($atts){
$atts = shortcode_atts(array(
'type' => '',
'source' => ''
), $atts);
if($atts['type'] == 'youtube'){
$output = "<style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='embed-container'><iframe src='http://www.youtube.com/embed/" . $atts['source'] . "?modestbranding=1&rel=0&showinfo=0&color=white&iv_load_policy=3' frameborder='0' allowfullscreen></iframe></div>";
return $output;
}
if($atts['type'] == 'image'){
$output = "<img src='" . $atts['source'] . "' class='img-responsive'>";
return $output;
}
}
add_shortcode('CheesyEmbed','CheesyEmbed_func');
If you are using Bootstrap the above code should work. If not you can use other methods to fire modal window, and edit the html by using same logic.
You can Add Upload button with type attribute like so:
You can handle user’s selection with jquery like this:
Hope that helps. If not feel free to ask!