Example:
<?php class Radio_Fields_Widget extends WP_Widget { function __construct() { // Instantiate the parent object $widget_ops = array( 'classname' => 'widget_radio_fields_entries', 'description' => esc_attr__('Radio Field Widget - Created by deshisoft', 'eye-theme') ); $control_ops = array( 'width' => 275 ); parent::__construct( false, 'Radio Field Widget', $widget_ops, $control_ops ); } function widget( $args, $instance ) { extract( $args ); // Widget output $title = apply_filters('widget_title', $instance['title']); $radio = apply_filters('widget_radio', $instance['radio']); echo $before_widget; echo '<div style="border:1px solid #ddd; background:#f6f6f6;padding:15px;">'; echo esc_attr($title); echo '<br/>'; echo esc_attr($radio); echo '</div>'; echo $after_widget; } function update( $new_instance, $old_instance ) { // Save widget options $instance = array(); $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['radio'] = strip_tags( $new_instance['radio'] ); return $instance; } function form( $instance ) { // Output admin widget options form $title = $instance['title']; $radio = $instance['radio']; ?> <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 $title; ?>" /></p> <p><label for="<?php echo $this->get_field_id('radio_option_1'); ?>"><?php _e('Option 1:'); ?> <input class="" id="<?php echo $this->get_field_id('radio_option_1'); ?>" name="<?php echo $this->get_field_name('radio'); ?>" type="radio" value="radio_option_1" <?php if($radio === 'radio_option_1'){ echo 'checked="checked"'; } ?> /></label><br> <label for="<?php echo $this->get_field_id('radio_option_2'); ?>"><?php _e('Option 2:'); ?> <input class="" id="<?php echo $this->get_field_id('radio_option_2'); ?>" name="<?php echo $this->get_field_name('radio'); ?>" type="radio" value="radio_option_2" <?php if($radio === 'radio_option_2'){ echo 'checked="checked"'; } ?> /></label></p> <?php } } function radio_field_register_widgets() { register_widget( 'Radio_Fields_Widget' ); } add_action( 'widgets_init', 'radio_field_register_widgets' ); ?>
Theme Folder -> function.php
Add this line:
require get_template_directory() . '/inc/radio_widget.php'; // inc folder name depend on your theme.