我正试着开始写带有选项的自定义小部件。我一直在学习一些教程,并一直在努力理解。
我有一个小部件,它在小部件管理器中工作得很好,但在前端Undefined variable: image_path
. 我无法追查我做错了什么,我肯定会感激你的另一双眼睛。
小部件代码:
/* Image Link Widget
===================================================== */
class ImageLinkWidget extends WP_Widget {
function ImageLinkWidget() {
$widget_ops = array(
\'classname\' => \'google-map-widget nopadding\',
\'description\' => \'An image that links somewhere\'
);
$this->WP_Widget(\'ImageLinkWidget\', \'Image + Link\', $widget_ops);
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, array( \'title\' => \'\', \'image_path\' => \'default\' ) );
$title = $instance[\'title\'];
$image_path = $instance[\'image_path\']
?>
<p>
<label for="<?php echo $this->get_field_id(\'title\'); ?>">
Title:
<input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id(\'image_path\'); ?>">
Image Path (include http://):
<input class="widefat" id="<?php echo $this->get_field_id(\'image_path\'); ?>" name="<?php echo $this->get_field_name(\'image_path\'); ?>" type="text" value="<?php echo esc_attr($image_path); ?>" />
</label>
</p>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'image_path\'] = strip_tags( $new_instance[\'image_path\'] );
return $instance;
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance[\'title\']) ? \' \' : apply_filters(\'widget_title\', $instance[\'title\']);
if (!empty($title))
echo $before_title . $title . $after_title;
// Show image plus link
//echo \'<a href="\' . $link . \'" target="\' . $target . \'">\';
//echo \'<img src="\' . $image_path . \'" alt="\' . $alt_text . \'">\';
echo $image_path;
//echo \'</a>\';
echo $after_widget;
}
}
add_action( \'widgets_init\', create_function(\'\', \'return register_widget("ImageLinkWidget");\') );
提前感谢!