Register widgets in the WidgetServiceProvider
.
<?php namespace App\Providers;
use LaraPress\Widgets\WidgetServiceProvider as BaseWidgetServiceProvider;
class WidgetServiceProvider extends BaseWidgetServiceProvider
{
/**
* An array of class names to be registered as WordPress widgets.
*
* @var array
*/
protected $widgets = [
'App\Widgets\ExampleWidget'
];
}
Create a widget class in /app/Widgets
. It looks very similar to the WordPress format except we extend our own widget class. From here, if you want to return a view you must echo
it.
Note: Leaving empty form and update methods can break the widget
<?php namespace App\Widgets;
use App\News;
use LaraPress\Widgets\Widget;
class ExampleWidget extends Widget
{
protected $news;
function __construct()
{
parent::__construct(class_basename($this), 'News Widget');
$this->news = News::all();
}
// Output
function widget($args, $instance)
{
echo view('widgets.news', array(
'news' => $this->news,
'title => get_option('news-widget-title'),
));
}
// Admin Form
public function form($instance)
{
}
// Admin Form Save
public function update($new_instance, $old_instance)
{
}
}
Here is an example of how to save data to a widget. Use the following on a Widget class.
// Admin Form
public function form($instance)
{
echo view('admin.widgets.news', [
'name' => $this->get_field_name('news_widget_title'),
'value' => array_get($instance, 'news_widget_title'),
]);
}
// Admin Form Save
public function update($newInstance, $oldInstance)
{
return array_merge($old_instance, $new_instance);
}
The admin.widgets.news
blade file:
<p>
<label for="{{ $name }}">Title:</label>
<input class="widefat" id="{{ $name }}" name="{{ $name }}" value="{{ $value }}" />
</p>
@php(the_widget(\App\Widgets\ExampleWidget::class))