Widget creation in wordpress to upload media file
- by admin
- 0
Download script file and upload into your directory :
Add below code into fucntions.php file:
// class Files_Widget
// register Foo_Widget widget
function files_foo_widget() {
register_widget(‘Files_Widget’);
}
add_action(‘widgets_init’, ‘files_foo_widget’);
/**
* Adds Foo_Widget widget.
*/
class Files_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
// Add Widget scripts
add_action(‘admin_enqueue_scripts’, array($this, ‘jscripts’));
parent::__construct(
‘our_widget’, // Base ID
__(‘Slider Widget’, ‘text_domain’), // Name
array(‘description’ => __(‘Our Widget with media files’, ‘text_domain’),) // Args
);
}
public function widget($args, $instance) {
// Our variables from the widget settings
$title = apply_filters(‘widget_title’, empty($instance[‘titles’]) ? __(‘Default title’, ‘text_domain’) : $instance[‘title’] );
$image = !empty($instance[‘images’]) ? $instance[‘images’] : ”;
ob_start();
echo $args[‘before_widget’];
if (!empty($instance[‘titles’])) {
echo $args[‘before_title’] . $title . $args[‘after_title’];
}
?>
<?php if ($image): ?>
<img src=”<?php echo esc_url($image); ?>” alt=””>
<?php endif; ?>
<?php
echo $args[‘after_widget’];
ob_end_flush();
}
public function jscripts() {
wp_enqueue_script(‘media-upload’);
if (function_exists(‘wp_enqueue_media’)) {
wp_enqueue_media();
} else {
wp_enqueue_style(‘thickbox’);
wp_enqueue_script(‘media-upload’);
wp_enqueue_script(‘thickbox’);
}
wp_register_script(‘our_admin’, get_template_directory_uri().’/our_admin.js’);
wp_enqueue_script(‘our_admin’);
}
public function form($instance) {
$title = !empty($instance[‘titles’]) ? $instance[‘titles’] : __(‘New title’, ‘text_domain’);
$image = !empty($instance[‘images’]) ? $instance[‘images’] : ”;
?>
<p>
<label for=”<?php echo $this->get_field_id(‘titles’); ?>”><?php _e(‘Title:’); ?></label>
<input class=”widefat” id=”<?php echo $this->get_field_id(‘titles’); ?>” 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_id(‘images’); ?>”><?php _e(‘Image:’); ?></label>
<input class=”widefat” id=”<?php echo $this->get_field_id(‘images’); ?>” name=”<?php echo $this->get_field_name(‘images’); ?>” type=”text” value=”<?php echo esc_url($image); ?>” />
<img src=”<?php echo esc_url($image); ?>”>
<button class=”upload_image_button button button-primary”>Upload Image</button>
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance[‘titles’] = (!empty($new_instance[‘titles’]) ) ? strip_tags($new_instance[‘titles’]) : ”;
$instance[‘images’] = (!empty($new_instance[‘images’]) ) ? $new_instance[‘images’] : ”;
return $instance;
}
}
?>
Download script file and upload into your directory : script Add below code into fucntions.php file: // class Files_Widget // register Foo_Widget widget function files_foo_widget() { register_widget(‘Files_Widget’); } add_action(‘widgets_init’, ‘files_foo_widget’); /** * Adds Foo_Widget widget. */ class Files_Widget extends WP_Widget { /** * Register widget with WordPress. */ function __construct() { // Add Widget…
Download script file and upload into your directory : script Add below code into fucntions.php file: // class Files_Widget // register Foo_Widget widget function files_foo_widget() { register_widget(‘Files_Widget’); } add_action(‘widgets_init’, ‘files_foo_widget’); /** * Adds Foo_Widget widget. */ class Files_Widget extends WP_Widget { /** * Register widget with WordPress. */ function __construct() { // Add Widget…