Promotion Widget
class Promotion_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'promotion',
__( 'Promotional Widget', 'kln' ),
array(
'description' => __( 'Use this widget to promote an event, store product, etc.', 'kln' ),
'classname' => 'feature--promotion',
)
);
}
public function widget( $args, $instance ) {
}
public function form( $instance ) {
// Set default values
$instance = wp_parse_args( (array) $instance, array(
'promo_title' => '',
'promo_url' => '',
) );
// Retrieve an existing value from the database
$promo_title = !empty( $instance['promo_title'] ) ? $instance['promo_title'] : '';
$promo_url = !empty( $instance['promo_url'] ) ? $instance['promo_url'] : '';
// Form fields
echo '<p>';
echo ' <label for="' . $this->get_field_id( 'promo_title' ) . '" class="promo_title_label">' . __( 'Promotion Title', 'kln' ) . '</label>';
echo ' <input type="text" id="' . $this->get_field_id( 'promo_title' ) . '" name="' . $this->get_field_name( 'promo_title' ) . '" class="widefat" placeholder="' . esc_attr__( 'Product, Event, Etc.', 'kln' ) . '" value="' . esc_attr( $promo_title ) . '">';
echo ' <span class="description">' . __( 'Title of the promotion', 'kln' ) . '</span>';
echo '</p>';
echo '<p>';
echo ' <label for="' . $this->get_field_id( 'promo_url' ) . '" class="promo_url_label">' . __( 'URL', 'kln' ) . '</label>';
echo ' <input type="url" id="' . $this->get_field_id( 'promo_url' ) . '" name="' . $this->get_field_name( 'promo_url' ) . '" class="widefat" placeholder="' . esc_attr__( 'https://example.com', 'kln' ) . '" value="' . esc_attr( $promo_url ) . '">';
echo '</p>';
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['promo_title'] = !empty( $new_instance['promo_title'] ) ? strip_tags( $new_instance['promo_title'] ) : '';
$instance['promo_url'] = !empty( $new_instance['promo_url'] ) ? strip_tags( $new_instance['promo_url'] ) : '';
return $instance;
}
}
function register_widgets() {
register_widget( 'Promotion_Widget' );
}
add_action( 'widgets_init', 'register_widgets' );