mindfullsilence
12/11/2013 - 3:02 AM

Create a widget that will display an image from the theme directory, optionally wrapped with a link. Also has the option of defining whether

Create a widget that will display an image from the theme directory, optionally wrapped with a link. Also has the option of defining whether or not to open said link in a new tab.

<?php
/*
Plugin Name: Theme Image widget
Description: Display an image from the theme directory in a widget. 
*/

// initialize the widget on install
add_action('widgets_init', create_function('', 'return register_widget("pm_image_upload_widget");'));

// extend the Widget class with the new widget
class pm_image_upload_widget extends WP_Widget
{

	// constructor
	function zz_image_upload_widget()
	{
		$desc = array('description' => __('Add an image from the template directory', 'text_domain'));
		parent::WP_Widget(
			false,
			$name = __('Theme Image', 'pm_image_upload_widget'),
			$widget_options = $desc
		);
	}

	// widget form creation
	function form($instance)
	{
		// Check if values exist
		if ($instance) {
			$fileName = esc_attr($instance['fileName']);
			$url = esc_attr($instance['url']);
			$newTab = esc_attr($instance['newTab']);
		} else {
			$fileName = '';
			$url = '';
			$newTab = '';
		}
		?>
		<p>
			<label for="<?php echo $this->get_field_id('fileName'); ?>">
				<?php _e('Image Location (relative to template directory):', 'pm_image_upload_widget'); ?>
			</label>
			<input
				class="widefat"
				id="<?php echo $this->get_field_id('fileName'); ?>"
				name="<?php echo $this->get_field_name('fileName'); ?>"
				type="text"
				value="<?php echo $fileName; ?>"/>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id('url'); ?>">
				<?php _e('Should this image link to anywhere?:', 'pm_image_upload_widget'); ?>
			</label>
			<input
				class="widefat"
				id="<?php echo $this->get_field_id('url'); ?>"
				name="<?php echo $this->get_field_name('url'); ?>"
				type="text"
				value="<?php echo $url; ?>"/>
		</p>

		<p>
			<input 
			  id="<?php echo $this->get_field_id('newTab'); ?>" 
			  name="<?php echo $this->get_field_name('newTab'); ?>" 
			  type="checkbox" 
			  value="1" 
			  <?php checked( '1', $newTab ); ?> 
			/>
			<label for="<?php echo $this->get_field_id('newTab'); ?>">
			  <?php _e('If linked, should it open in a new tab?', 'wp_widget_plugin'); ?>
			</label>
		</p>
	<?php
	}

	// update widget on save
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		// Fields
		$instance['fileName'] = strip_tags($new_instance['fileName']);
		$instance['url'] = strip_tags($new_instance['url']);
		$instance['newTab'] = strip_tags($new_instance['newTab']);
		return $instance;
	}

	// display widget
	function widget($args, $instance)
	{
		extract($args);
		// these are the widget options
		$fileName = $instance['fileName'];
		$url = $instance['url'];
		$newTab = $instance['newTab'];
		echo $before_widget;
		// Display the widget
		echo '<div class="image-upload-widget wp_widget_plugin_box">';

		// Check if text is set
		if ( $fileName!=='' && $url!=='') {
		  
			// Check if external link, otherwise link to site page. E.g "/blog" = "http://the-site.com/blog"
		  $is_url = preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $url);
			$real_link = ($is_url) ? $url : get_bloginfo('wpurl') . $url;
			
			// new tab? 
			$doNewTab = ($newTab && $newTab === '1') ? 'target="_blank"' : '';
		?>
			<a href="<?php echo $real_link; ?>" <?php echo $doNewTab; ?>>
				<img src="<?php echo get_template_directory_uri() . '/' . $fileName ?>"/>
			</a>
		<?php
		} else if ( $fileName!=='' && $url==='' ) {
			?>
			<img src="<?php echo get_template_directory_uri() . '/' . $fileName ?>"/>
		<?php
		}
		echo '</div>';
		echo $after_widget;
	}
}
?>