0
X
Add Snippet To Project
New Project
Add To Existing Project
/**
* wpturbo_Social_Media_Follow_Buttons_Widget Class
*
* This class sets up a custom widget for the wpturbo plugin
*
* @package WPTurbo
*/
class wpturbo_Social_Media_Follow_Buttons_Widget extends WP_Widget {
/**
* Initialize the widget
*
* @return void
*/
function __construct() {
parent::__construct(
'wpturbo_Social_Media_Follow_Buttons_Widget',
esc_html__( 'Social Media Follow Buttons', 'wpturbo' ),
[ 'description' => esc_html__( 'Add social media follow buttons to your sidebar.', 'wpturbo' ), ]
);
}
/**
* Widget Fields array
*
* @var array $widget_fields Array of fields.
*/
private $widget_fields = [
[
'label' => 'Instagram',
'id' => 'wpturbo-Instagram',
'default' => 'https://instagram.com',
'type' => 'url',
],
];
/**
* This function generates the content that will be displayed, which is determined by the user's input.
*
* @param array $args Array of arguments.
* @param array $instance Array of instance.
*
* @return void
*/
public function widget( array $args, array $instance ) : void {
// 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-Instagram'] . '</p>';
echo $args['after_widget'];
}
/**
* Generates the widget fields.
*
* @param array $instance The array of options.
*
* @return void
*/
public function field_generator( array $instance ) : void {
$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>';
}
}
echo $output;
}
/**
* Outputs the options form on admin
*
* @param array $instance The array of options
*
* @return void
*/
public function form( array $instance ) : void {
$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 );
}
/**
* Processes widget options to be saved
*
* @param array $new_instance The new array of options
* @param array $old_instance The old array of options
*
* @return array $instance The updated array of options
*/
public function update( array $new_instance, array $old_instance ): array {
$instance = [];
$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;
}
}
/**
* Registers wpturbo_Social_Media_Follow_Buttons_Widget widget
*
* @return void
*/
function register_wpturbo_Social_Media_Follow_Buttons_Widget_widget() : void {
register_widget( 'wpturbo_Social_Media_Follow_Buttons_Widget' );
}
add_action( 'widgets_init', 'register_wpturbo_Social_Media_Follow_Buttons_Widget_widget' );