New widget adding in wordpress
- by admin
- 0
Add below code in functions.php:
// widget adding
function lbi_widgets_init() {
register_widget( ‘My_Widget_Avatar’ );
}
add_action( ‘widgets_init’, ‘lbi_widgets_init’ );
class My_Widget_Avatar extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
‘wmu_text_widget’, // Base ID
__(‘text test Widget’, ‘text_domain’), // Name
array(‘description’ => __(‘testing Widget’, ‘text_domain’),) // Args
);
}
public function widget($args, $instance) {
// Our variables from the widget settings
$titles = apply_filters(‘widget_title’, empty($instance[‘titles’]) ? __(‘dhfdh’, ‘text_domain’) : $instance[‘titles’] );
$titles1 = apply_filters(‘widget_title’, empty($instance[‘titles1’]) ? __(‘hgfh’, ‘text_domain’) : $instance[‘titles1’] );
echo “gnvhj”;
ob_start();
echo $args[‘before_widget’];
if (!empty($instance[‘titles’])) {
echo $args[‘before_title’] . $titles . $args[‘after_title’];
}
if (!empty($instance[‘titles1’])) {
echo $args[‘before_title’] . $titles1 . $args[‘after_title’];
}
?>
<?php
echo $args[‘after_widget’];
ob_end_flush();
}
public function form($instance) {
$title = !empty($instance[‘titles’]) ? $instance[‘titles’] : __(‘New title’, ‘text_domain’);
$title1 = !empty($instance[‘titles1’]) ? $instance[‘titles1’] : __(‘New titles’, ‘text_domain’);
?>
<p>
<label for=”<?php echo $this->get_field_id(‘titles’); ?>”><?php _e(‘Title:’); ?></label>
<input class=”widefat” id=”<?php echo $this->get_field_id(‘titles’); ?>” name=”<?php echo $this->get_field_name(‘titles’); ?>” type=”text” value=”<?php echo esc_attr($title); ?>”>
</p>
<p>
<label for=”<?php echo $this->get_field_id(‘titles1’); ?>”><?php _e(‘Title1:’); ?></label>
<input class=”widefat” id=”<?php echo $this->get_field_id(‘titles1’); ?>” name=”<?php echo $this->get_field_name(‘titles1’); ?>” type=”text” value=”<?php echo esc_attr($title1); ?>”>
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance[‘titles’] = (!empty($new_instance[‘titles’]) ) ? strip_tags($new_instance[‘titles’]) : ”;
$instance[‘titles1’] = (!empty($new_instance[‘titles1’]) ) ? strip_tags($new_instance[‘titles1’]) : ”;
return $instance;
}
}
Add below code in functions.php: // widget adding function lbi_widgets_init() { register_widget( ‘My_Widget_Avatar’ ); } add_action( ‘widgets_init’, ‘lbi_widgets_init’ ); class My_Widget_Avatar extends WP_Widget { /** * Register widget with WordPress. */ function __construct() { parent::__construct( ‘wmu_text_widget’, // Base ID __(‘text test Widget’, ‘text_domain’), // Name array(‘description’ => __(‘testing Widget’, ‘text_domain’),) // Args ); } public…
Add below code in functions.php: // widget adding function lbi_widgets_init() { register_widget( ‘My_Widget_Avatar’ ); } add_action( ‘widgets_init’, ‘lbi_widgets_init’ ); class My_Widget_Avatar extends WP_Widget { /** * Register widget with WordPress. */ function __construct() { parent::__construct( ‘wmu_text_widget’, // Base ID __(‘text test Widget’, ‘text_domain’), // Name array(‘description’ => __(‘testing Widget’, ‘text_domain’),) // Args ); } public…