widget example
example widget with 2 parameters
class float_widget extends WP_Widget {
public function __construct() {
parent::__construct(
'html_id',
__( 'widget title', 'text_domain' ),
array(
'description' => __( 'demo widget', 'text_domain' ),
'classname' => 'widget classname',
)
);
}
public function widget( $args, $instance ) {
<h2> widget output </h2>
<p> hello world </p>
}
public function form( $instance ) {
// Set default values
$instance = wp_parse_args( (array) $instance, array(
'float' => '',
'floatnumber_id' => '',
) );
// Retrieve an existing value from the database
$float = !empty( $instance['float'] ) ? $instance['float'] : '';
$floatnumber_id = !empty( $instance['floatnumber_id'] ) ? $instance['floatnumber_id'] : '';
// Form fields
echo '<p>';
echo ' <label for="' . $this->get_field_id( 'float' ) . '" class="float_label">' . __( 'please fiil the input', 'text_domain' ) . '</label>';
echo ' <input type="text" id="' . $this->get_field_id( 'float' ) . '" name="' . $this->get_field_name( 'float' ) . '" class="widefat" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $float ) . '">';
echo '</p>';
echo '<p>';
echo ' <label for="' . $this->get_field_id( 'floatnumber_id' ) . '" class="floatnumber_id_label">' . __( 'number ', 'text_domain' ) . '</label>';
echo ' <input type="number" id="' . $this->get_field_id( 'floatnumber_id' ) . '" name="' . $this->get_field_name( 'floatnumber_id' ) . '" class="widefat" placeholder="' . esc_attr__( 'enter number', 'text_domain' ) . '" value="' . esc_attr( $floatnumber_id ) . '">';
echo ' <span class="description">' . __( 'id to see the id', 'text_domain' ) . '</span>';
echo '</p>';
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['float'] = !empty( $new_instance['float'] ) ? strip_tags( $new_instance['float'] ) : '';
$instance['floatnumber_id'] = !empty( $new_instance['floatnumber_id'] ) ? strip_tags( $new_instance['floatnumber_id'] ) : '';
return $instance;
}
}
function floatregister_widgets() {
register_widget( 'float_widget' );
}
add_action( 'widgets_init', 'floatregister_widgets' );