0

For a Wordpress plugin, I made a function that contains a big HTML portion (following WP docs, I used ob_start and ob_get_clean to insert it):

function myShortcode() {

   ob_start();?> 

    <!-- here a lot of HTML -->

    <?php
        return ob_get_clean();

}

I would like to put the HTML outside this function and include or require it.

Is this possible? Is there something I should be aware of? Is it preferable file_get_contents? Any other tip is appreciated, thanks in advance

1 Answer 1

1

You can move your html to separate file and then include it as simple php file after ob_start()... Yes it will work, just make sure your view.php template partial is Echoing that html, i.e. You may have html outside of php tags e.g.

File view.php

     <?php //Template code starts ?>

      ALL HTML HERE

    <?php // Template code ends ?>

And your current function in current plugin php file will become:

 function myShortcode() {

               ob_start();
               include(PLUGIN_DIR_PATH/templates/view.php);
               return ob_get_clean();

            }
Sign up to request clarification or add additional context in comments.

2 Comments

Echoing as a echo "big html string"? That is: I can't insert it as raw HTML?
@3000 You can as raw html, no need to Echo, see I have closed php tags, so you can insert HTML as RAW html, i just made it .php file so if you need to have any php variables inside html for data like <h2><?= $title ?></h2> for it to work in raw html, you need to have php file.. hence view.php...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.