Blog

Pligg’s template uses Template Lite (a template system based on Smarty), and one of the built-in features for this system is that it comes with plugin functionality. This allows you to make use of a number of pre-installed plugins (located in the /plugins directory), or you can even add your own plugins. In this post I’ll be demonstrating how you can add a new template plugin to return an embedded Youtube video. This is a very quick example that should provide enough direction for you to craft your own template plugin for custom functions.

The plugin that we will be creating in this guide will scan the contents of a template variable and convert any Youtube URL into an embedded Youtube video. Now let’s begin by creating a new file located at /plugins/modifier.youtube.php. Inside of this file we are going to write a regular expression to find the Youtube URL within the string (the template variable content). We then define that we want to replace the Youtube URL with an embedded video.

Next we need to make use of our new modifier by using it in the link_summary.tpl file. This is the template file where Pligg stores the data for an individual story (ex. Title, URL, Description, Author, Voting, etc.). Open up the file and find where you would like to embed a Youtube video. One popular location is either directly before or after the “$story_content” template variable, where the story description is called. Paste in the code below when you’ve found a good location:

The first and last lines of the code above check to see if the URL for this article is from either Youtube.com or Youtu.be (Youtube’s URL shortening domain). If the {$url} value were to be from another domain, then it would only return a URL in text form and we don’t want that. The middle line is where we make use of the Youtube modifier. Save the file and clear your template cache. Any stories that used a Youtube.com or Youtu.be video URL should now return an embedded video wherever you placed the code block above.

If you want to get a little creative, you can use the same modifier on the story description area by using {$story_content|youtube}. Any Youtube URLs in the story description will be converted into embedded videos. You can also stack modifiers to apply more than one to a template variable. Here’s an example: {$story_content|nl2br|youtube}