2013-10-26 70 views
1

我通過php programe爲wordpress創建了自己的小部件代碼...並且我想將小部件添加到小部件面板..如果我想在小部件面板中顯示該小部件我在哪裏添加(指wordpress中的哪個文件)我的小部件代碼...?在wordpress中添加我自己創建的小部件代碼

<?php 

add_action('widgets_init', 'my_widget'); 


function my_widget() { 
    register_widget('MY_Widget'); 
} 

class MY_Widget extends WP_Widget { 

    function MY_Widget() { 
     $widget_ops = array('classname' => 'example', 'description' => __('A widget that displays the authors name ', 'example')); 

     $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'example-widget'); 

     $this->WP_Widget('example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops); 
    } 

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

     //Our variables from the widget settings. 
     $title = apply_filters('widget_title', $instance['title']); 
     $name = $instance['name']; 
     $show_info = isset($instance['show_info']) ? $instance['show_info'] : false; 

     echo $before_widget; 

     // Display the widget title 
     if ($title) 
      echo $before_title . $title . $after_title; 

     //Display the name 
     if ($name) 
      printf('<p>' . __('Hey their Sailor! My name is %1$s.', 'example') . '</p>', $name); 


     if ($show_info) 
      printf($name); 


     echo $after_widget; 
    } 

    //Update the widget 

    function update($new_instance, $old_instance) { 
     $instance = $old_instance; 

     //Strip tags from title and name to remove HTML 
     $instance['title'] = strip_tags($new_instance['title']); 
     $instance['name'] = strip_tags($new_instance['name']); 
     $instance['show_info'] = $new_instance['show_info']; 

     return $instance; 
    } 


    function form($instance) { 

     //Set up some default widget settings. 
     $defaults = array('title' => __('Example', 'example'), 'name' => __('Bilal Shaheen', 'example'), 'show_info' => true); 
     $instance = wp_parse_args((array) $instance, $defaults); ?> 

     //Widget Title: Text Input. 
     <p> 
      <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'example'); ?></label> 
      <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /> 
     </p> 

     //Text Input. 
     <p> 
      <label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Your Name:', 'example'); ?></label> 
      <input id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" value="<?php echo $instance['name']; ?>" style="width:100%;" /> 
     </p> 


     //Checkbox. 
     <p> 
      <input class="checkbox" type="checkbox" <?php checked($instance['show_info'], true); ?> id="<?php echo $this->get_field_id('show_info'); ?>" name="<?php echo $this->get_field_name('show_info'); ?>" /> 
      <label for="<?php echo $this->get_field_id('show_info'); ?>"><?php _e('Display info publicly?', 'example'); ?></label> 
     </p> 

    <?php 
    } 
} 

?>

+0

簡單的方法:在你的模板的'functions.php' – revo

+0

@revo雅感謝 – kalyan

回答

1

你可以把它放在你的主題functions.php文件,但它會更好,如果你把它放在一個單獨的文件,然後包括像

include('mywidget.php'); 

functions.php文件創建文件(mywidget.php)並將其保存在主題根文件夾(其中functions.php所在位置)中,並將代碼粘貼到該文件中,就是這樣。將小部件代碼保存在不同的文件中將保持您的functions.php的清潔,並且您的小部件文件將在其自己的文件中易於閱讀/編輯,而無需查看functions.php中的代碼。

+1

感謝...工作不錯... – kalyan

+0

@kalyan,歡迎您:-) –

+0

海,你能告訴我確切的使用apply_filters的()和鉤子.. – kalyan

相關問題