Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
24 posts Page 1 of 3
I often embed videos on my Couch generated pages. They can come from YouTube, or sometimes they're flash movies served by other sites. Other times I serve my own HTML5 video using the <video> tag. But they all have one thing in common. Each has a snippet of code you use to embed the movie on your site.

So I created an all-purpose "embed code" shortcode to allow me to easily include these snippets in richtext areas of pages.

The Couch documentation (http://www.couchcms.com/docs/miscellane ... codes.html) includes a collection of shortcode examples for many different uses, including embedding youtube videos, iframes, Google maps, and other bits of specialized code. I do all these things on my sites, but not often enough to remember how to use the specific shortcode each time.

With this all-purpose shortcode, I can add nearly any snippet of code to a page: embedded videos, iframes, HTML5 tags… just about anything. And do it with a single shortcode. Usage is simple:
Code: Select all
[embed code='<p>Any snippet of code goes here.</p>']

There are some limitations:
1. Couch scrubs the word "script" for security purposes. For instance a flash movie with the parameter "allowscriptaccess" will break.
2. No new lines. Put everything on a single line.
3. Quotation marks. Be careful of your quotation mark types. You can accidentally close the shortcode prematurely.

Where I can't work around these issues, I create a one-off shortcode for that code with its own unique name.

These two together - the all-purpose embed shortcode and one-off shortcodes - allow me to easily add any bit of code I need into a Couch-based page. There's nothing fancy about it, but it's very handy if you hadn't already thought of it yourself.

Code: Select all
   // All-Purpose Embed Shortcode
   // Embed any code (almost).
   // Careful of your quotation mark types.
   // Won't accept the word "script."  No new lines. PHP code won't work.
   // Usage: [embed code='<p>Any code goes here.</p>']
   $FUNCS->register_shortcode( 'embed', 'embed_handler' );
   function embed_handler( $params, $content=null ){
      global $FUNCS;

      extract( $FUNCS->get_named_vars(array(
         'code' => '',
      ), $params) );

       // Pass on the code to Couch for execution using the 'embed' function
      return $FUNCS->embed( $code, $is_code=1 );
   }

EDIT: I updated this shortcode to pass the embed code through Couch's 'embed' function. D'uh... :roll:
Maybe not a big deal for clients, but on my own sites, I can now add couch tags for things like internal links, or even embed php with the cms:php tag.
Works like a charm. Thank you, I have been looking for a solution like that for some time.
Trying to use this shortcode and I get this error on kfunctions.php:


Fatal error: Call to a member function register_shortcode() on a non-object in /nfs/c09/h05/mnt/210212/domains/XXXX.com/html/kfunctions.php on line 2

Code: Select all
<?php
$FUNCS->register_shortcode( 'embed', 'embed_handler' );
   function embed_handler( $params, $content=null ){
      global $FUNCS;

      extract( $FUNCS->get_named_vars(array(
         'code' => '',
      ), $params) );
       return $code;
   }
Call to a member function register_shortcode() on a non-object

What this error is telling us is that the $FUNCS object is out of scope at the time that kfunctions.php is being executed. It's saying there is no such object when it gets to
Code: Select all
$FUNCS->register_shortcode( 'embed', 'embed_handler' );

Why that would be, I can only imagine. I can't reproduce it on my own system.

Have you tried using any other shortcodes? Is it just for this particular code that you get the error, or for all shortcodes?
It seems to be with all shortcodes. I uploaded the kfunctions.php file from the tutorial and still got the same error.
I suspect that something is missing or corrupted in your Couch core files. If you have a different installation of Couch, you could test shortcode functionality there to see if you get the same problem.

I think I would try to see if I can get shortcodes working in a different or new Couch installation. If so, then maybe try replacing the Couch core files as if you were upgrading.

This might be a good place for @KK to step in and save the day, though. :)
As far as I know, nothing has changed in the way kfunctions.php file is brought into the execution chain. So it is unusual to find $KFuncs object not being in reference at that point.

@tonjaggart, if your setup happens to be online, please get me its FTP+Couch access and I'll try to see if I can find the reason for this problem.
@tonjaggart, thanks for entrusting with the access creds.

I had a look and tried using this all-purpose shortcode and it worked just fine. You can see one in action on the 'custom-product' page. I have taken the liberty to add the following line (you can remove it) in the template near the footer -
Code: Select all
<cms:do_shortcodes>[embed code='<h2>Any snippet of code goes here.</h2>']</cms:do_shortcodes>

So, I'm afraid I couldn't duplicate the issue.
If you are still experiencing it, please PM me the exact template/page where you are using the shortcode and getting the error.
Thanks for looking into it KK. I am still having trouble actually having a YouTube video show on richtext. This is the code I used, but it just displays the code instead of the video embed:

Code: Select all
<cms:do_shortcodes>[embed code='<iframe width="853" height="480" src="https://www.youtube.com/embed/r_ML1PvSZzQ" frameborder="0" allowfullscreen></iframe>']</cms:do_shortcodes>

I am still having trouble actually having a YouTube video show on richtext.

I take it this means you are using an editable region of type "richtext". On the front end you would show this editable region as
Code: Select all
<cms:do_shortcodes><cms:show my_richtext /></cms:do_shortcodes>

Then you would place the embed code in the WYSIWYG editor, on a line by itself.
Code: Select all
[embed code='<iframe width="853" height="480" src="https://www.youtube.com/embed/r_ML1PvSZzQ" frameborder="0" allowfullscreen></iframe>']

Does this detail correct the issue for you?
24 posts Page 1 of 3