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.

Data Model Questions /

Simple short code example


Reply


2 Posts   1792 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, 5513 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.