mardi 31 mai 2016

Integrating media-uploader in plugin wordpress

I'm trying to build a little plugin in wordpress: just a field and a button to upload photos. I have this main page (myplugin.php)

/////////////////////////

<?php

/*
Plugin Name: My Plugin
Version: 1.0
Description: description
Author: xxxxxxx
Author URI: xxxxxx
*/


function myplugin_admin_actions() {
add_options_page('myplugin', 'My Plugin', 'manage_options', __FILE__, 'myplugin_setup_page', get_stylesheet_directory_uri()."/images/plugin_icon.png");
}

add_action('admin_menu', 'myplugin_admin_actions'); 

function myplugin_scripts(){
    wp_enqueue_script('jquery');
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');  
    wp_register_script('uploader',plugin_dir_url( __FILE__ ).'js/uploader.js', array('jquery','media-upload','thickbox'));  
    wp_enqueue('uploader');     
}

add_action('wp_enqueue_scripts','myplugin_scripts');

function myplugin_setup_page()
{   
?>

<div class="wrap">
<h1>MEDIA UPLOADER PLUGIN</h1>
<h4>First plugin</h4>
<tr valign="top">
<td>Upload Image</td>
<td><label for="upload_image">
<input id="upload_image" type="text" size="36" name="upload_image" value="" />

<input id="upload_logo_button" class="button button-secondary" type="button"  value="Upload Image" />
<br />
Enter an URL or upload an image.
<br/>
</label>
<br/>
</td>
</tr>
</div>
 <?php } ?>

//////////////////////////

and this Javascript file (uploader.js):

//////////////////

jQuery(document).ready(function() {
jQuery('#upload_logo_button').click(function() {
tb_show('Upload a Image', 'media-upload.php?referer=media_page&type=image&TB_iframe=true&post_id=0', false);
return false;
});


window.send_to_editor = function(html) {
var image_url = jQuery('img', html).attr('src');
jQuery('#add_image').val(image_url);
tb_remove();
}

});

//////////////////////

No problems with plugin interface, but I tried to click the upload button without any result (no action, no window opened). I can't understand what's wrong...ok, I'm new with javascript, so I'm sorry in advanced, I think it's no so difficult, but I'm stuck here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire