Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Building WordPress Widgets, and Shortcodes How to Create a WordPress Shortcode

Output buffering doesn't seem to work

Step 3 of 4 in this challenge asks us to "wrap" the require statement in output buffering functions: "After the extract, require front-end.php in the inc folder. Before the require statement, start output buffering. Then, after the require statement, end the buffer."

I do this, but get an error, every time:

<?php
function my_plugin_shortcode( $atts, $content = null) {

    global $post;

    extract( shortcode_atts( array('num_courses' => '4'), $atts) );

    ob_start();
        require('inc/front-end.php');
    ob_end_clean();
}


?>

3 Answers

Andrew McCormick
Andrew McCormick
17,730 Points

It's not exactly worded well. As you technically did end the buffer. However according to the video right before at about the 6:30 mark, Zac describes that what you want to do here when creating the shortcode is end the buffer and returned the buffer content.
Knowing that we want to return $content we can use ob_get_clean() like so

    ob_start();
        require('inc/front-end.php');
   $content= ob_get_clean();

hope that helps

Thanks. Yes, it's not worded that way at all--"end" the buffer, versus get the buffer results at the end (two different things). Ah well.

<?php function my_plugin_shortcode( $atts, $content = null) {

global $post;

extract( shortcode_atts( array('num_courses' => '4'), $atts) );

ob_start();
    require('inc/front-end.php');

$content= ob_get_clean(); }

?>

This one worked for me

Andres Ramirez
Andres Ramirez
18,094 Points
<?php
  function my_plugin_shortcode ($atts, $content = null) {
    global $post;
    extract(shortcode_atts(array(
      'num_courses' => '4'
    ), $atts));
    ob_start();
    require('inc/front-end.php');
    $content= ob_get_clean();
  };
?>