Примеры работ

https://codex.wordpress.org/Widgets_API

В файл functions.php прописываем класс виджета.

Пример пустого класса:


class Xlo_Widget extends WP_Widget {
 
	/**
	 * Sets up the widgets name etc
	 */
	public function __construct() {
		$widget_ops = array( 
			'classname' => 'xlo_widget',
			'description' => 'Xlo Widget is awesome',
		);
		parent::__construct( 'xlo_Widget', 'Xlo Widget', $widget_ops );
	}
 
	/**
	 * Outputs the content of the widget FRONTEND
	 *
	 * @param array $args
	 * @param array $instance
	 */
	public function widget( $args, $instance ) {
		// outputs the content of the widget FRONTEND
	}
 
	/**
	 * Outputs the options form on admin
	 *
	 * @param array $instance The widget options
	 */
	public function form( $instance ) {
		// outputs the options form on admin BACKEND
	}
 
	/**
	 * Processing widget options on save
	 *
	 * @param array $new_instance The new options
	 * @param array $old_instance The previous options
	 *
	 * @return array
	 */
	public function update( $new_instance, $old_instance ) {
		// processes widget options to be saved Сохраняет введенные данные из админки
	}
}
function register_xlo_widget() {
    register_widget( 'Xlo_Widget' );
    register_widget( 'Foo_Widget' );//какой-либо еще виджет
}
add_action( 'widgets_init', 'register_xlo_widget' );

 Пример рабочего класса:


<?php
class Foo_Widget extends WP_Widget {
 
	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'foo_widget', // Base ID
			esc_html__( 'Foo Widget', 'text_domain' ), // Name
			array( 'description' => esc_html__( 'A Foo Widget', 'text_domain' ), ) // Args
		);
	}
 
	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database. ПУБЛИЧНАЯ ЧАСТЬ. ТУт выводим значения введенные в админской части
	 */
	public function widget( $args, $instance ) {
		echo $args['before_widget'];
		if ( ! empty( $instance['title'] ) ) {
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
		}
		echo esc_html__( 'Hello, World!', 'text_domain' );
		echo $args['after_widget'];
	}
 
	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database. АДМИНСКАЯ ЧАСТЬ. Здесь вводим значения в полях
	 */
	public function form( $instance ) {
		$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );//Если в базе этого значения нет до добавляем placeholder
		?>
		<p>
		<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></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 
	}
 
	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved. Запись введенных значений в бд 
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';//сохраняет введенное значение в базу. Если такого нет, создаст новую запись в бд
 
		return $instance;
	}
 
} // class Foo_Widget

 Содержимое виджета выводится в сайдбаре, куда мы перетащили виджет. А сайдбар выводится там куда поместили сайдбар. См. главу о сайдбарах