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

Maja B.
Maja B.
12,984 Points

Challenge Task 2 of 4 (How to Create a WP Shortcode)

Hi, please help me pass this challenge:

Inside of the my_plugin_shortcode function, pull in the global post variable. Then extract the num_courses attribute, setting it to a default value of 4.

<?php

function my_plugin_shortcode ($atts, $content = null) {
  global $post;
  extract (shortcode_atts( array(
    'num_courses' => '4'
  ), $atts));
};
?>

Bummer! extract should be passed the return value of shortcode_atts directly.

4 Answers

Hi Maja,

Remove your space between extract and the opening ( and that should do it.

Thanks

-Rich

Maja B.
Maja B.
12,984 Points

great! thanks!

-Maja

<?php function my_plugin_shortcode( $atts, $content = null ) { global $post; extract(shortcode_atts( array( 'num_courses' => '4' ), $atts) );

} Here's my answer. Make sure to take the 's' off the posts of global $posts

<?php

function my_plugin_shortcode ($atts, $content = null) { global $post; extract(shortcode_atts( array( 'num_courses' => '4' ), $atts)); }; ?>

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));
  };

?>