Created
May 3, 2014 00:58
-
-
Save alvarovillafane/d2b43b6465f159f5f1c4 to your computer and use it in GitHub Desktop.
Wordpress Widget Example + Site-Specific 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: AVM Plugin | |
| Description: Site specific code changes for AVM plugin | |
| */ | |
| // Additing Action hook widgets_init | |
| add_action( 'widgets_init', 'avm_widget'); | |
| function avm_widget() { | |
| register_widget( 'avm_widget_info' ); | |
| } | |
| class avm_widget_info extends WP_Widget { | |
| //Name the widget, here avm Widget will be displayed as widget name, $widget_ops may be an array of value, which may holds the title, description like that. | |
| function avm_widget_info () { | |
| $widget_ops = array( 'classname' => 'example', 'description' => __('Descirption of the widget ', 'avm_widget_info') );// optional | |
| $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'avm-widget' ); //optional | |
| $this->WP_Widget('avm_widget_info', 'Title of the widget', $widget_ops ); | |
| } | |
| //Designing the form widget, which will be displayed in the admin dashboard widget location. | |
| public function form( $instance ) { | |
| if ( isset( $instance[ 'name' ]) && isset ($instance[ 'domain' ]) && isset($instance[ 'designation' ]) ) { | |
| $name = $instance[ 'name' ]; | |
| $domain = $instance[ 'domain' ]; | |
| $designation = $instance[ 'designation' ]; | |
| } | |
| else { | |
| $name = __( '', 'bc_widget_title' ); | |
| $domain = __( '', 'bc_widget_title' ); | |
| $designation = __( '', 'bc_widget_title' ); | |
| } ?> | |
| <p>Name: <input name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name );?>" /></p> | |
| <p>Domain: <input name="<?php echo $this->get_field_name( 'domain' ); ?>" type="text" value="<?php echo esc_attr( $domain ); ?>" /></p> | |
| <p>Designation: <input name="<?php echo $this->get_field_name( 'designation' ); ?>" type="text" value="<?php echo esc_attr( $designation ); ?>" /></p> | |
| <?php | |
| } | |
| // update the new values in database | |
| function update($new_instance, $old_instance) { | |
| $instance = $old_instance; | |
| $instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : ''; | |
| $instance['domain'] = ( ! empty( $new_instance['domain'] ) ) ? strip_tags( $new_instance['domain'] ) : ''; | |
| $instance['designation'] = ( ! empty( $new_instance['designation'] ) ) ? strip_tags( $new_instance['designation'] ) : ''; | |
| return $instance; | |
| } | |
| //Display the stored widget information in webpage. | |
| function widget($args, $instance) { | |
| extract($args); | |
| echo $before_widget; //Widget starts to print information | |
| $name = apply_filters( 'widget_title', $instance['name'] ); | |
| $domain = empty( $instance['domain'] ) ? ' ' : $instance['domain']; | |
| $designation = empty( $instance['designation'] ) ? ' ' : $instance['designation']; | |
| if ( !empty( $name ) ) { echo $before_title . $name . $after_title; }; | |
| echo '<p>Domain: ' . $domain . '</p>'; | |
| echo '<p>Designation: ' . $designation . '</p>'; | |
| echo $after_widget; //Widget ends printing information | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment