WP Widget - 在前端缺少字段

时间:2015-02-14 17:00:58

标签: php wordpress widget

我已经创建了一个集成小部件的插件。小部件用于收集各种数据,然后在侧边栏中输出。 显然收集数据是因为它们出现在后端。但是,它们不会转移到前端。

标题 - 图片名称 - 描述 - 链接 - 链接文本

除最后两个字段外,所有字段都显示在前端。只有链接和链接文字才会显示出来。

任何人都可以帮我解决这个问题吗?

提前谢谢!!!

<?php
/*
Plugin Name: Widget-Plugin
Plugin URI: http://www.123456.de
Description: Plugin zur Anzeige eines Widgets
Version: 1.0
*/

class wp_widget_plugin extends WP_Widget {

// constructor
function wp_widget_plugin() {
    parent::WP_Widget(false, $name = __('widget Widget', 'wp_widget_plugin') );
}   


// widget form creation
function form($instance) {

// Check values
if( $instance) {
     $title = esc_attr($instance['title']);
     $text = esc_attr($instance['text']);
     $textarea = esc_textarea($instance['textarea']);
     $select = esc_attr($instance['select']); 
     $hlink = esc_attr($instance['hlink']);
     $hlinktext = esc_attr($instance['hlinktext']);
} else {
     $title = '';
     $text = '';
     $textarea = '';
     $select = '';
     $hlink = '';
     $hlinktext = '';
}
?>

<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Titel</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('text'); ?>">Bildname</label>
<input class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" type="text" value="<?php echo $text; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('textarea'); ?>">Beschreibung</label>
<textarea class="widefat" rows="8" cols="20" id="<?php echo $this->get_field_id('textarea'); ?>" name="<?php echo $this->get_field_name('textarea'); ?>"><?php echo $textarea; ?></textarea>
</p>

<p>
<label for="<?php echo $this->get_field_id('hlink'); ?>">Link</label>
<input class="widefat" id="<?php echo $this->get_field_id('hlink'); ?>" name="<?php echo $this->get_field_name('hlink'); ?>" value="<?php echo $hlink; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('hlinktext'); ?>">Linktext</label>
<input class="widefat" id="<?php echo $this->get_field_id('hlinktext'); ?>" name="<?php echo $this->get_field_name('hlinktext'); ?>" value="<?php echo $hlinktext; ?>" />
</p>

<?php
}

// update widget
function update($new_instance, $old_instance) {
    $instance = $old_instance;
    // Fields
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['text'] = strip_tags($new_instance['text']);
    $instance['hlink'] = strip_tags($new_instance['hlink']);
    $instance['hlinktext'] = strip_tags($new_instance['hlinktext']);
    if ( current_user_can('unfiltered_html') )
    $instance['textarea'] =  $new_instance['textarea'];
else
    $instance['textarea'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['textarea']) ) );
    return $instance;
}

// display widget
function widget($args, $instance) {
    extract( $args );

    // these are the widget options
    $title = apply_filters('widget_title', $instance['title']);

    $text = $instance['text'];

    $textarea = apply_filters( 'widget_textarea', empty( $instance['textarea'] ) ? '' : $instance['textarea'], $instance );
    echo $before_widget;

    // Display the widget
    echo '<div class="h-widget">';

        if ( $title ) {
          echo '<div class="con-title"><h2>' . $title . '</h2></div>';
        }

        if( $text ) {
          echo '<div class="con-img"><img src="../wp-content/uploads/' . $text . '.jpg"></div>';
        }
        echo '<div class="con-text">';
        if( $textarea ) {
        echo wpautop($textarea); 
        }
        echo '</div>';

        echo '<div class="con-title"><h2><a href="';
        if( $hlink ) {
            echo ' . $hlink . ';
        }
        echo '">';
        if( $hlinktext ) {
            echo ' . $hlinktext . ';
        }
        echo '</a></h2></div>';
    echo '</div>';
echo $after_widget;
}
}

// register widget
add_action('widgets_init', create_function('', 'return            r egister_widget("wp_widget_plugin");'));



?>

1 个答案:

答案 0 :(得分:0)

$hlink$hlinktext未定义。您应该在输出HTML之前定义它们(可能在echo $before_widget;行之前),如下所示:

$hlink = $instance['hlink'];
$hlinktext = $instance['hlinktext'];