Save HTML Content To Variable In WordPress And Call Anywhere

By March 29, 2017Blog, CMS, Wordpress
WordPress Tutorials by Allure Web Solutions

Sometimes we may want to display a block or snippet of HTML inside our WordPress theme or plugin. We can save this snippet to a PHP variable and call it anywhere, inside or outside a loop.

<?php ob_start(); ?>
  <span>Your HTML goes here</span>
<?php $html = ob_get_clean(); ?>

Now you call the $html variable anywhere in your code.

Mike Doubintchik

Author Mike Doubintchik

More posts by Mike Doubintchik

Leave a Reply