0

I'm starting to work on creating a WordPress shortcode and am having difficulty understanding how to display the output specifically using 'Return', the code I have is

if (!function_exists('kfl')) {
    function kfl( $atts, $content = null ) {
        extract(shortcode_atts(array(
            'title'      => 'Title goes here',
        ), $atts));

        return "<div class=\"container panel\">";
        return "<h3 class=\"kfl title\">".$title."</h3>";
        return "</div>";
    }
    add_shortcode('kfl', 'kfl');
}

?>

When I use this nothing is displayed, if I remove the second Return line and add the .$title to the first line I get the container panel and unstyled title. If I replace the Returns with Echo it all works fine, but I assume there must be a reason why 'return' must is used (in most tuts).

Any advice or guidance would be most appreciated.

Thanks

1
  • No lines of code are executed after return. Commented Nov 13, 2013 at 18:38

4 Answers 4

1

Usually, a function that is registered as a shortcode, has a variable, in which you gradually insert code and then return it. In your case:

if (!function_exists('kfl')) {
    function kfl( $atts, $content = null ) {
        extract(shortcode_atts(array(
            'title'      => 'Title goes here',
        ), $atts));

        $output = "<div class=\"container panel\">";

        $output .= "<h3 class=\"kfl title\">".$title."</h3>";

        $output .= "</div>";

        return $output;
    }
    add_shortcode('kfl', 'kfl');
}
Sign up to request clarification or add additional context in comments.

Comments

1

the problem in your code comes here :

    return "<div class=\"container panel\">";
    return "<h3 class=\"kfl title\">".$title."</h3>";
    return "</div>";

when the 1st return is processed the control will leave the function so the 2nd and 3rd returns won't get executed

if you need to return all of them, then concatenate all of them in a single variable and return it in the end like this :

$return = "<div class=\"container panel\"><h3 class=\"kfl title\">".$title."</h3></div>";
return $return ;

else if you just want to display all of them then use this :

    echo "<div class=\"container panel\">";
    echo "<h3 class=\"kfl title\">".$title."</h3>";
    echo "</div>";

Comments

0

Once execution hits a return, it leaves the function. So putting three returns in a row means that the second and third never get executed. You can do something like this, using the string concatenation operator to join the three parts into one string:

function foo() {
    // blah blah blah
    return
        '...' .
        '...' .
        '...';
}

4 Comments

Could I just stick with using echo for all three lines?
Depends how the function is called. Returning a value doesn't output it anywhere. I.e., if your function returns a string and you do echo foo() then it will echo the value that foo() returns. On the other hand, if you use echo in the function, you don't need to call echo when you invoke the function, you only need to call it via foo().
in shortcode callback functions you need to return the output
@diggy Thanks for the clarification, I don't know WP that well off the top of my head.
0

You can concatenate elements with . and return the result at the end:

$html = '';
$html .= '<div class="container panel">';
$html .= '<h3 class="kfl title">' . $title . '</h3>';
$html .= '</div>';
return $html;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.