开发者

Why does the output of my code contain additional <br> and <p> tags when it's not indicated in the code?

开发者 https://www.devze.com 2023-02-28 18:42 出处:网络
I\'m new to PHP coding. I\'m confused about this one. I am creating some shortcodes for my wordpress site. Basically, I have created a number of shortcodes that work fine, but when I start coding this

I'm new to PHP coding. I'm confused about this one. I am creating some shortcodes for my wordpress site. Basically, I have created a number of shortcodes that work fine, but when I start coding this one, the output contains unnecessary < br> and < p> tags.

here's a portion of my code:

function searchbox ($atts) {
global $wpdb;
$link = get_bloginfo('url').'/search/';
$out .= '<form name="search" action="'.$link.'" method="get">';
$out .= '<label>Developer:</label>';
$out .= '<select name="dev">';
$developers = $wpdb->get_col($wpdb->prepare("
        SELECT DISTINCT meta_value 
        FROM $wpdb->postmeta 
        WHERE meta_key = %s 
        ORDER BY meta_value ASC", 'developer') );
 if ($developers) {
   foreach ($developers as $developer) {
     $out .= "<option value=\"" . $developer . "\">" . $developer . "</option>";
   }
 }
$out .= '</select>';
$out .= '<label>Location</label><select name="loc">';
$locs = $wpdb->get_col($wpdb->prepare("
        SELECT DISTINCT meta_value 
        FROM $wpdb->postmeta 
        WHERE meta_key = %s 
        ORDER BY meta_value ASC", 'loc') );
 if ($locs) {
   foreach ($locs as $loc) {
     $out .= "<option value=\"" . $loc . "\">" . $loc . "</option>";
   }
 }
$out .= '</select>';
$out .= 'Price Range: <input type="text" size="4" name="pmin"> '
        +'to <input type="text" size="4" name="pmax">';  
$out .=  '<input typ开发者_如何学编程e="submit" value="search" />';
$out .= '</form>';
return $out;
}
add_shortcode ('searchbox', 'searchbox');

then the shortcode [searchbox] is added in the WP editor. the output html code is:

<form name="search" action="http://www.mycondophilippines.com/search/" method="get">
    <label>Developer:</label><br /> 
    <select name="dev"> 
        <option value="Avida Land">Avida Land</option> 
        <option value="DMCI Homes">DMCI Homes</option> 
        <option value="SMDC">SMDC</option> 
    </select> 
    <p><label>Location</label><br /> 
    <select name="loc"> 
        <option value="Makati">Makati</option> 
        <option value="Mandaluyong">Mandaluyong</option> 
        <option value="Manila">Manila</option> 
    </select> 
    <p>Price Range:<br /> 
    <input type="text" size="4" name="pmin"> to<br /> 
    <input type="text" size="4" name="pmax"> 
    <input type="submit" value="search" />
</form> 

Notice the added br and p tags. I observed that when a text is added on the html code, it automatically adds <br> or <p> tags. I'm confused with this one, because the shortcode functions that I make are in one php file, and the others work fine with text on html code. It is this function that adds the tags.

I use Dreamweaver CS5 for coding. But I tried to edit with notepad2 but with the same result.


WordPress adds the wrapping <p> and <br /> in the page/post editor. I'm assuming that's where you're entering your shortcode.

UPDATE

The OP found a solution - http://www.simonbattersby.com/blog/2009/08/stop-wordpress-adding-br-tags/


You can simply add a function in your theme file (function.php). That way when you upgrade WP source you won't have to think about re-changing that function.

by using the WP codex(http://codex.wordpress.org/Function_Reference/wpautop)

just add:

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


You must be passing your generated html through a beautification function before sending it to the client (htmlpurifier?). The output has linebreaks, while your code does not insert any at all, which means there's something modifying your output.

You may also want to look up HEREDOCs, which let you create multi-line strings without any of the hassles of repeated string contatenations or quote escaping.

0

精彩评论

暂无评论...
验证码 换一张
取 消