Call to Action Button
class Button_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lp-button-widget',
__( 'Call to Action Button', 'launchpad' ),
array(
'description' => __( 'Creates a button using FoundationPress ".sites-button"', 'launchpad' ),
'classname' => 'lp-button-widget',
)
);
}
public function widget( $args, $instance ) {
echo '<div class="sites-button '.$instance['lp_button_class'].'"><a href="'.$instance['lp_button_url'].'">'.$instance['lp_button_text'].'</a></div>';
}
public function form( $instance ) {
// Set default values
$instance = wp_parse_args( (array) $instance, array(
'lp_button_text' => '',
'lp_button_url' => '',
'lp_button_class' => '',
) );
// Retrieve an existing value from the database
$lp_button_text = !empty( $instance['lp_button_text'] ) ? $instance['lp_button_text'] : '';
$lp_button_url = !empty( $instance['lp_button_url'] ) ? $instance['lp_button_url'] : '';
$lp_button_class = !empty( $instance['lp_button_class'] ) ? $instance['lp_button_class'] : '';
// Form fields
echo '<p>';
echo ' <label for="' . $this->get_field_id( 'lp_button_text' ) . '" class="lp_button_text_label">' . __( 'Button Text', 'launchpad' ) . '</label>';
echo ' <input type="text" id="' . $this->get_field_id( 'lp_button_text' ) . '" name="' . $this->get_field_name( 'lp_button_text' ) . '" class="widefat" placeholder="' . esc_attr__( 'Contact Us', 'launchpad' ) . '" value="' . esc_attr( $lp_button_text ) . '">';
echo '</p>';
echo '<p>';
echo ' <label for="' . $this->get_field_id( 'lp_button_url' ) . '" class="lp_button_url_label">' . __( 'Button URL', 'launchpad' ) . '</label>';
echo ' <input type="text" id="' . $this->get_field_id( 'lp_button_url' ) . '" name="' . $this->get_field_name( 'lp_button_url' ) . '" class="widefat" placeholder="' . esc_attr__( '/contact/', 'launchpad' ) . '" value="' . esc_attr( $lp_button_url ) . '">';
echo ' <span class="description">' . __( 'Use absolute or relative url', 'launchpad' ) . '</span>';
echo '</p>';
echo '<p>';
echo ' <label for="' . $this->get_field_id( 'lp_button_class' ) . '" class="lp_button_class_label">' . __( 'Extra class to add to the button', 'launchpad' ) . '</label>';
echo ' <input type="text" id="' . $this->get_field_id( 'lp_button_class' ) . '" name="' . $this->get_field_name( 'lp_button_class' ) . '" class="widefat" placeholder="' . esc_attr__( '', 'launchpad' ) . '" value="' . esc_attr( $lp_button_class ) . '">';
echo '</p>';
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['lp_button_text'] = !empty( $new_instance['lp_button_text'] ) ? strip_tags( $new_instance['lp_button_text'] ) : '';
$instance['lp_button_url'] = !empty( $new_instance['lp_button_url'] ) ? strip_tags( $new_instance['lp_button_url'] ) : '';
$instance['lp_button_class'] = !empty( $new_instance['lp_button_class'] ) ? strip_tags( $new_instance['lp_button_class'] ) : '';
return $instance;
}
}
function lp_register_widgets() {
register_widget( 'Button_Widget' );
}
add_action( 'widgets_init', 'lp_register_widgets' );