Support » Plugins and Hacks » Why can't I echo or print?

Why can't I echo or print?

  • I’ve created shortcodes that print variable content on the screen. The only way I’ve been able to get the content to show up is to use return (i.e. return ‘Hello World!’;). Neither echo nor print works, even for something simple.

    Return works to a point, but I can only use it once within the shortcode, which doesn’t work if I need to add code between. Is there a way I can use echo or print? Am I missing something?


Viewing 4 replies - 1 through 4 (of 4 total)
  • Just for arguments sake, I’ve also tried assigning this content to variables and outputting the variables in a return string. It doesn’t work. Here’s the code:

    if (array_key_exists('logo-file', $_GET))
    $logo-file = '<img style="max-height: 75px;" src="/assets/' . strip_tags( stripslashes( $_GET['logo-file'] ) ) . '" alt="" />';
    return $logo-file;

    Figured out this particular problem…the dashes in my variables were causing the issue. (Which is odd, because I’ve seen others use code with dashes in their variable. Must be bad syntax.)

    Anyway, it would still be nice to be able to echo or print with this. Is it just not possible with shortcodes? A limitation of the WordPress code, perhaps?



    Forum Moderator

    You should never use hyphens in PHP generally. Stick to underscores.

    @asrailight: Yup, shortcodes require a return value. Check out this in the Codex.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Why can't I echo or print?’ is closed to new replies.