1

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.

2
  • Have you searched the site already?
    – kaiser
    Commented Jun 25, 2013 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. Commented Jun 26, 2013 at 0:54

2 Answers 2

1

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.

1
  • 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. Commented Jun 25, 2013 at 23:53
1

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' );
1
  • 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. Commented Jun 25, 2013 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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