Created
September 7, 2018 04:08
-
-
Save IamSwap/9022895d7f7bb80a7c6bade145731afe to your computer and use it in GitHub Desktop.
WordPress Sample Widget
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 | |
/** | |
* Widget Name: Company WidgetName Widget | |
* Description: A sample widget. | |
*/ | |
class Company_WidgetName_Widget extends WP_Widget { | |
/** | |
* Register widget with WordPress. | |
*/ | |
public function __construct() { | |
parent::__construct( | |
'company_widgetname_widget', // Widget ID | |
__('Company WidgetName', 'text-domain'), // Widget Name | |
array( 'description' => __('A sample widget.', 'text-domain'), ) // Widget Descriptions | |
); | |
} | |
/** | |
* Back-end widget form. | |
* @param array $instance Previously saved values from database. | |
*/ | |
public function form($instance) { | |
$defaults = array( 'title' => __('Title', 'text-domain')); | |
$instance = wp_parse_args($instance, $defaults); ?> | |
<p> | |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'text-domain'); ?></label> | |
<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" class="widefat" /> | |
</p> | |
<?php | |
} | |
/** | |
* Front-end display of widget. | |
* @param array $args Widget arguments. | |
* @param array $instance Saved values from database. | |
*/ | |
public function widget($args, $instance) { | |
extract($args); | |
$title = apply_filters('widget_title', $instance['title']); | |
echo $before_widget; | |
if ($title) { | |
echo $before_title . $title . $after_title; | |
} ?> | |
<ul class="list-unstyled"> | |
</ul> | |
<?php echo $after_widget; | |
} | |
/** | |
* Sanitize widget form values as they are saved. | |
* @param array $new_instance Values just sent to be saved. | |
* @param array $old_instance Previously saved values from database. | |
* @return array Updated safe values to be saved. | |
*/ | |
public function update($new_instance, $old_instance) { | |
$instance = array(); | |
//The strip_tags() function strips a string from HTML, XML, and PHP tags. | |
$instance['title'] = strip_tags($new_instance['title']); | |
return $instance; | |
} | |
} | |
// function to register popular post widget | |
add_action('widgets_init', function () { | |
register_widget('company_widgetname_widget'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment