Use Media Uploader in Plugin

Try using this code:

define("ME_URL", rtrim(WP_PLUGIN_URL,"https://wordpress.stackexchange.com/") . "https://wordpress.stackexchange.com/" . basename(dirname(__FILE__)));
define("ME_DIR", rtrim(dirname(__FILE__), "https://wordpress.stackexchange.com/"));
    
function my_admin_scripts() {
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
}
    
function my_admin_styles() {
    wp_enqueue_style('thickbox');
}
    
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
<td>
    <img src="<?php echo esc_attr( get_the_author_meta( 'profileimage', $user->ID ) ); ?>" id="ppimage">
    <input type="text" name="profileimage" id="profileimagetxt" value="<?php echo esc_attr( get_the_author_meta( 'profileimage', $user->ID ) ); ?>" class="regular-text" /><br />
    <input type="button" id="profileimage" value="Upload Image">
    <span class="description"><?php _e("profileimage"); ?></span>
</td>
</tr>
<script type="text/javascript">
    $ = jQuery.noConflict();
    $(document).ready(function($){
      $('#profileimage').live('click',function() {
        formfield = $('#upload_image').attr('name');
        tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
        return false;
      });
    
      window.send_to_editor = function(html) {
        imgurl = $('img',html).attr('src');
        $('#profileimagetxt').val(imgurl);
        $('#ppimage').attr('src',imgurl);
        tb_remove();
      }
    });
</script>

Hope it works for you 🙂

Leave a Comment