Using the echo command adds unwanted br tags to source

I am making a simple HTML form in a plugin I am writing. I was thinking of using echo for all output, but I can’t seem to control the formatting of the HTML outputted (for debugging, neat looking HTML, NOT talking about how the page looks to the user).

If I have an echo like:

Read More
  echo '<sometag>
      <otherTag>';

It generates HTML like:

 <sometag><br><otherTag>

So I changed the echo statement to:

 echo '<sometag>';
 echo '<otherTag>';

and got HTML like:

 <sometag><otherTag>

Lastly I changed the echo statement to:

 echo "<sometag>n<otherTag>"

But then WordPress turns the n into a <br>!

Is the only way to have nicely formatted HTML to output the text with HTML outside of php?

Or is there a way to get echo (or print) just output what I send it?

OK – I found some code that points to a ‘why’. It appears to me that the makers of this theme are using wptexturize and wpautop on all shortcodes, likely assuming that all shortcodes are their OWN (button, ui widget) shortcodes, and having wptexturize work on the shortcodes helps with some theme settings. I can think of perhaps better ways to do this…

I am writing a plugin for this theme, and will not be editing the theme, so I think I will tell them to use the [raw] shortcode route.

Theme code from /themename/functions/shortcodes/setup.php

 <?php
 // Remove WordPress automatic formatting
 function theme_formatter ($content) {
     $new_content = '';
     $pattern_full = '{([raw].*?[/raw])}is';
     $pattern_contents = '{[raw](.*?)[/raw]}is';
     $pieces = preg_split ($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

     foreach ($pieces as $piece) {
        if (preg_match ($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
         }
         else {
        $new_content .= wptexturize (wpautop ($piece));
         }
     }

     return $new_content;
 }

 remove_filter ('the_content', 'wpautop');
 remove_filter ('the_content', 'wptexturize');

 add_filter ('the_content', 'theme_formatter', 99);



 // Enable shortdoces in sidebar default Text widget
 add_filter ('widget_text', 'do_shortcode');

Related posts

Leave a Reply

1 comment

  1. If this is plugin code there is NO REASON the theme would make a difference. My guess is it’s a server issue (outputting of /n) or a WordPress function that changes the /n into <br>

    I would check wpautop() as I find that sometimes it’s this functions fault. See this post on the WordPress.org forum for more.