1

Wordpress 5.0.3 Shared LAMP Hosting

I'm devloping a plugin that requires a shortcode like this:

function my_shortcode() {
require_once( 'includes/functions.php' );
$my_text = '<pre id="myspecs>'.get_all_stats().'</pre>';
return $my_text;
}
add_shortcode('MyShortCode', 'my_shortcode');

When I insert [MyShortCode] into the page content, the get_all_stats() data are rendered, but pre html formatting rendered after the data, separately. Rendered source looks like this:

<div class="entry-content">
Mywordpressdata-all-in-a-jumble-over-multiple-lines-squashed-together...

<pre id="myspecs">\n\n</pre>
</div>

How can I tell WP to keep the data inside the pre html formatting?

Chris
  • 261
  • 2
  • 8

1 Answers1

1

This should fix it,

function my_shortcode() {
ob_start();
  require_once( 'includes/functions.php' );
  ?>
  <pre id="myspecs><?php get_all_stats(); ?></pre>
  <?php
  return ob_get_clean();
}
add_shortcode('MyShortCode', 'my_shortcode');

more info

admcfajn
  • 2,013
  • 3
  • 24
  • 32
  • Thank you! I'd tried ob_start() before, but I didn't have the correct coding grammar. This helped immensely. It now works as expected. – Chris Jan 18 '19 at 02:06
  • 1
    Cheers! Glad I could help. **I think** you could also store the result of the function as a variable and output it in the string, but the function call within the string won't work. – admcfajn Jan 18 '19 at 02:16