Featured Author
A widget that allows the admin to select an author to feature.
class Author_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'author--widget',
__( 'Featured Author', 'kln' ),
array(
'description' => __( 'A widget that displays a featured author', 'kln' ),
'classname' => 'author--widget',
)
);
}
public function widget( $args, $instance ) {
}
public function form( $instance ) {
// Set default values
$instance = wp_parse_args( (array) $instance, array(
'author--name' => '',
) );
// Retrieve an existing value from the database
$author--name = !empty( $instance['author--name'] ) ? $instance['author--name'] : '';
// Form fields
echo '<p>';
echo ' <label for="' . $this->get_field_id( 'author--name' ) . '" class="author--name_label">' . __( 'Author', 'kln' ) . '</label>';
echo ' <select id="' . $this->get_field_id( 'author--name' ) . '" name="' . $this->get_field_name( 'author--name' ) . '" class="widefat">';
echo ' <option value="id" ' . selected( $author--name, 'id', false ) . '> ' . __( 'Author Name', 'kln' ) . '</option>';
echo ' <option value="id2" ' . selected( $author--name, 'id2', false ) . '> ' . __( 'Author Name 2', 'kln' ) . '</option>';
echo ' </select>';
echo ' <span class="description">' . __( 'Select an Author', 'kln' ) . '</span>';
echo '</p>';
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['author--name'] = !empty( $new_instance['author--name'] ) ? strip_tags( $new_instance['author--name'] ) : '';
return $instance;
}
}
function register_widgets() {
register_widget( 'Author_Widget' );
}
add_action( 'widgets_init', 'register_widgets' );