Shortcodes in C5?

Permalink 1 user found helpful
Hi,

A client came to me the other day and asked:

'why do I have to insert a YouTube block in-between two content blocks if I want a video to appear? That means if i want to edit content either side of it, I have double the workload (or more) if I have several YouTube and Content blocks together?'

So I told her that you can also insert YT vids via the WYSIWYG editor (and HTML editor), but she found that difficult so I wondered whether shortcodes (like in Wordpress) would be possible in C5?

That way, I could write a function that lets her do this:

[youtube url="http://www.youtube.com/?v=a7shnf"]

And she could place a video where she wants within text and move it easily.

I think this is great feature of WP, is something like that possible in C5?

Thanks

osu

osu
 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
I personally don't think this would be a good addition to the core system, as it's kind of an edge case that would complicate things for most people at the expense of helping a few. But that's just my opinion, for what it's worth.

Anyway, to make this work for your client you could override the Content block controller by copying the "controller.php" file out of concrete/blocks/content into a new folder at blocks/content (note that you're copying it out of the "concrete" folder into the plain old "blocks" folder in your site's root directory).
Now in that new controller.php file, find this code:
function getContent() {
   $content = $this->translateFrom($this->content);
   return $content;            
}

and modify it to something like this:
function getContent() {
   $content = $this->translateFrom($this->content);
   $content = preg_replace('/\[youtube url=\"(.*)?\"\]/Ui', '<a href="$1">$1</a>' ,$content);
   return $content;            
}

I'm using a regular expression to pull out a string that's formatted the way you stated -- you may need to play around with this to get it working just the way you want.

HTH!

-Jordan
osu replied on at Permalink Reply
osu
Thanks jordanlev,

That works for me.
surefyre replied on at Permalink Reply
surefyre
Am wondering the same myself as I'm with a day-to-day client who has a big site (several thousand pages) in Wordpress which 'seemed like a great choice at the time' (I'm encouraging them to move to C5).

Coding up a [shortcode] thingy though would, imho, introduce a security weakness into C5 although it's a useful thing to be able to do.

Perhaps creating a special view for a block which could parse shortcodes could be a halfway house.

I need to find a solution to this myself, anyway, so if I do I'll make sure the C5 community benefits from anything useful I create :o)

G