I noticed both echo
and return
works fine for displaying content from a shortcode function in wordpress.
function foobar_shortcode($atts) {
echo "Foo Bar"; //this works fine
}
function foobar_shortcode($atts) {
return "Foo Bar"; //so does this
}
Is there any difference between using either of these? If yes, what's the recommended approach for wordpress? I normally use echo
in this case - is this okay?
// Enable the use of shortcodes in text widgets. // Use shortcodes in form like Landing Page Template. // Store the short code in a variable. Square brackets are required to echo the shortcode, not just the name of the shortcode. This is not clear in the example.
If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues when plugins are disabled but the shortcode will still show up in the post or content. (string) (Required) Content to search for shortcodes.
Search content for shortcodes and filter shortcodes through their hooks. If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues when plugins are disabled but the shortcode will still show up in the post or content.
If you are outputting a lot of contents, then you should use:
add_shortcode('test', 'test_func');
function test_func( $args ) {
ob_start();
?>
<!-- your contents/html/(maybe in separate file to include) code etc -->
<?php
return ob_get_clean();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With