User Defined Tags

Contents

Share Your tags

Please share your tags here.

Short version

User-defined tags are a simple way to insert PHP code into your site. To use them, simply create a new tag and paste your PHP code in (leaving "<?php" and "?>" out). You can call tags with smarty like this: {tag_name}

TIP: If you like to edit your code from an outside editor so you may have syntax highlighting you may use ?><? at the start of your UDT to keep the highlighting without throwing an error when trying to save the UDT.

Standard installation has one tag already defined which you can use with {user_agent} (guess what it does :)

Parameters for tags

You can pass parameters to tags for example: "{image_link src='hedgehog'}" and tag code
  echo '<a href="' . $params['src'] . '.jpg" target="_blank">';
  echo '<img src="t_' . $params['src'] . '.jpg"></a>';

Long Version

How to Make a User Defined Tag

Like most things in CMS Made Simple, adding a new plug-in is simple, although it's not quite like a holiday.
To add your own plugin follow these steps:
  1. The plugin editor is in the back-end so you need to login as admin, or a user with appropriate permissions.
  2. In the Admin Console click on "Extensions/User Defined Tags" in the top drop-down menu.
  3. At the bottom of the page click 'Add User Defined Tag'.
  4. In the 'Name' text-box type the name of the tag (it can only contain letters, numbers and underscores). This is what you'll have to type in parenthesies to add a tag into a page so be descriptive but don't make it long.
  5. In the 'Code' text-box type the php code that the tag will be replaced with when the page is requested. (Check the next section for more info)
  6. Click on the 'Submit'.

Your First UDT

Instead of me rambling on about every last detail of plugins and php a simple example should help you get started. Following in the footsteps of all introductions to programming we'll try a hello world script first.
Follow the steps above to create a new plug-in, 'Name' it "helloworld" (no quotes) and in the 'Code' box type/paste this code...
  echo 'Hello World!';
Click 'Submit'. To test the module, create a new 'Content Page' (see Add Content), type "{helloworld}" (no quotes) somewhere in the body and then click 'Preview'. Instead of seeing {helloworld} you should see "Hello World!".
The echo command just writes what's in the quotes so we can also use this to add HTML, or even DHTML or Javascript objects. Try out this code, just edit the plugin we made by clicking on the edit icon next to it on the 'Plugin Managment' page. Try this code...
  echo '<h3>Hello World!</h3>';
Test it in the same way as with the last one, you should now see something that looks a bit like this:

Hello World!

"But you can do all that with a html blob!" I hear you cry. Well, plugins get really useful when you start to add parameters. Parameters alow you to specify something in the tag. Let's say that we wanted to say hello to someone called Bob, try this code...
  echo '<h3>Hello ' . $params['name'] . '!</h3>';
This had added a parameter, "name", to the plugin, the contents of that parameter will be put there when the plugin is called from a tag in a page. Test it in the same way as before, but instead of using "{helloworld}" use "{helloworld name='Bob'}" as the tag to define the name parameter. You should see something like this...

Hello Bob!

That just about covers writing basic plugins, you can do some useful things with the echo command, parameters and a little imagination so just think what you can do if you learn a little PHP...

Passing the page content as a parameter

You can access the page content in a user defined tag by passing it as a parameter:
In your template:
  {content assign=pagecontent}
  {table_of_contents thepagecontent="$pagecontent"}
In your user defined tag named "table_of_contents":
  echo $params['thepagecontent']; // Display page content.
I use this so I can parse to the page content to automatically create a table of contents.

How to get the URL of a page from its content_id

This assumes your page content_id is in a variable named $page_content_id
  $hm = cmsms()->GetHierarchyManager();
  $curnode = $hm->getNodeById($page_content_id);
  $curcontent = $curnode->GetContent();
  echo 'Page URL: ' . $curcontent->GetURL();

How to provide variables from a UDT to your page/template

Sometimes you need to provide calculated or processed information via PHP variables from a UDT to a page or template. To do this add a new UDT and call it e.g. "Var" and fill in the code below

$smarty = cmsms()->GetSmarty();
$foo= "test"; // Variable you want to provide
$smarty->assign('foo', $foo); // make variable "foo" visible for CMSms

Now you are able to use the variable in pages or templates via Smarty:

{Var} {* Call the UDT "Var" before using its variable(s) *}
<p>The content of the variable "foo" is {$foo}.</p>

Output:

The content of the variable "foo" is test.

In the section Share your tags here you will find a more reasonable example (see "Check for expire dates")
This page in: English - Deutsch - Español - Français - Italiano - Lietuvių - Nederlands - Norsk - Polski - Česky - Русский - Svenska - Tiếng Việt - عربي - 日本語 简体中文

User Handbook/Admin Panel/Extensions/User Defined Tags

From CMSMS

Arvixe - A CMSMS Partner