Documentation
Services Provider
Widgets

Widgets

You may register your widgets in the /config/plugin.php files in the widgets key:

/config/plugin.php
<?php
  /*
  |--------------------------------------------------------------------------
  | Widgets
  |--------------------------------------------------------------------------
  |
  | Here is where you can register all of the Widget for a plugin.
  |
  */
 
  'widgets' => [ ],

WP Bones provides a new Widget class to manage a WordPress widget. Let's see a simple example of Widget:

/plugin/Widgets/MyWidget.php
<?php
namespace WPKirk\Widgets;
 
use WPKirk\WPBones\Support\Widget;
 
class MyWidget extends Widget
{
 
  /**
   * Base ID for the widget, lower case, if left empty a portion of the widget's class name will be used. Has to be
   * unique.
   *
   * @var string
   */
  public $id_base = 'wpkirk-demo-widget';
 
  /**
   * Name for the widget displayed on the configuration page.
   *
   * @var string
   */
  public $name = 'WP Kirk Widget';
 
  /**
   * Optional. Passed to wp_register_sidebar_widget()
   *
   * - description: shown on the configuration page
   * - classname
   *
   * @var array
   */
  public $widget_options = [
    'description' => 'WP Kirk Demo Widget Description'
  ];
 
  /**
   * Optional. Passed to wp_register_widget_control()
   *
   * - width: required if more than 250px
   * - height: currently not used but may be needed in the future
   *
   * @var array
   */
  public $control_options = [
    'width'           => 400,
    'height'          => 350,
  ];
 
  /**
   * Update the widget options.
   *
   * @return array
   */
  public function update( $new_instance, $old_instance )
  {
    $old_instance[ 'title' ] = ( $new_instance[ 'title' ] );
 
    return $old_instance;
  }
 
  /**
   * Retrun a key pairs array with the default value for widget.
   *
   * @return array
   */
  public function defaults()
  {
    return [
      'title' => 'My Title',
    ];
  }
 
  public function viewForm( $instance )
  {
    return WPKirk()->view( 'widgets.form' );
  }
 
  public function viewWidget( $args, $instance )
  {
    return WPKirk()->view( 'widgets.index' );
  }
}

As you can see, this new Widget class supports the resource view like a view.

The viewForm method should return the view used in the backend admin area.

The viewWidget should return the view used in the frontend. In other words, this is the output of widget.

In these methods, you may choose which view to return. Inside the view, you'll have available $plugin instance, $instance WordPress variable and Html facade. For example, in widgets.form you can:

<div>
	<h1>Widget Backend</h1>
	<h3><?php echo $plugin->Name ?></h3>
	<?php echo WPKirk\Html::button( 'Click Me!' ) ?>
</div>

Of course, if you wish to share the $instance variable, then you can:

return WPKirk()->view( 'widgets.form' )->with( 'instance', $instance );
<div>
	<h1>Widget Backend</h1>
	<h2><?php var_dump( $instance ) ?></h2>
	<h3><?php echo $plugin->Name ?></h3>
	<?php echo WPKirk\Html::button( 'Click Me!' ) ?>
</div>

PHP Bones Command

Using the php bones make:widget Bones command, we can quickly create an Widget Service Provider.