class wpturbo_My_Custom_Widget extends WP_Widget {
// Initialize The Widget.
function __construct() {
parent::__construct(
'wpturbo_My_Custom_Widget',
esc_html__('My Custom Widget', 'wpturbo')
array( 'description' => esc_html__('My Awesome Custom Widget', 'wpturbo'), )
);
}
// Widget Fields
private $widget_fields = array(
array(
'label' => 'My Text Field',
'id' => 'wpturbo-my-text-field',
'default' => 'My Default Value',
'type' => 'text',
),
);
// This function generates the front-end content that will be displayed.
public function widget( $args, $instance ) {
// Before and after widget is set by the theme.
echo $args['before_widget'];
// Check if user has set a title for this widget
if ( !empty($instance['title']) ) {
// Before and after widget title is set by the theme.
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
// Now, output the generated fields
echo '<p>' . $instance['wpturbo-my-text-field'] . '</p>';
echo $args['after_widget'];
}
public function field_generator( $instance ) {
$output = '';
foreach ( $this->widget_fields as $widget_field ) {
$default = '';
if ( isset($widget_field['default']) ) {
$default = $widget_field['default'];
}
$widget_value = ! empty( $instance[$widget_field['id']] ) ? $instance[$widget_field['id']] : esc_html__( $default, 'textdomain' );
switch ( $widget_field['type'] ) {
default:
$output .= '<p>';
$output .= '<label for="'.esc_attr( $this->get_field_id( $widget_field['id'] ) ).'">'.esc_attr( $widget_field['label'], 'textdomain' ).':</label> ';
$output .= '<input class="widefat" id="'.esc_attr( $this->get_field_id( $widget_field['id'] ) ).'" name="'.esc_attr( $this->get_field_name( $widget_field['id'] ) ).'" type="'.$widget_field['type'].'" value="'.esc_attr( $widget_value ).'">';
$output .= '</p>';
}
}
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'textdomain' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'textdomain' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
$this->field_generator( $instance );
}
// Sanitize widget form values as they are saved
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
foreach ( $this->widget_fields as $widget_field ) {
switch ( $widget_field['type'] ) {
default:
$instance[$widget_field['id']] = ( ! empty( $new_instance[$widget_field['id']] ) ) ? strip_tags( $new_instance[$widget_field['id']] ) : '';
}
}
return $instance;
}
}
function register_wpturbo_My_Custom_Widget_widget() {
register_widget( 'wpturbo_My_Custom_Widget' );
}
add_action( 'widgets_init', 'register_wpturbo_My_Custom_Widget_widget' );
Widgets Generator
Looking for an easy way to add custom widgets to your WordPress site? Check out our powerful widget generator! Our user-friendly interface makes it simple to add the fields you need. Try our widget generator today and see the difference it can make on your website.