0

Recently I have been playing around with Wordpress Shortcode. When following this tutorial (changed a few things, of course), I found that even without returning anything (return $return_string; - part) my code is still working as intended.

I opened PHP manual and it says:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return will also end the execution of an eval() statement or script file.

My question:

Since looks like there is nothing broken, should I keep doing what I do, or should I "return" something from my function (there is actually a downside from not "return-ing" anything I just haven't seen, yet) ?

Edit: The code, just in case.. no "return", still processing the shortcode / loop

function looping_cat($atts, $content) {
    extract(shortcode_atts(array(
      "query" => '',
      "category" => ''
    ), $atts));

    $wp_query = new WP_Query();

    if(!empty($category)){
      $query .= '&category_name='.$category;
    }
    $wp_query->query($query);
    ?>

      <ul>
      <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
          <li>
              <div>
                  <?php the_post_thumbnail(); ?>

                   <h2><?php the_title() ?></h2>
              </div>
               <?php the_excerpt(); ?>
              </div>
          </li>
      <?php endwhile; wp_reset_query(); ?>
      </ul>
    <?php 
}
Mario88
  • 73
  • 2
  • 11

1 Answers1

0

If you don't specify a return statement, null will be returned. See this question.

If it fits your goals that nothing will be displayed for your shortcode, it is fine to omit the return statement.

For readability purposes I recommend to use return statements everywhere, even if you are returning null.

Edit:

Now I understand what do you mean by returning nothing. Actually you're inlining the HTML in your shortcut function.

Actually, I still prefer to collect and return all content as a string, because it gives you more control of handling the content. If a certain condition happens later, you may still decide to return something else. It may also "disturb" WP, because inlining will output the content immediately, while returning the content enabled WP to decide when (and where) will it show.

Community
  • 1
  • 1
Camouflage
  • 266
  • 1
  • 8
  • Hmm.. I know this sound cliche "it works here".. but it actually is. I'm sure you know how WP shortcode works, my shortcode is to display loop from a certain category. A few minutes ago, I removed 1 letter from [shortcode] so it became [hortcode]. Refreshed the page, the loop disappear. Add the letter back, save and refreshed, and the loop is back. ..? I am **not** saying that you are wrong, but.. well, this is why I am asking the question. – Mario88 Jul 07 '13 at 11:17
  • Well, if you want to display something, you should *really* use `return`. It also improves readability (even for yourself after 1 week). – Camouflage Jul 07 '13 at 11:41
  • 1
    A shortcode shall not `print`/`echo` **anything**. It has to `return` something. Otherwise, it prints *before* `the_content`. Well, unless it's the desired effect. – brasofilo Jul 07 '13 at 18:04
  • @brasofilo WP has a mechanism for building the page. By `return`ing the content you use it (if you are using it properly). If not, the **behavior is unpredictable**. It may *seem* to work, but future versions of WP may **behave differently**, then OP will face a terrible refactoring. – Camouflage Jul 08 '13 at 00:34