wordpress widget小工具圖解

Class My_Widget1 extends WP_Widget{
	public function __construct() {
		$widget_ops = array( 
			'classname' => 'my_widget2',
			'description' => 'My Widget is awesome',
		);
		parent::__construct( 'my_widget2', 'My Widget', $widget_ops );
	}

		/**
	 * Outputs the content of the widget
	 *
	 * @param array $args
	 * @param array $instance
	 */
	public function widget( $args, $instance ) {
		// outputs the content of the widget
		$out_html='<h1>my title is '.$instance['title'].'</h1>';
		echo $out_html;
	}

	/**
	 * Outputs the options form on admin
	 *
	 * @param array $instance The widget options
	 */
	public function form( $instance ) {
		$title=empty($instance['title']) ? 'hello word' : $instance['title'];
		echo '<input type="text" name="'.$this->get_field_name('title').'" value="'.$title.'" />';
	}

	/**
	 * Processing widget options on save
	 *
	 * @param array $new_instance The new options
	 * @param array $old_instance The previous options
	 *
	 * @return array
	 */
	public function update( $new_instance, $old_instance ) {
		// processes widget options to be saved
		$instance=[];
		$instance['title']=$new_instance['title'];
		return $instance;
	}
}
add_action('widgets_init',function(){
	register_widget('my_widget1');
});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章