Created
October 25, 2013 07:40
-
-
Save jeonghwan-kim/7150864 to your computer and use it in GitHub Desktop.
Add a custom widget as wordpress plugin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: My Test Widget | |
| Plugin URI: http://wordpress.org/plugins/??? | |
| Description: Test widget. | |
| Author: Jeonghwan Kim | |
| Version: 1.0 | |
| Author URI: mailto:ej88ej@gmail.com | |
| */ | |
| class MyTestWidget extends WP_Widget { | |
| public function __construct() { | |
| $widget_ops = array ( | |
| "classname" => "my_test_widget", | |
| "discription" => __( "My Test Widget" ), | |
| ); | |
| parent::__construct( "my_test_widget", "My Test Widget", $widget_ops ); | |
| } | |
| public function form( $instance ) { | |
| $defaults = array('title'=> 'My Notice', 'name'=> '', 'notice' => ''); | |
| $instance = wp_parse_args((array)$instance, $defaults); | |
| $title = strip_tags($instance['title']); | |
| $name = strip_tags($instance['name']); | |
| $notice = strip_tags($instance['notice']); | |
| ?> | |
| <p><?php _e('Title')?>: | |
| <input class="widefat" name="<?php echo $this->get_field_name('title');?>" | |
| type="text" value="<?php echo esc_attr($title)?>" /></p> | |
| <p><?php _e('Notice')?>: | |
| <textarea class="widefat" name="<?php echo $this->get_field_name('notice');?>"> | |
| <?php echo esc_attr($notice);?></textarea></p> | |
| <?php | |
| } | |
| public function update($new_instance, $old_instance){ | |
| $instance = $old_instance; | |
| $instance['title'] = strip_tags($new_instance['title']); | |
| $instance['notice'] = strip_tags($new_instance['notice']); | |
| return $instance; | |
| } | |
| public function widget( $args, $instance ) { | |
| extract($args); | |
| echo $before_widget; | |
| echo $before_title . $instance['title'] . $after_title; | |
| echo '<ul><li><a href="">' . $instance['notice'] . '</a></lu></ul>'; | |
| echo $after_widget; | |
| } | |
| } | |
| add_action("widgets_init", "my_register_widgets"); | |
| function my_register_widgets() { | |
| register_widget("MyTestWidget"); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment