I have created a simple widget which looks like this:
As there are going to be many instances of this I want to know if it's possible to update the title of each instance (where it says 'Membership Widget') to add the Company name that is entered in that field.
This is my code
<?php
class Membership_Widget extends WP_Widget
{
// Controller
public function __construct() {
$widget_ops = array('classname' => 'membership-widget', 'description' => __('Membership_Widget for IOMSA', 'wp_widget_plugin'));
$control_ops = array('width' => 400, 'height' => 300);
parent::WP_Widget(false, $name = __('Membership Widget', 'wp_widget_plugin'), $widget_ops, $control_ops );
}
public function widget( $args, $instance )
{
// basic output just for this example
echo '<p>'.esc_html($instance['cName']).'<p>
<a href="'.esc_html($instance['url']).'"><img src="'.esc_url($instance['image_uri']).'" /></a>
<a href="'.esc_html($instance['url']).'"><p>'.esc_html($instance['url']).'</p></a><hr class="red">';
}
public function form( $instance )
{
?>
<p>
<label for="<?php echo $this->get_field_id('cName'); ?>">Company Name</label><br />
<input type="text" name="<?php echo $this->get_field_name('cName'); ?>" id="<?php echo $this->get_field_id('cName'); ?>" value="<?php echo $instance['cName']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">URL</label><br />
<input type="text" name="<?php echo $this->get_field_name('url'); ?>" id="<?php echo $this->get_field_id('url'); ?>" value="<?php echo $instance['url']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('image_uri'); ?>">Image</label><br />
<input type="text" class="img" name="<?php echo $this->get_field_name('image_uri'); ?>" id="<?php echo $this->get_field_id('image_uri'); ?>" value="<?php echo $instance['image_uri']; ?>" />
<input type="button" class="select-img" value="Select Image" />
</p>
<?php
}
}
// end class
// init the widget
add_action( 'widgets_init', create_function('', 'return register_widget("Membership_Widget");') );
// queue up the necessary js
function ml_enqueue($hook)
{
if( $hook != 'widgets.php' )
return;
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
// moved the js to an external file, you may want to change the path
//wp_enqueue_script('ml', '/wp-content/plugins/home-rollover-widget/script.js', null, null, true);
}
add_action('admin_enqueue_scripts', 'ml_enqueue');
?>
<script type="text/javascript">
var image_field;
jQuery(function($){
$(document).on('click', 'input.select-img', function(evt){
image_field = $(this).siblings('.img');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(html) {
imgurl = $('img', html).attr('src');
image_field.val(imgurl);
tb_remove();
}
});
</script>