0

This is my first time asking a question here. I'm kinda stuck on how to do echo do_shortcode(); inside a shortcode.

I tried googling for an answer but to no avail.

I'd like to know if it is possible to echo a shortcode inside a shortcode. If it is, any pointer is much appreciated.

Thank you.

    function cpa_haven_banner() {
        return '    
            <ul class="list-group list-group-horizontal entry-meta">
                <li class="list-group-item list-group-action-item">
                    <strong>Female</strong> <?php echo do_shortcode("[get_sheet_value location='LOAN!B7']"); ?>
                </li>
            </ul>
        ';
    }
    add_shortcode('cpa-haven', 'cpa_haven_banner');
1

This should work:

function cpa_haven_banner() {
    
    $shortcode = do_shortcode("[get_sheet_value location='LOAN!B7']");
    
    return '    
            <ul class="list-group list-group-horizontal entry-meta">
                <li class="list-group-item list-group-action-item">
                    <strong>Female</strong> ' . $shortcode .
                '</li>
            </ul>
        ';
    }
add_shortcode('cpa-haven', 'cpa_haven_banner');
1
  • Thank you. Appreciate the help ;) – yansusanto Aug 10 '20 at 13:52
0

I would put all the markup in a variable, and return the result of passing that through do_shortcode:

function cpa_haven_banner() {
    $html = '
        <ul class="list-group list-group-horizontal entry-meta">
            <li class="list-group-item list-group-action-item">
                <strong>Female</strong> [get_sheet_value location=\'LOAN!B7\']
            </li>
        </ul>
    ';


    return do_shortcode( $html );
}
add_shortcode('cpa-haven', 'cpa_haven_banner');
4
  • 1
    Thank you very much, Nate. That works! – yansusanto Aug 10 '20 at 13:51
  • You're welcome! Please accept the answer if this answer solved the problem. Thanks! – Nate Allen Aug 10 '20 at 20:32
  • I did accepted this answer but my reputation didn't allow me to do so. – yansusanto Aug 11 '20 at 5:43
  • @yansusanto Okay, no problem! Thanks! – Nate Allen Aug 11 '20 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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