2012-03-22 68 views
4

我是新的wordpress和小部件。如何添加另一個文本框WP_Widget_Text

我想添加另一個字段到默認文本小部件。你們能幫我嗎?

的什麼,我試圖做一個形象: http://imgur.com/KoYvV

我能夠編輯默認widgets.php並添加另一個textarea,但它不能正常工作。請幫助並指導我走向正確的方向。

class WP_Widget_Text extends WP_Widget { 

    function __construct() { 
     $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML')); 
     $control_ops = array('width' => 400, 'height' => 350); 
     parent::__construct('text', __('Text'), $widget_ops, $control_ops); 
    } 

    function widget($args, $instance) { 
     extract($args); 
     $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); 
     $text = apply_filters('widget_text', empty($instance['text']) ? '' : $instance['text'], $instance); 
     $text = apply_filters('widget_text2', empty($instance['text2']) ? '' : $instance['text2'], $instance); 
     echo $before_widget; 
     if (!empty($title)) { echo $before_title . $title . $after_title; } ?> 
      <div class="textwidget"> 
       <?php echo !empty($instance['filter']) ? wpautop($text) : $text; ?> 
      </div> 
      <div class="textwidget2"> 
       <?php echo !empty($instance['filter']) ? wpautop($text2) : $text2; ?> 
      </div> 
     <?php 
     echo $after_widget; 
    } 

    function update($new_instance, $old_instance) { 
     $instance = $old_instance; 
     $instance['title'] = strip_tags($new_instance['title']); 
     if (current_user_can('unfiltered_html')) { 
      $instance['text'] = $new_instance['text']; 
      $instance['text2'] = $new_instance['text2']; 
     } else { 
      $instance['text'] = stripslashes(wp_filter_post_kses(addslashes($new_instance['text']))); // wp_filter_post_kses() expects slashed 
      $instance['text2'] = stripslashes(wp_filter_post_kses(addslashes($new_instance['text2']))); // wp_filter_post_kses() expects slashed 
     } 
     $instance['filter'] = isset($new_instance['filter']); 
     return $instance; 
    } 

    function form($instance) { 
     $instance = wp_parse_args((array) $instance, array('title' => '', 'text' => '', 'text2' => '')); 
     $title = strip_tags($instance['title']); 
     $text = esc_textarea($instance['text']); 
     $text2 = esc_textarea($instance['text2']); 
?> 
     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></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 esc_attr($title); ?>" /></p> 

     <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea> 
     <textarea class="widefat" rows="8" cols="20" id="<?php echo $this->get_field_id('text2'); ?>" name="<?php echo $this->get_field_name('text2'); ?>"><?php echo $text2; ?></textarea> 

     <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p> 
<?php 
    } 
} 
+1

這屬於http://wordpress.stackexchange.com/ – 2012-03-22 12:48:27

+0

什麼不工作?你沒有顯示那個小部件的內容?你能解釋更多嗎? – 2012-03-22 14:48:11

回答

3

我不知道你是否想通了,但這裏是解決你的問題。

首先你不想直接編輯小部件。如果你更新WordPress,你的代碼將被覆蓋。 WordPress的高度可擴展性,所以你永遠不必直接編輯代碼。

理想情況下,您應該創建一個插件。如果你想學習如何閱讀這個http://codex.wordpress.org/Writing_a_Plugin

關於WordPress的另一個偉大的事情是,他們有非常好的文檔。

但是,爲了簡單起見,我們只會在您使用的主題的functions.php中執行此操作。你可以找到它在/wp-content/themes/your_theme/functions.php

下面是代碼樣本在你的問題修改後的版本:

//First we need to change the name of the Widget. I just named it RT_Widget_Text, so just be aware that you can change it to anything you want and to replace all instances of that text. Just make sure to not name it WP_Widget_Text 
class RT_Widget_Text extends WP_Widget { 

    function __construct() { 
     //I made a change here. I changed the class name to widget_double_text. This probably has no effect, but I just changed it for good measure. 
     $widget_ops = array('classname' => 'widget_double_text', 'description' => __('Arbitrary text or HTML')); 
     $control_ops = array('width' => 400, 'height' => 350); 

     //Here is the important part. Change the "text" to "double_text" and "Text" to "Text 2" or to some other text that will identify the widget. 
     parent::__construct('double_text', __('Text 2'), $widget_ops, $control_ops); 
    } 

    function widget($args, $instance) { 
     extract($args); 
     $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); 
     $text = apply_filters('widget_text', empty($instance['text']) ? '' : $instance['text'], $instance); 
     //I changed this to $text2 = instead of $text = 
     //This would have caused the first text section to display you text2 and your text2 to display nothing. 
     //I wonder if this was your issue? 
     $text2 = apply_filters('widget_text', empty($instance['text2']) ? '' : $instance['text2'], $instance); 
     echo $before_widget; 
     if (!empty($title)) { echo $before_title . $title . $after_title; } ?> 
      <div class="textwidget"> 
       <?php echo !empty($instance['filter']) ? wpautop($text) : $text; ?> 
      </div> 
      <div class="textwidget2"> 
       <?php echo !empty($instance['filter']) ? wpautop($text2) : $text2; ?> 
      </div> 
     <?php 
     echo $after_widget; 
    } 

    function update($new_instance, $old_instance) { 
     $instance = $old_instance; 
     $instance['title'] = strip_tags($new_instance['title']); 
     if (current_user_can('unfiltered_html')) { 
      $instance['text'] = $new_instance['text']; 
      $instance['text2'] = $new_instance['text2']; 
     } else { 
      $instance['text'] = stripslashes(wp_filter_post_kses(addslashes($new_instance['text']))); // wp_filter_post_kses() expects slashed 
      $instance['text2'] = stripslashes(wp_filter_post_kses(addslashes($new_instance['text2']))); // wp_filter_post_kses() expects slashed 
     } 
     $instance['filter'] = isset($new_instance['filter']); 
     return $instance; 
    } 

    function form($instance) { 
     $instance = wp_parse_args((array) $instance, array('title' => '', 'text' => '', 'text2' => '')); 
     $title = strip_tags($instance['title']); 
     $text = esc_textarea($instance['text']); 
     $text2 = esc_textarea($instance['text2']); 
?> 
     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></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 esc_attr($title); ?>" /></p> 

     <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea> 
     <textarea class="widefat" rows="8" cols="20" id="<?php echo $this->get_field_id('text2'); ?>" name="<?php echo $this->get_field_name('text2'); ?>"><?php echo $text2; ?></textarea> 

     <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p> 
<?php 
    } 
} 
//Now you need to register your widget. Remember to change it to whatever you named your widget 
add_action('widgets_init', create_function('', 'register_widget("RT_Widget_Text");')); 

與所有的說,我認爲你的錯誤可能會在您widget($args, instance)方法是一個簡單的拼寫錯誤在這裏:

$text = apply_filters('widget_text', empty($instance['text']) ? '' : $instance['text'], $instance); 
$text = apply_filters('widget_text2', empty($instance['text2']) ? '' : $instance['text2'], $instance); 

請注意,您在這裏設置了兩次$text =。所以你基本上覆蓋了文本的價值,並用text2取而代之。然後,將text2留空。

如果這不是你的問題,那麼它可能與註冊Widget爲「文本」有關,因爲這是WP_Widget_Text所採取的。這不太可能,因爲我假設你直接編輯WP_Widget_Text類。

相關問題