Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active July 16, 2019 11:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/7141599 to your computer and use it in GitHub Desktop.
Save westonruter/7141599 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Old-Style Single Widget with Wide Control
* Description: For testing purposes.
* Author: Weston Ruter, XWP
* Author URI: https://weston.ruter.net/
*/
add_action( 'widgets_init', 'single_widget_register' );
/**
* Register single widget.
*/
function single_widget_register() {
$id = 'single';
$name = __( 'Wide Single', 'single-widget' );
$widget_ops = array(
'classname' => 'widget_single',
'description' => __( 'Old single wide widget for testing purposes.', 'single-widget' ),
);
$control_ops = array(
'width' => 500,
'height' => 100,
'id_base' => $id,
);
wp_register_sidebar_widget( $id, $name, 'single_widget_display', $widget_ops, array() );
wp_register_widget_control( $id, $name, 'single_widget_update_form', $control_ops, array() );
}
/**
* Display widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget args.
*/
function single_widget_display( $args ) {
$options = array_merge(
array( 'title' => '' ),
get_option( 'widget_single', array() )
);
$title = apply_filters( 'widget_title', $options['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'];
echo $title;
echo $args['after_title'];
}
?>
<div id="single_wrap">
<?php _e( 'I am an old single wide widget which should be deprecated in favor of <code>WP_Widget</code>!', 'single-widget' ); ?>
</div>
<?php
echo $args['after_widget'];
}
/**
* Show the widget form and handle updates to the widget.
*
* @see WP_Widget::form()
* @see WP_Widget::update()
*/
function single_widget_update_form() {
global $wp_registered_widget_controls;
$instance = array_merge(
array( 'title' => '' ),
get_option( 'widget_single', array() )
);
if ( isset( $_POST['single-submit'] ) ) {
$instance['title'] = sanitize_text_field( wp_unslash( $_POST['single-title'] ) );
update_option( 'widget_single', $instance );
}
?>
<p>
<label for="single-title">
<?php _e( 'Title:' ); ?>
<input class="widefat" id="single-title" name="single-title" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
</label>
</p>
<p>
<em>
<?php
/* translators: placeholder is width of widget */
echo sprintf( __( 'I am an old-style single widget with a %spx wide widget control.', 'single-widget' ), $wp_registered_widget_controls['single']['width'] );
?>
</em>
</p>
<input type="hidden" id="single-submit" name="single-submit" value="1" />
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment