# Snippets for plugins

**URL:** <https://forum.getkirby.com/t/snippets-for-plugins/1826>\
**Category:** Solutions\
**Tags:** v2\
**Created:** [August 14, 2015, 2:13pm UTC](https://forum.getkirby.com/t/snippets-for-plugins/1826 "2015-08-14T14:13:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [August 14, 2015, 2:13pm UTC](https://forum.getkirby.com/t/snippets-for-plugins/1826/1 "2015-08-14T14:13:41Z")

</div>

Snippets for templates are awesome. In some cases snippets can be needed for plugins as well. Maybe we don’t want to mix template snippets with plugin snippets. Instead we keep the plugin snippets in the plugin folder.

## Snippets for plugins - Solution

Very simple, but powerful, based on the snippet function in the Kirby core. Change your function name to something unique.

This goes into your plugin file `site/plugins/my_plugin/my_plugin.php` or any other file called by the plugin:

```php
function pluginSnippet( $file, $data = array(), $return = false ) {
	$plugin = 'my_plugin';
	$folder = 'snippets';
	if( is_object( $data ) ) $data = array( 'item' => $data );
	return tpl::load( kirby()->roots()->plugins() . DS . $plugin . DS . $folder . DS . $file . '.php', $data, $return );
}

echo pluginSnippet( 'test', array('testing' => 'Hoho'), true );

```

### Snippet

This code goes into the `site/plugins/my_plugin/snippets/test.php`:

```php
<h1><?php echo $testing; ?></h1>

```

The output would be:

```html
 <h1>Hoho</h1>

```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:27pm UTC](https://forum.getkirby.com/t/snippets-for-plugins/1826/3 "2025-02-06T17:27:52Z")

</div>


