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 2 of 3
Thank you!
Hello:

I am trying to master shortcodes and am a bit confused. I get the php part and even the basic logic.

Content of my kfunctions.php file which is in the CouchCMS root:

<?php

$FUNCS->register_shortcode( 'hello', 'hello_handler' );

function hello_handler( $params, $content=null ){
return '<h1>Hello from a shortcode!</h1>';
}

I'm new to php but doesn't there need to be a close tag? ?>

What I don't get is where to put these tags.

I have a template (basic HTML file structure) that has an editable region:

<cms:editable
name='main_content'
label='Content'
type='richtext'
order='2'
>

[hello]

Custom HTML here

</cms:editable>

I tried wrapping the editable tags with <cms:do_shortcodes>...</cms:do_shortcodes> and then writing the [hello] inside the editable area to test it. It doesn't render on the page.

Do I need to place the code within the template tags at the top?:

<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Contact Us' order='10'>

<cms:do_shortcodes>

<cms:show main_content />

</cms:do_shortcodes>

</cms:template>

I tried this but it isn't working either.

I'm not a PHP guru and would appreciate it if I didn't receive any flames for what might be a stupid question. I'm missing something major even though I thoroughly read the shortcodes documentation page. Please don't just point me there.

Any help would be appreciated.

Thanks.
@TFG, I've posted an explanation in your other thread on the same topic - viewtopic.php?p=23300#p23300

Hope it helps.
Hi TFG. @KK gave a thorough answer to your question, but didn't touch on the bit about the php close tag. It's a pretty trivial question, but was surprising to me, too. You don't need the closing php tag. I guess when the code comes to the end, it stops executing. Period.

PHP is usually mixed with html, and the closing tag would be necessary to separate it from html in a document.
Code: Select all
<html>
   <body>
      <h1>
         <?php echo 'Hi there, world.'; ?>
      </h1>
   </body>
</html>

But just to come to a stop, or as in this case to go on to more php code, you don't need it. As far as I know, none of the Couch files have closing php tags just to end the file. Maybe such a reckless disregard for the closing tag is just one of @KK's stylistic flourishes. Nonetheless, it works.
@tim, not using the closing PHP tag is, in fact, a recommended practice.

The reason being that it is very easy to inadvertently leave a white-space after the closing tag, if you choose to use one.

This, of course, will only provide yet another avenue for the dreaded 'Cannot modify header information - headers already sent" error we encounter only too often (viewtopic.php?f=4&t=53).
Hi, I am trying to use the shortcode (I have shortcodes enabled and working and am using youtube ones for example).
It doesn't work for some reason...

What I am trying to do is to incorporate javascript into modal with:

Code: Select all
$(document).ready(function() {

  if(window.location.href.indexOf('#myModal') != -1) {
    $('#myModal').modal('show');
  }

});


Therefore, I am putting:

Code: Select all
[embed code='<p>$(document).ready(function() {  if(window.location.href.indexOf('#article-42') != -1) { $('#article-42').modal('show'); } });</p>']


This way I am trying to be able to share a link which will directly open the modal.
In principle, the method works - if I put the javascript in the site page itself and after open the modal's URL it opens correctly.

However, when I use the above shortcode, all I get is the script as a text in the modal without "[embed code= ]" and the link does not work.

What am I getting wrong?
Please try this -
Code: Select all
[embed code="<p>$(document).ready(function() {  if(location.href.indexOf('#article-42') != -1) { $('#article-42').modal('show'); } });</p>"]
Hi. I think what you're getting wrong is that you've placed your JavaScript inside of a <p> tag. Perhaps the example in the documentation - which simply embeds a paragraph - was confusing. Try removing the <p> tag from your shortcode.
Hi, none of the approaches works - using ", using ', using ' or " in the whole code, removing the <p> tags.
No luck.
@aztk,
Sorry that you've been frustrated by this. What you can do is compare what happens when you put the code directly on the site page to the rendered page when you use the shortcode. If you take a look at the source code of the rendered page, I think the problem will become obvious.

I'm wondering where you are embedding this code on the page. Is it in an existing script tag? It doesn't seem like it, because the text of the shortcode is being rendered directly on the page. When you put the javascript directly on the page, you are putting it inside a script tag. Where is the script tag when you use the shortcode?

You can't use the word "script" in the All-Purpose Embed shortcode. This might be a case where you want to use a one-off shortcode. For example:
Code: Select all
// My Modal Shortcode
   // ordinary All-Purpose Embed shortcode defaces the word "script" for security purposes
   // Usage: [my_modal_shortcode]
   $FUNCS->register_shortcode( 'my_modal_shortcode', 'my_modal_shortcode_handler' );
   function my_modal_shortcode_handler( $params, $content=null ){
      return "<script>$(document).ready(function() {  if(window.location.href.indexOf('#article-42') != -1) { $('#article-42').modal('show'); } });</script>";
   }

Then use the shortcode "[my_modal_shortcode]" wherever you want to inject the script.
24 posts Page 2 of 3
cron