{"id":1784,"date":"2011-02-01T21:56:17","date_gmt":"2011-02-01T20:56:17","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?p=1784"},"modified":"2011-06-15T17:40:54","modified_gmt":"2011-06-15T16:40:54","slug":"wordpress-wrapping-shortcodes-with-p-tags","status":"publish","type":"post","link":"https:\/\/www.simonbattersby.com\/blog\/2011\/02\/wordpress-wrapping-shortcodes-with-p-tags\/","title":{"rendered":"WordPress wrapping shortcodes with p tags"},"content":{"rendered":"<p>Been struggling over the last day or so to stop WordPress wrapping p tags around a shortcode. I&#8217;m using Arnan de Gans <a href=\"http:\/\/meandmymac.net\/plugins\/events\/\">Events plugin<\/a> on a WordPress site for a client. One of the pages just includes this shortcode:<\/p>\r\n\r\n<pre>[events_show]<\/pre>\r\n\r\n<p>This shows a list of events. I&#8217;ve configured the plugin so each event is in its own div. And it all works fine, except WordPress insists on wrapping the whole thing in a couple of p tags, making the code invalid, producing this:<\/p>\r\n\r\n<pre>&lt;p&gt;&lt;div class=\"event&gt;.........&lt;\/div&gt;&lt;\/p&gt;<\/pre>\r\n\r\n<p>Irritating. The WP shortcode API site says <em>&#8220;wpautop recognizes shortcode syntax and will attempt not to wrap p or br tags around shortcodes that stand alone on a line by themselves&#8221;<\/em>. Nooooo.<\/p>\r\n\r\n<p>I finally solved this by adding the following to my functions.php file:<\/p>\r\n\r\n<pre>function simonbattersby_shortcode_format($content){\r\n$content = preg_replace('\/(&lt;p&gt;)\\s*(&lt;div)\/','&lt;div',$content);\r\n$content =  preg_replace('\/(&lt;\\\/div&gt;)\\s*(&lt;\\\/p&gt;)\/', '&lt;\/div&gt;', $content);\r\nreturn $content;\r\n}\r\n\r\nadd_filter('the_content','simonbattersby_shortcode_format',11);<\/pre>\r\n\r\n<p>Points to note &#8211; the filter needs to be set with priority 11 (or higher) because otherwise the shortcode won&#8217;t have produced its output, and it&#8217;ll have no effect. All the <code>preg_replace<\/code> lines are doing is looking for a <code>&lt;p&gt;<\/code> tag followed by <code>&lt;div&gt;<\/code>, with any number of whitespaces between them, and replacing with just a <code>&lt;div&gt;<\/code> tag. Brute force, inelegant, but it works, and I couldn&#8217;t find any other way&#8230;.<\/p>\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>Been struggling over the last day or so to stop WordPress wrapping p tags around a shortcode. I&#8217;m using Arnan de Gans Events plugin on a WordPress site for a client. One of the pages just includes this shortcode: [events_show] This shows a list of events. I&#8217;ve configured the plugin so each event is in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[10],"class_list":["post-1784","post","type-post","status-publish","format-standard","hentry","category-web-design-and-build","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/1784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=1784"}],"version-history":[{"count":0,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/1784\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=1784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/categories?post=1784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/tags?post=1784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}