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.

I found this example of a shortcode on the Internet:

function project_shortcode( $atts, $content = null ) {
    extract( shortcode_atts( array(
        'class' => '',
        'id' => '',
        ), $atts ) );

        return '<div id="' . $id . '" class="' . $class . '">' . $content . '</div>';

}
add_shortcode('button', 'project_shortcode');

This is the output I want:

<div class="container item">
    <div class="row">
        <div class="intro twelvecol">
            <div class="top-border"></div>
        </div>
        <div class="screenshot eightcol">
            <img src="<?php bloginfo( 'template_url' ); ?>/images/studyatbest.png">
        </div>
        <div class="screenshot fourcol last">
            <h3>BEST LANGUAGE CENTER</h3>
            <p>BEST Language Center is an educational establishment in Taichung, Taiwan. I was asked to build and design a website they could use to offer classes, programs and display photos online.</p>
            <ul>
                <li>
                    <h4>ROLE</h4>
                    <p>Design, HTML/CSS, JavaScript</p>
                <li>
                    <h4>YEAR</h4>
                    <p>2010</p>
                </li>
                <li>
                    <h4>WEBSITE</h4>
                    <p><a href="http://studyatbest.com/">studyatbest.com</a></p>
                </li>
            </ul>
        </div>
    </div>
</div>

But I'm not sure how to do it so that I can return multiples lines in the shortcode (in order to keep the code clean). Any suggestions?

share|improve this question
    
what is your input for the shortcode –  phantom.omaga Dec 30 '11 at 9:17
add comment

2 Answers

up vote 1 down vote accepted

Don’t use output buffering. It is too slow and sometimes hard to debug. Use heredoc.

Example:

$output = <<<MYUNIQUENAME
    <div>$foo
      <p>Some $bar</p>
    </div>
MYUNIQUENAME;
share|improve this answer
add comment

Got it:

// Project shortcode
function project_shortcode( $atts, $content = null ) {
    extract( shortcode_atts( array(
        'client' => '',
        'screenshot' => '',
        'role' => '',
        'year' => '',
        'website' => '',
        ), $atts ) );

        ob_start();
    ?>

    <div class="container item">
        <div class="row">
            <div class="intro twelvecol">
                <div class="top-border"></div>
            </div>
            <div class="screenshot eightcol">
                <img src="<?php bloginfo( 'template_url' ); ?>/images/<?php echo $screenshot ?>">
            </div>
            <div class="screenshot fourcol last">
                <h3><?php echo $client ?></h3>
                <p><?php echo $content ?></p>
                <ul>
                    <li>
                        <h4>ROLE</h4>
                        <p><?php echo $role ?></p>
                    <li>
                        <h4>YEAR</h4>
                        <p><?php echo $year ?></p>
                    </li>
                    <li>
                        <h4>WEBSITE</h4>
                        <p><a href="http://<?php echo $website ?>"><?php echo $website ?></a></p>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <?php
        return ob_get_clean();
}
add_shortcode('project', 'project_shortcode');

(Please tell me if there is any bad practice in that code).

share|improve this answer
add comment

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.