Search code examples
phpwordpressshortcode

Adding content between a PHP shortcode


I have a shortcode in a theme that displays only when a user is logged out. Usually, I would use the following shortcode to achieve this

[logged_out]Content[/logged_out]

How would I achieve this in PHP?

<?php echo do_shortcode("[logged_out]"); ?>

Solution

  • Can you just try the code like below,

    <?php
    $string = 'Hello world';
    echo do_shortcode('[logged_out]'.$string.'[/logged_out]');
    ?>
    

    you can assign any value to the variable $string ;

    Or you can directly use like this,

    <?php
    echo do_shortcode('[logged_out]Hello world[/logged_out]');
    ?>
    

    For more information please refer,

    https://developer.wordpress.org/reference/functions/do_shortcode/