Last active
September 2, 2019 06:27
-
-
Save frensuren/51b5649766072ac98546ad114f283924 to your computer and use it in GitHub Desktop.
Register a Widget in Wordpress
This file contains 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 | |
class CustomWidget extends WP_Widget | |
{ | |
public function __construct() | |
{ | |
$widget_options = array( | |
'classname' => 'custom_widget', | |
'description' => 'This is a custom widget', | |
); | |
parent::__construct('custom_widget', 'Halesi: Testimonial Widget', $widget_options); | |
} | |
public function widget($args, $instance) | |
{ | |
if (empty( $instance )){ | |
return; | |
}; | |
?> | |
<!-- Widget HTML --> | |
<?php echo $instance['title']; ?> | |
<?php | |
} | |
public function form($instance) | |
{ | |
if (isset($instance['title'])) { | |
$title = $instance['title']; | |
} else { | |
$title = __('New title', 'wpb_widget_domain'); | |
} | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" | |
name="<?php echo $this->get_field_name('title'); ?>" type="text" | |
value="<?php echo esc_attr($title); ?>"/> | |
</p> | |
<?php | |
return $instance; | |
} | |
public function update($new_instance, $old_instance) | |
{ | |
$instance = array(); | |
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : ''; | |
return $instance; | |
} | |
} | |
function register_custom_widget() | |
{ | |
register_widget('CustomWidget'); | |
} | |
add_action('widgets_init', 'register_custom_widget'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment