我创建了一个简单的小部件,如下所示:
由于将有许多这样的实例,我想知道是否有可能更新每个实例的标题(其中显示“会员小部件”),以添加在该字段中输入的公司名称。
这是我的密码
<?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>