1 [**I am a developer: ** Developer API](#developer-api)
3 [**I am a template designer: ** Guide for template designers](#guide-for-template-designer)
9 ### What can I do with plugins?
11 The plugin system let you:
13 - insert content into specific places across templates.
14 - alter data before templates rendering.
15 - alter data before saving new links.
17 ### How can I create a plugin for Shaarli?
19 First, chose a plugin name, such as `demo_plugin`.
21 Under `plugin` folder, create a folder named with your plugin name. Then create a <plugin_name>.php file in that folder.
23 You should have the following tree view:
29 | |---| demo_plugin.php
32 ### Plugin initialization
34 At the beginning of Shaarli execution, all enabled plugins are loaded. At this point, the plugin system looks for an `init()` function to execute and run it if it exists. This function must be named this way, and takes the `ConfigManager` as parameter.
36 <plugin_name>_init($conf)
38 This function can be used to create initial data, load default settings, etc. But also to set *plugin errors*. If the initialization function returns an array of strings, they will be understand as errors, and displayed in the header to logged in users.
40 ### Understanding hooks
42 A plugin is a set of functions. Each function will be triggered by the plugin system at certain point in Shaarli execution.
44 These functions need to be named with this pattern:
47 hook_<plugin_name>_<hook_name>($data, $conf)
52 - data: see [$data section](https://shaarli.readthedocs.io/en/master/Plugin-System/#plugins-data)
53 - conf: the `ConfigManager` instance.
55 For example, if my plugin want to add data to the header, this function is needed:
57 hook_demo_plugin_render_header
59 If this function is declared, and the plugin enabled, it will be called every time Shaarli is rendering the header.
65 Every hook function has a `$data` parameter. Its content differs for each hooks.
67 **This parameter needs to be returned every time**, otherwise data is lost.
71 #### Filling templates placeholder
73 Template placeholders are displayed in template in specific places.
75 RainTPL displays every element contained in the placeholder's array. These element can be added by plugins.
77 For example, let's add a value in the placeholder `top_placeholder` which is displayed at the top of my page:
80 $data['top_placeholder'][] = 'My content';
82 array_push($data['top_placeholder'], 'My', 'content');
87 #### Data manipulation
89 When a page is displayed, every variable send to the template engine is passed to plugins before that in `$data`.
91 The data contained by this array can be altered before template rendering.
93 For exemple, in linklist, it is possible to alter every title:
96 // mind the reference if you want $data to be altered
97 foreach ($data['links'] as &$value) {
98 // String reverse every title.
99 $value['title'] = strrev($value['title']);
107 Every plugin needs a `<plugin_name>.meta` file, which is in fact an `.ini` file (`KEY="VALUE"`), to be listed in plugin administration.
109 Each file contain two keys:
111 - `description`: plugin description
112 - `parameters`: user parameter names, separated by a `;`.
113 - `parameter.<PARAMETER_NAME>`: add a text description the specified parameter.
115 > Note: In PHP, `parse_ini_file()` seems to want strings to be between by quotes `"` in the ini file.
117 ### It's not working!
119 Use `demo_plugin` as a functional example. It covers most of the plugin system features.
121 If it's still not working, please [open an issue](https://github.com/shaarli/Shaarli/issues/new).
125 | Hooks | Description |
126 | ------------- |:-------------:|
127 | [render_header](#render_header) | Allow plugin to add content in page headers. |
128 | [render_includes](#render_includes) | Allow plugin to include their own CSS files. |
129 | [render_footer](#render_footer) | Allow plugin to add content in page footer and include their own JS files. |
130 | [render_linklist](#render_linklist) | It allows to add content at the begining and end of the page, after every link displayed and to alter link data. |
131 | [render_editlink](#render_editlink) | Allow to add fields in the form, or display elements. |
132 | [render_tools](#render_tools) | Allow to add content at the end of the page. |
133 | [render_picwall](#render_picwall) | Allow to add content at the top and bottom of the page. |
134 | [render_tagcloud](#render_tagcloud) | Allow to add content at the top and bottom of the page, and after all tags. |
135 | [render_taglist](#render_taglist) | Allow to add content at the top and bottom of the page, and after all tags. |
136 | [render_daily](#render_daily) | Allow to add content at the top and bottom of the page, the bottom of each link and to alter data. |
137 | [render_feed](#render_feed) | Allow to do add tags in RSS and ATOM feeds. |
138 | [save_link](#save_link) | Allow to alter the link being saved in the datastore. |
139 | [delete_link](#delete_link) | Allow to do an action before a link is deleted from the datastore. |
145 Triggered on every page.
147 Allow plugin to add content in page headers.
151 `$data` is an array containing:
153 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
154 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
156 ##### Template placeholders
158 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
160 List of placeholders:
162 - `buttons_toolbar`: after the list of buttons in the header.
164 ![buttons_toolbar_example](http://i.imgur.com/ssJUOrt.png)
166 - `fields_toolbar`: after search fields in the header.
168 > Note: This will only be called in linklist.
170 ![fields_toolbar_example](http://i.imgur.com/3GMifI2.png)
174 Triggered on every page.
176 Allow plugin to include their own CSS files.
180 `$data` is an array containing:
182 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
183 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
185 ##### Template placeholders
187 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
189 List of placeholders:
191 - `css_files`: called after loading default CSS.
193 > Note: only add the path of the CSS file. E.g: `plugins/demo_plugin/custom_demo.css`.
197 Triggered on every page.
199 Allow plugin to add content in page footer and include their own JS files.
203 `$data` is an array containing:
205 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
206 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
208 ##### Template placeholders
210 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
212 List of placeholders:
214 - `text`: called after the end of the footer text.
215 - `endofpage`: called at the end of the page.
217 ![text_example](http://i.imgur.com/L5S2YEH.png)
219 - `js_files`: called at the end of the page, to include custom JS scripts.
221 > Note: only add the path of the JS file. E.g: `plugins/demo_plugin/custom_demo.js`.
225 Triggered when `linklist` is displayed (list of links, permalink, search, tag filtered, etc.).
227 It allows to add content at the begining and end of the page, after every link displayed and to alter link data.
231 `$data` is an array containing:
233 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
234 - All templates data, including links.
236 ##### Template placeholders
238 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
240 List of placeholders:
242 - `action_plugin`: next to the button "private only" at the top and bottom of the page.
244 ![action_plugin_example](http://i.imgur.com/Q12PWg0.png)
246 - `link_plugin`: for every link, between permalink and link URL.
248 ![link_plugin_example](http://i.imgur.com/3oDPhWx.png)
250 - `plugin_start_zone`: before displaying the template content.
252 ![plugin_start_zone_example](http://i.imgur.com/OVBkGy3.png)
254 - `plugin_end_zone`: after displaying the template content.
256 ![plugin_end_zone_example](http://i.imgur.com/6IoRuop.png)
260 Triggered when the link edition form is displayed.
262 Allow to add fields in the form, or display elements.
266 `$data` is an array containing:
268 - All templates data.
270 ##### Template placeholders
272 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
274 List of placeholders:
276 - `edit_link_plugin`: after tags field.
278 ![edit_link_plugin_example](http://i.imgur.com/5u17Ens.png)
282 Triggered when the "tools" page is displayed.
284 Allow to add content at the end of the page.
288 `$data` is an array containing:
290 - All templates data.
292 ##### Template placeholders
294 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
296 List of placeholders:
298 - `tools_plugin`: at the end of the page.
300 ![tools_plugin_example](http://i.imgur.com/Bqhu9oQ.png)
304 Triggered when picwall is displayed.
306 Allow to add content at the top and bottom of the page.
310 `$data` is an array containing:
312 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
313 - All templates data.
315 ##### Template placeholders
317 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
319 List of placeholders:
321 - `plugin_start_zone`: before displaying the template content.
322 - `plugin_end_zone`: after displaying the template content.
324 ![plugin_start_end_zone_example](http://i.imgur.com/tVTQFER.png)
328 Triggered when tagcloud is displayed.
330 Allow to add content at the top and bottom of the page.
334 `$data` is an array containing:
336 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
337 - All templates data.
339 ##### Template placeholders
341 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
343 List of placeholders:
345 - `plugin_start_zone`: before displaying the template content.
346 - `plugin_end_zone`: after displaying the template content.
348 For each tag, the following placeholder can be used:
350 - `tag_plugin`: after each tag
352 ![plugin_start_end_zone_example](http://i.imgur.com/vHmyT3a.png)
357 Triggered when taglist is displayed.
359 Allow to add content at the top and bottom of the page.
363 `$data` is an array containing:
365 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
366 - All templates data.
368 ##### Template placeholders
370 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
372 List of placeholders:
374 - `plugin_start_zone`: before displaying the template content.
375 - `plugin_end_zone`: after displaying the template content.
377 For each tag, the following placeholder can be used:
379 - `tag_plugin`: after each tag
383 Triggered when tagcloud is displayed.
385 Allow to add content at the top and bottom of the page, the bottom of each link and to alter data.
389 `$data` is an array containing:
391 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
392 - All templates data, including links.
394 ##### Template placeholders
396 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
398 List of placeholders:
400 - `link_plugin`: used at bottom of each link.
402 ![link_plugin_example](http://i.imgur.com/hzhMfSZ.png)
404 - `plugin_start_zone`: before displaying the template content.
405 - `plugin_end_zone`: after displaying the template content.
409 Triggered when the ATOM or RSS feed is displayed.
411 Allow to add tags in the feed, either in the header or for each items. Items (links) can also be altered before being rendered.
415 `$data` is an array containing:
417 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
418 - `_PAGE_`: containing either `rss` or `atom`.
419 - All templates data, including links.
421 ##### Template placeholders
423 Tags can be added in feeds by adding an entry in `$data['<placeholder>']` array.
425 List of placeholders:
427 - `feed_plugins_header`: used as a header tag in the feed.
431 - `feed_plugins`: additional tag for every link entry.
435 Triggered when a link is save (new link or edit).
437 Allow to alter the link being saved in the datastore.
441 `$data` is an array containing the link being saved:
456 Triggered when a link is deleted.
458 Allow to execute any action before the link is actually removed from the datastore
462 `$data` is an array containing the link being saved:
474 ## Guide for template designer
476 ### Plugin administration
478 Your theme must include a plugin administration page: `pluginsadmin.html`.
480 > Note: repo's template link needs to be added when the PR is merged.
482 Use the default one as an example.
484 Aside from classic RainTPL loops, plugins order is handle by JavaScript. You can just include `plugin_admin.js`, only if:
486 - you're using a table.
487 - you call orderUp() and orderUp() onclick on arrows.
488 - you add data-line and data-order to your rows.
490 Otherwise, you can use your own JS as long as this field is send by the form:
492 <input type="hidden" name="order_{$key}" value="{$counter}">
494 ### Placeholder system
496 In order to make plugins work with every custom themes, you need to add variable placeholder in your templates.
498 It's a RainTPL loop like this:
500 {loop="$plugin_variable"}
504 You should enable `demo_plugin` for testing purpose, since it uses every placeholder available.
506 ### List of placeholders
510 At the end of the menu:
512 {loop="$plugins_header.buttons_toolbar"}
516 At the end of file, before clearing floating blocks:
518 {if="!empty($plugin_errors) && isLoggedIn()"}
520 {loop="plugin_errors"}
528 At the end of the file:
531 {loop="$plugins_includes.css_files"}
532 <link type="text/css" rel="stylesheet" href="{$value}#"/>
538 At the end of your footer notes:
541 {loop="$plugins_footer.text"}
549 {loop="$plugins_footer.js_files"}
550 <script src="{$value}#"></script>
559 {loop="$plugins_header.fields_toolbar"}
564 Before displaying the link list (after paging):
567 {loop="$plugin_start_zone"}
572 For every links (icons):
575 {loop="$value.link_plugin"}
576 <span>{$value}</span>
583 {loop="$plugin_end_zone"}
588 **linklist.paging.html**
590 After the "private only" icon:
593 {loop="$action_plugin"}
603 {loop="$edit_link_plugin"}
613 {loop="$tools_plugin"}
623 <div id="plugin_zone_start_picwall" class="plugin_zone">
624 {loop="$plugin_start_zone"}
633 <div id="plugin_zone_end_picwall" class="plugin_zone">
634 {loop="$plugin_end_zone"}
645 <div id="plugin_zone_start_tagcloud" class="plugin_zone">
646 {loop="$plugin_start_zone"}
655 <div id="plugin_zone_end_tagcloud" class="plugin_zone">
656 {loop="$plugin_end_zone"}
667 <div id="plugin_zone_start_picwall" class="plugin_zone">
668 {loop="$plugin_start_zone"}
677 <div class="dailyEntryFooter">
678 {loop="$link.link_plugin"}
687 <div id="plugin_zone_end_picwall" class="plugin_zone">
688 {loop="$plugin_end_zone"}
694 **feed.atom.xml** and **feed.rss.xml**:
696 In headers tags section:
698 {loop="$feed_plugins_header"}
705 {loop="$value.feed_plugins"}