Created
September 13, 2012 13:13
-
-
Save Kevinlearynet/3714200 to your computer and use it in GitHub Desktop.
These functions can be added to Matt Varone's widget helper class (https://github.com/sksmatt/WordPress-Widgets-Helper-Class) to add a Media Upload field to a WordPress widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Load Media Upload Scripts | |
* | |
* Load the JS & CSS needed for the media upload field | |
*/ | |
function enqueue_scripts() { | |
// Only load on widgets screen | |
$screen = get_current_screen(); | |
if ( $screen->id != 'widgets' ) | |
return; | |
// Register media upload JS file | |
$js_file = get_stylesheet_directory_uri() .'/js/media-upload.min.js'; | |
wp_register_script( 'wptuts-upload', $js_file, array('jquery','media-upload','thickbox') ); | |
// jQuery | |
wp_enqueue_script('jquery'); | |
// Thickbox | |
wp_enqueue_script('thickbox'); | |
wp_enqueue_style('thickbox'); | |
// Uploader scripts | |
wp_enqueue_script('media-upload'); | |
wp_enqueue_script('wptuts-upload'); | |
} | |
/** | |
* Media Upload Field | |
* | |
* Allows you to add a WordPress thickbox media upload popup field | |
* to a given widget | |
* | |
* @param $key array() Settings for the given field | |
* @param $out string Anything to display before the field | |
* @return $out HTML output to display the field | |
*/ | |
function create_field_media_upload( $key, $out = "" ) | |
{ | |
// Create label | |
$out .= $this->create_field_label( $key['name'], $key['_id'] ) . '<br/>'; | |
// Created input text field | |
$out .= '<input type="text" '; | |
// Add class | |
if ( isset( $key['class'] ) ) | |
$out .= 'class="' . esc_attr( $key['class'] ) . '" '; | |
// Add ID and value attributes | |
$value = isset( $key['value'] ) ? $key['value'] : $key['std']; | |
$out .= 'id="' . esc_attr( $key['_id'] ) . '" name="' . esc_attr( $key['_name'] ) . '" value="' . esc_url( $value ) . '" '; | |
// Input size | |
if ( isset( $key['size'] ) ) | |
$out .= 'size="' . esc_attr( $key['size'] ) . '" '; | |
$out .= 'style="display:inline-block; width:161px" />'; | |
// Upload button | |
$out .= ' <input id="btn-' . esc_attr( $key['_id'] ) . '" type="button" class="media-upload-button button" value="Upload" style="font-size:12px; display:inline-block;" />'; | |
// Description | |
if ( isset( $key['desc'] ) ) | |
$out .= '<br/><small class="description">'.esc_html( $key['desc'] ).'</small>'; | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These functions can be added to Matt Varone's widget helper class (https://github.com/sksmatt/WordPress-Widgets-Helper-Class) to add a Media Upload field to a WordPress widget