Created
November 2, 2022 10:18
-
-
Save MrJoshFisher/1ff71c2793453ac0e465345c70bb2d10 to your computer and use it in GitHub Desktop.
[Widget] #wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Creating the widget | |
class spacer_widget extends WP_Widget { | |
function __construct() { | |
parent::__construct('spacer_widget', __('Spacer', 'spacer_widget_domain'), array( 'description' => __( 'Spacer widget', 'spacer_widget_domain' ))); | |
} | |
public function widget( $args, $instance ) { | |
$space_px = apply_filters( 'space_px', $instance['space_px'] ); | |
echo $args['before_widget']; | |
echo $space_px; | |
echo $args['after_widget']; | |
} | |
public function form( $instance ) { | |
$space_px = ((isset( $instance[ 'space_px' ] ) )?$instance[ 'space_px' ]:20); | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'space_px' ); ?>"><?php _e( 'Space Size (px):' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'space_px' ); ?>" name="<?php echo $this->get_field_name( 'space_px' ); ?>" type="text" value="<?php echo esc_attr( $space_px ); ?>" /> | |
</p> | |
<?php | |
} | |
public function update( $new_instance, $old_instance ) { | |
$instance = array(); | |
$instance['space_px'] = ( ! empty( $new_instance['space_px'] ) ) ? strip_tags( $new_instance['space_px'] ) : ''; | |
return $instance; | |
} | |
} | |
// Register and load the widget | |
function wpb_load_widget() { | |
register_widget( 'spacer_widget' ); | |
} | |
add_action( 'widgets_init', 'wpb_load_widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment