Skip to content

Instantly share code, notes, and snippets.

@rodica-andronache
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save rodica-andronache/0f8b502fdfb84009132f to your computer and use it in GitHub Desktop.

Select an option

Save rodica-andronache/0f8b502fdfb84009132f to your computer and use it in GitHub Desktop.
Custom size for files uploaded via media uploader ( for custom widgets)
add_image_size( 'zerif-our-focus-widget', 125, 125, true );
global $wpdb;
$our_focus_attachment_id = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", esc_url($instance['image_uri']) )); /* attachement id by attachement url */
$aa = wp_get_attachment_image_src( $attachment[0], 'zerif-our-focus-widget' );
echo $aa[0]; /* url-ul imaginii */
SAU varianta mai buna:
http://css.dzone.com/articles/add-upload-media-library
<?php
/*
* Plugin Name: Media Upload Widget
* Plugin URI: http://www.paulund.co.uk
* Description: A widget that allows you to upload media from a widget
* Version: 1.0
* Author: Paul Underwood
* Author URI: http://www.paulund.co.uk
* License: GPL2
Copyright 2012 Paul Underwood
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/**
* Register the Widget
*/
add_action( 'widgets_init', create_function( '', 'register_widget("pu_media_upload_widget");' ) );
class pu_media_upload_widget extends WP_Widget
{
/**
* Constructor
**/
public function __construct()
{
$widget_ops = array(
'classname' => 'pu_media_upload',
'description' => 'Widget that uses the built in Media library.'
);
parent::__construct( 'pu_media_upload', 'Media Upload Widget', $widget_ops );
add_action('admin_enqueue_scripts', array($this, 'upload_scripts'));
add_action('admin_enqueue_styles', array($this, 'upload_styles'));
}
/**
* Upload the Javascripts for the media uploader
*/
public function upload_scripts()
{
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('upload_media_widget', plugin_dir_url(__FILE__) . 'upload-media.js');
}
/**
* Add the styles for the upload media box
*/
public function upload_styles()
{
wp_enqueue_style('thickbox');
}
/**
* Outputs the HTML for this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
* @return void Echoes it's output
**/
public function widget( $args, $instance )
{
// Add any html to output the image in the $instance array
}
/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*
* @param array An array of new settings as submitted by the admin
* @param array An array of the previous settings
* @return array The validated and (if necessary) amended settings
**/
public function update( $new_instance, $old_instance ) {
// update logic goes here
$updated_instance = $new_instance;
return $updated_instance;
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*
* @param array An array of the current settings for this widget
* @return void
**/
public function form( $instance )
{
$title = __('Widget Image');
if(isset($instance['title']))
{
$title = $instance['title'];
}
$image = '';
if(isset($instance['image']))
{
$image = $instance['image'];
}
?>
<p>
<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_name( 'image' ); ?>"><?php _e( 'Image:' ); ?></label>
<input name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" class="widefat" type="text" size="36" value="<?php echo esc_url( $image ); ?>" />
<input class="upload_image_button" type="button" value="Upload Image" />
</p>
<?php
}
}
?>
upload-media.js:
jQuery(document).ready(function($) {
$(document).on("click", ".upload_image_button", function() {
jQuery.data(document.body, 'prevElement', $(this).prev());
window.send_to_editor = function(html) {
var imgurl = jQuery('img',html).attr('src');
var inputText = jQuery.data(document.body, 'prevElement');
if(inputText != undefined && inputText != '')
{
inputText.val(imgurl);
}
tb_remove();
};
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment