Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hi to the community,
i try to build a shortcode that prints the Creative Commons logo and some text, everything is working fine except that i put the shortcode at the end of the post but the shortcode goes to the top of the post content,

i use the code bellow:

<?php // Creative Commons License Shortcode

function creativecommons( $atts, $content = null )
{
    extract( shortcode_atts( array(
      'type' => '',
      'code' => '',
      ), $atts ) );

{ ?>
<div class="license">
    <div class="cc-img">
        <img src="<?php bloginfo('template_directory'); ?>/images/cc.png" width="32" height="32" alt="Creative Commons License" />
    </div>
    <div class="cc-content">
        <h4><?php echo $type ?></h4>
        <small><?php echo $code ?></small>
    </div>
</div><!-- end of license -->
<div class="clear"></div><!-- clear div -->
<?php }
    return;
}
add_shortcode('cc1', 'creativecommons');
?>

I'm missing something here or something else have to do with my problem?
Thanks a lot,
Philip

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Couple things: I think your shortcode function should return the value rather than print it, and you shouldn't have the output wrapped in braces like that (the one just before the first div, and the one just before the return). Unless that's some obscure php syntax I've never seen (which is entirely possible)?

As far as I know, whenever you close a php tag--even inside a function--the parser will just immediately output the results, not return them.

share|improve this answer
    
Braces there are little strangely used, but not technically wrong (in many languages with C-like syntax braces can be inserted arbitrary as long as nesting makes sense). You are entirely correct that this function written for echoing result, but should be returning it. –  Rarst Jan 1 '11 at 7:29
1  
Thanks a lot @MathSmath and @Rarst, you are correct i have to return it in addition to the echo. –  Philip Jan 1 '11 at 8:57
    
Philip: Filters always need to return something. Actions do not. That are the two hook types in wordpress. Like function or method in any programming language. –  hakre Jan 1 '11 at 14:21

The only thing wrong with the braces in the function definition is that the closing brace is the same as the opening brace. The return statement is out of place. It should be within the function definition braces, and should look something like this (from W3 Schools):

function add($x,$y)
{
    $total=$x+$y;
    return $total;
}

echo "1 + 16 = " . add(1,16);

I am not sure what value you want returned.

Plus, the previous comment is correct. You cannot echo the values of variables defined in the function outside that function. I believe that variables defined in a function are local to that function. That is where the return statement comes into play.

Apparently, you want to return multiple values from your function. Since a function cannot return more than one value, you can do this with an array. You can then retrieve your values outside the function.

See following example I picked up on the web.

function small_numbers()
{
    return array (0, 1, 2);
}

list ($zero, $one, $two) = small_numbers();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.