Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Like others, I'm having issues with WP adding unwanted p tags in my code. I have a shortcode I created that is to simply just spit out my javascript and html when I use it. Everything works fine except WP is wraping each in p tags. Adding many lines of blank unwanted space. I have tried the solution here: Automatically added brs and paragraphs? to no avail. Here is what I have in my functions.php file

<?php
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 12);
remove_filter( 'the_excerpt', 'wpautop' );

/***************/
/**ShortCodes**/
/**************/

/*** Form to contact Us **/
function contact_us() {
 return <<<HTML
   <P> Here's the low down on what to do </p>
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css"/>\n
<link rel="stylesheet" href="http://studiocloud.com/css/validationEngine.jquery.css" type="text/css"/>\n
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>\n
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>\n
<script src="http://cloudforms.co/js/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8"></script>\n
<script src="http://cloudforms.co/js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>\n
<script type="text/javascript" src="http://cloudforms.co/js/html5placeholder.jquery.js"></script>\n
<form action="" id="signupform" name="signupform">
                <input type="hidden" id="tid" name="tid" value=""/>
                <input type="hidden" id="scid" name="scid" value="TPN02L48KAVJKVRD"/>
                <input type="hidden" id="fid" name="fid" value="392"/>
                <div class="box" >
                                    <fieldset class="field_set""><legend>Contact Us For More Information</legend>
<label><span>First Name *</span><input type="text" id="firstName" name="firstName"  class="validate[required] input_text"  placeholder="First Name" ></label>
<label><span>Last Name *</span><input type="text" id="lastName" name="lastName"  class="validate[required] input_text"  placeholder="Last Name" ></label>
<label><span>Phone</span><input type="text" id="workPhone" name="workPhone"  class="validate[optional] input_text"  placeholder="Phone" ></label>
<label><span>Email *</span><input type="text" id="email" name="email"  class="validate[required,custom[email]] input_text"  placeholder="Email" ></label>
<label><span>Comments *</span><textarea name="item5"  id="item5"  class="message" placeholder="Comments" ></textarea></label>
<label><span></span>* Required Fields</label>
<label><input type="button" id="item7"  name="item7" onClick="processContactUs(event)" class="button" value="Submit" ></label>
</fieldset>
                </div>
            </form>
            <div class="modal"><!-- Place at bottom of page --></div>
HTML;
}


add_shortcode('contact_us','contact_us');

any ideas? using \n seems to get rid of unwanted br tags I was getting, but now still getting the p tags. thanks.

share|improve this question
    
Have you searched the site already? –  kaiser Jun 25 '13 at 21:58
    
yep, that's why I linked to a previous solution that didn't work for me, that many other questions linked to as the answer. Tried to indicate that I have searched around and the top solution that is referenced wasn't working. –  Andy McCormick Jun 26 '13 at 0:54
add comment

2 Answers

up vote 1 down vote accepted

The simple solution is to not give wpautop anything to chew on.

function contact_us() {
    $str = "<p>Here's the low down on what to do</p>";
    $str .= '<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css"/>';
    $str .= '<link rel="stylesheet" href="http://studiocloud.com/css/validationEngine.jquery.css" type="text/css"/>';
    // etc
    $return $str;
}

Your generated code won't be formatted, but there is no reason it should be. We are long past the days of static HTML, at least I am ( "Thank you" to the Lords of Kobol ), where such formatting mattered. Now, all that formatted is just unnecessary characters that have to travel down the wire.

share|improve this answer
    
Thanks, that seemed to do it. I was trying to be lazy and not have to add each line to a string and just use the heredoc. Oh well. I went ahead and did it this way and it cleaned up perfectly. –  Andy McCormick Jun 25 '13 at 23:53
add comment

Re-arranging the default WP filters may have unwanted consequences (I've had some issues with some plugins that generated forms via shortcodes when using this method). What works perfectly for me (and many others) is this solution:

function wpse104216_shortcode_fix( $content ) {
    // List all your shortcodes as an array
    $block = join( '|', array( 'contact_us', 'column', 'button', 'another-shortcode' ) );

    $rep = preg_replace( "/(<p>)?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/", '[$2$3]', $content );
    $rep = preg_replace( "/(<p>)?\[\/($block)](<\/p>|<br \/>)?/", '[/$2]', $rep );

    return $rep;
}
add_filter( 'the_content', 'wpse104216_shortcode_fix' );
share|improve this answer
    
thanks, however that didn't seem to work for me. I also tried other versions of shortcode_fix functions I found after seeing your example. –  Andy McCormick Jun 25 '13 at 23:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.