Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Simple short code example


Go to End


2 Posts   2869 Views

Avatar
WebSiteGuy

Community Member, 8 Posts

6 December 2010 at 2:10pm

Can someone please provide a simple shortcodes example. I have created several text fields in my siteconfig, such as SiteTextURL, which stores the domain name of the site. In a template I know I can call, $Siteconfig.SiteTextURL, but this doesn't work inside tinyMCE's htmleditor field (for $content). I'd like to create a simple shortcode I can insert into tinyMCE whenever I want the value of SiteTextURL to be shown.

I reviewed the "Using Short Codes to embed a YouTube video" tutorial on SSBits, but it seems overtly complex.

Avatar
Willr

Forum Moderator, 5523 Posts

6 December 2010 at 7:44pm

The ssbits tutorial is quite good.

In your case ($SiteTextURL) you would do something like

// mysite/_config.php
ShortcodeParser::get('default')->register('SiteTextURL', array('Page', 'SiteTextURL'));

That line adds a ShortCode of [SiteTextURL] (1st argument) and the 2nd one says it calls the SiteTextURL function on the Page class. So lets define that function SiteTextURL on Page.

// mysite/code/Page.php

class Page extends SiteTree {
...
..
function SiteTextURL() {
return SiteConfig::current_site_config()->SiteTextURL;
}

And that should be all you need. Simply include [SiteTextURL] in the TinyMCE editor in the backend and it should be translated when you call $Content.