Skip to content

Instantly share code, notes, and snippets.

@shohag-biswas
Created February 6, 2018 15:49
Show Gist options
  • Save shohag-biswas/e32fef858b8985a2a288b265f84c003c to your computer and use it in GitHub Desktop.
Save shohag-biswas/e32fef858b8985a2a288b265f84c003c to your computer and use it in GitHub Desktop.
This is how to create custom widget
<?php
/*
*Creating custom widgets
*Check out the tuts: https://www.ministryofwp.com/build-custom-wordpress-widget/
*/
class mwp_wid extends WP_Widget{
function mwp_wid(){
parent::WP_Widget(false, $name = "MWP Test Widgets");
}
function form($instance){
$name = esc_attr($instance['name']);
?>
<p>
<label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Your Name:'); ?></label>
<input id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" type="text" value="<?php echo $name; ?>" />
</p>
<?php
}
function update($new_instance,$old_instance){
$instance = $old_instance;
$instance['name'] = strip_tags($new_instance['name']);
return $instance;
}
function widget($args, $instance) {
extract( $args );
// these are the widget options
$name = $instance['name'];
echo $before_widget;
// Check if options are set
if( $name ) {
echo '<p class="wp_widget_plugin_textarea" style="font-size:25px;">'.$name.'</p>';
}
echo $after_widget;
}
}
add_action('widgets_init', create_function('', 'return register_widget("mwp_wid");'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment