How to create a widget

Posted on March 24, 2021

In this tutorial we will create a widget that displays the last tweets of an account. Instead of using an event to run the code, we let the administrator create instances of the widget and choose in which widget area wants to display the plugin. Inside src/ create a folder twitter-timelines and add the following files:



  "name":"Twitter Timelines",
  "description":"Installs a widget to display twitter timelines."


// registers the widget name and its path 
Config::widgets([ 'twitter-timeline'=>'twitter-timelines/widgets/twitter-timeline' ]); 

widgets/twitter-timeline/widget.php will include the widget options we want to use. In this case we need the user account and the name to be displayed.


return [
   'accountID'=>[ 'title'=>'Twitter Account' ]

widgets/twitter-timeline/twitter-timeline.php is the view file of the widget, it will generate the html code. We use the embedding Twitter content from here

$account = Config::getOption('twitter-timelines.accountID','gilacms'); 
<a class="twitter-timeline" data-height="400" href="<?=$account?>">Tweets by <?=$account?></a> 
<script async src="" charset="utf-8"></script>

Config::getOption() gets the option of the package that we set up in the package settings. A default value can be used if the option is null.


Activate the package.

Now in /admin/widgets you can create a new widget with type twitter-timeline and set the widget area sidebar or dashboard to see it.