Best way to output HTML

[ad_1]

Hey everyone,

what is the best way to output html via a shortcode?

Variant 1:

function shortcode_callback(){
?>
<p>This is my HTML :)</p>
<?php
}

Variant 2:

function shortcode_callback(){
   ob_start();
   ?>
   <p>THis is my HTML</p>
   <?php
   $output = ob_end_clean();
   return $output;

Variant 3:

function shortcode_callback(){
   $html="<p> this is my Html</p>
            <div>over multiple lines</div>";
   return $html;
}

Variant 4:

function shortcode_callback(){
   $html="<p>This is my HTML</p>";
   $html .= '<div>over multiple lines</div>';
   return $html;
}

Variant 5:

function shortcode_callback(){
   $html="<p>This is my HTML</p>";
   $html .= '<div>over multiple lines</div>';
   echo $html;
}

I tend to go for Variant 4 since variant 1 doesn’t seem right to me, Variant 3 and 4 are kinda just preferances and I like variant 4 more for readability.

Variant 5 is wrong since you shouldn’t echo in plugins right? I just wanted to add it, in case someone wonders the same in the future.

I don’t know about variant 2, I think I saw this somewhere once.

What is the best way tho, I would love to hear your tips.

  • This topic was modified 6 hours, 55 minutes ago by !Benni.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer