I've added a button to tinyMCE's toolbar to launch a popup. The button calls editor.windowManager.open(...)
and passes an argument of this sort as the URL:
/wp-admin/media-upload.php?post_id=SOME_POST_ID&type=z_recipe&tab=amd_zlrecipe&TB_iframe=true
In my main plugin file, I have this code that generates some iframe code when the above URL is called:
if (strpos($_SERVER['REQUEST_URI'], 'media-upload.php') && strpos($_SERVER['REQUEST_URI'], '&type=z_recipe') && !strpos($_SERVER['REQUEST_URI'], '&wrt='))
{
ZipRecipes::zrdn_iframe_content($_POST, $_REQUEST);
exit;
}
I found a related answer about media uploads which claimed that the following should work:
<!DOCTYPE html>
<!--suppress HtmlUnknownTarget -->
<head>
<?php
function load_custom_code() {
// jQuery
wp_enqueue_script('jquery');
// This will enqueue the Media Uploader script
wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'load_custom_code' );
?>
</head>
<body id="amd-zlrecipe-uploader">
<label for="image_url">Image</label>
<input type="text" name="image_url" id="image_url" class="regular-text">
<input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image">
<script type="text/javascript">
jQuery(document).ready(function($){
$('#upload-btn').click(function(e) {
e.preventDefault();
var image = wp.media({
title: 'Upload Image',
// mutiple: true if you want to upload multiple files at once
multiple: false
}).open()
.on('select', function(e){
// This will return the selected image from the Media Uploader, the result is an object
var uploaded_image = image.state().get('selection').first();
// We convert uploaded_image to a JSON object to make accessing it easier
// Output to the console uploaded_image
console.log(uploaded_image);
var image_url = uploaded_image.toJSON().url;
// Let's assign the url value to the input field
$('#image_url').val(image_url);
});
});
});
</script>
</body>
However, the browser dev tools tell me that jQuery
and $
are not defined.
So, this could be solved either by fixing the way jQuery and the wp_enqueue_media
is included or by changing the way my initial popup is created.
Any help you got, I can use.