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. |
140 | [save_plugin_parameters](#save_plugin_parameters) | Allow to manipulate plugin parameters before they're saved. |
146 Triggered on every page.
148 Allow plugin to add content in page headers.
152 `$data` is an array containing:
154 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
155 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
157 ##### Template placeholders
159 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
161 List of placeholders:
163 - `buttons_toolbar`: after the list of buttons in the header.
165 ![buttons_toolbar_example](http://i.imgur.com/ssJUOrt.png)
167 - `fields_toolbar`: after search fields in the header.
169 > Note: This will only be called in linklist.
171 ![fields_toolbar_example](http://i.imgur.com/3GMifI2.png)
175 Triggered on every page.
177 Allow plugin to include their own CSS files.
181 `$data` is an array containing:
183 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
184 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
186 ##### Template placeholders
188 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
190 List of placeholders:
192 - `css_files`: called after loading default CSS.
194 > Note: only add the path of the CSS file. E.g: `plugins/demo_plugin/custom_demo.css`.
198 Triggered on every page.
200 Allow plugin to add content in page footer and include their own JS files.
204 `$data` is an array containing:
206 - `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.).
207 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
209 ##### Template placeholders
211 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
213 List of placeholders:
215 - `text`: called after the end of the footer text.
216 - `endofpage`: called at the end of the page.
218 ![text_example](http://i.imgur.com/L5S2YEH.png)
220 - `js_files`: called at the end of the page, to include custom JS scripts.
222 > Note: only add the path of the JS file. E.g: `plugins/demo_plugin/custom_demo.js`.
226 Triggered when `linklist` is displayed (list of links, permalink, search, tag filtered, etc.).
228 It allows to add content at the begining and end of the page, after every link displayed and to alter link data.
232 `$data` is an array containing:
234 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
235 - All templates data, including links.
237 ##### Template placeholders
239 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
241 List of placeholders:
243 - `action_plugin`: next to the button "private only" at the top and bottom of the page.
245 ![action_plugin_example](http://i.imgur.com/Q12PWg0.png)
247 - `link_plugin`: for every link, between permalink and link URL.
249 ![link_plugin_example](http://i.imgur.com/3oDPhWx.png)
251 - `plugin_start_zone`: before displaying the template content.
253 ![plugin_start_zone_example](http://i.imgur.com/OVBkGy3.png)
255 - `plugin_end_zone`: after displaying the template content.
257 ![plugin_end_zone_example](http://i.imgur.com/6IoRuop.png)
261 Triggered when the link edition form is displayed.
263 Allow to add fields in the form, or display elements.
267 `$data` is an array containing:
269 - All templates data.
271 ##### Template placeholders
273 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
275 List of placeholders:
277 - `edit_link_plugin`: after tags field.
279 ![edit_link_plugin_example](http://i.imgur.com/5u17Ens.png)
283 Triggered when the "tools" page is displayed.
285 Allow to add content at the end of the page.
289 `$data` is an array containing:
291 - All templates data.
293 ##### Template placeholders
295 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
297 List of placeholders:
299 - `tools_plugin`: at the end of the page.
301 ![tools_plugin_example](http://i.imgur.com/Bqhu9oQ.png)
305 Triggered when picwall is displayed.
307 Allow to add content at the top and bottom of the page.
311 `$data` is an array containing:
313 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
314 - All templates data.
316 ##### Template placeholders
318 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
320 List of placeholders:
322 - `plugin_start_zone`: before displaying the template content.
323 - `plugin_end_zone`: after displaying the template content.
325 ![plugin_start_end_zone_example](http://i.imgur.com/tVTQFER.png)
329 Triggered when tagcloud is displayed.
331 Allow to add content at the top and bottom of the page.
335 `$data` is an array containing:
337 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
338 - All templates data.
340 ##### Template placeholders
342 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
344 List of placeholders:
346 - `plugin_start_zone`: before displaying the template content.
347 - `plugin_end_zone`: after displaying the template content.
349 For each tag, the following placeholder can be used:
351 - `tag_plugin`: after each tag
353 ![plugin_start_end_zone_example](http://i.imgur.com/vHmyT3a.png)
358 Triggered when taglist is displayed.
360 Allow to add content at the top and bottom of the page.
364 `$data` is an array containing:
366 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
367 - All templates data.
369 ##### Template placeholders
371 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
373 List of placeholders:
375 - `plugin_start_zone`: before displaying the template content.
376 - `plugin_end_zone`: after displaying the template content.
378 For each tag, the following placeholder can be used:
380 - `tag_plugin`: after each tag
384 Triggered when tagcloud is displayed.
386 Allow to add content at the top and bottom of the page, the bottom of each link and to alter data.
390 `$data` is an array containing:
392 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
393 - All templates data, including links.
395 ##### Template placeholders
397 Items can be displayed in templates by adding an entry in `$data['<placeholder>']` array.
399 List of placeholders:
401 - `link_plugin`: used at bottom of each link.
403 ![link_plugin_example](http://i.imgur.com/hzhMfSZ.png)
405 - `plugin_start_zone`: before displaying the template content.
406 - `plugin_end_zone`: after displaying the template content.
410 Triggered when the ATOM or RSS feed is displayed.
412 Allow to add tags in the feed, either in the header or for each items. Items (links) can also be altered before being rendered.
416 `$data` is an array containing:
418 - `_LOGGEDIN_`: true if user is logged in, false otherwise.
419 - `_PAGE_`: containing either `rss` or `atom`.
420 - All templates data, including links.
422 ##### Template placeholders
424 Tags can be added in feeds by adding an entry in `$data['<placeholder>']` array.
426 List of placeholders:
428 - `feed_plugins_header`: used as a header tag in the feed.
432 - `feed_plugins`: additional tag for every link entry.
436 Triggered when a link is save (new link or edit).
438 Allow to alter the link being saved in the datastore.
442 `$data` is an array containing the link being saved:
457 Triggered when a link is deleted.
459 Allow to execute any action before the link is actually removed from the datastore
463 `$data` is an array containing the link being saved:
476 #### save_plugin_parameters
478 Triggered when the plugin parameters are saved from the plugin administration page.
480 Plugins can perform an action every times their settings are updated.
481 For example it is used to update the CSS file of the `default_colors` plugins.
485 `$data` input contains the `$_POST` array.
487 So if the plugin has a parameter called `MYPLUGIN_PARAMETER`,
488 the array will contain an entry with `MYPLUGIN_PARAMETER` as a key.
491 ## Guide for template designer
493 ### Plugin administration
495 Your theme must include a plugin administration page: `pluginsadmin.html`.
497 > Note: repo's template link needs to be added when the PR is merged.
499 Use the default one as an example.
501 Aside from classic RainTPL loops, plugins order is handle by JavaScript. You can just include `plugin_admin.js`, only if:
503 - you're using a table.
504 - you call orderUp() and orderUp() onclick on arrows.
505 - you add data-line and data-order to your rows.
507 Otherwise, you can use your own JS as long as this field is send by the form:
509 <input type="hidden" name="order_{$key}" value="{$counter}">
511 ### Placeholder system
513 In order to make plugins work with every custom themes, you need to add variable placeholder in your templates.
515 It's a RainTPL loop like this:
517 {loop="$plugin_variable"}
521 You should enable `demo_plugin` for testing purpose, since it uses every placeholder available.
523 ### List of placeholders
527 At the end of the menu:
529 {loop="$plugins_header.buttons_toolbar"}
533 At the end of file, before clearing floating blocks:
535 {if="!empty($plugin_errors) && isLoggedIn()"}
537 {loop="plugin_errors"}
545 At the end of the file:
548 {loop="$plugins_includes.css_files"}
549 <link type="text/css" rel="stylesheet" href="{$value}#"/>
555 At the end of your footer notes:
558 {loop="$plugins_footer.text"}
566 {loop="$plugins_footer.js_files"}
567 <script src="{$value}#"></script>
576 {loop="$plugins_header.fields_toolbar"}
581 Before displaying the link list (after paging):
584 {loop="$plugin_start_zone"}
589 For every links (icons):
592 {loop="$value.link_plugin"}
593 <span>{$value}</span>
600 {loop="$plugin_end_zone"}
605 **linklist.paging.html**
607 After the "private only" icon:
610 {loop="$action_plugin"}
620 {loop="$edit_link_plugin"}
630 {loop="$tools_plugin"}
640 <div id="plugin_zone_start_picwall" class="plugin_zone">
641 {loop="$plugin_start_zone"}
650 <div id="plugin_zone_end_picwall" class="plugin_zone">
651 {loop="$plugin_end_zone"}
662 <div id="plugin_zone_start_tagcloud" class="plugin_zone">
663 {loop="$plugin_start_zone"}
672 <div id="plugin_zone_end_tagcloud" class="plugin_zone">
673 {loop="$plugin_end_zone"}
684 <div id="plugin_zone_start_picwall" class="plugin_zone">
685 {loop="$plugin_start_zone"}
694 <div class="dailyEntryFooter">
695 {loop="$link.link_plugin"}
704 <div id="plugin_zone_end_picwall" class="plugin_zone">
705 {loop="$plugin_end_zone"}
711 **feed.atom.xml** and **feed.rss.xml**:
713 In headers tags section:
715 {loop="$feed_plugins_header"}
722 {loop="$value.feed_plugins"}