WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

With a few extra lines in my functions.php I've created a shortcode that lets me include the content of a post:

function include_content( $atts ) {
  $id = (int) $atts['id'];
  if ( ! $post = get_post( $id ) )
    return '';
  return apply_filters( 'the_content', $post->post_content );
add_shortcode( "include", "include_content" );

Now, if I make a posting like

A few weeks ago I published an epic statement: [include id="345"]

WP generates

<p>A few weeks ago I published an epic statement: <p>FTW!<br />
just my 2 ¢</p>

So, obviously the content of the old post is wpautop'ed before it's included. If simply returning $post->post_content, that content would end up unrendered (i.e. no <br />) within the outer posting. Now, how can I get rid of the paragraph tags around the imported post, i.e. how can I do the include first and wpautop the whole thing after? Any code improvements are welcome, too!


share|improve this question

closed as too localized by toscho Jul 19 '12 at 23:46

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

I suggest you <blockquote> that and maybe even strip_tags() the_content and insert the tag on a new line which should look and feel better, since you're quoting another post. – soulseekah Oct 18 '11 at 5:42
@Soulseekah I'd really like to see it work plain include style. It's not only about quoting. Thanks anyway! – Morgan Oct 18 '11 at 5:57
I really don't see the point in removing the first <p> and the last </p> in an imported post, you either strip all the <p> or not strip the <p>; try to turn this into the filter substr( $post->post_content, 3, -4 ), which answers your initial call for something to "get rid of the paragraph tags around the imported post", you may end up with mismatching <p> tags in several circumstances, and the whole point of markup goes down the drain and you have <p> inside <p>. – soulseekah Oct 18 '11 at 6:04
Of course it's not only about getting rid of the first <p> and the last </p>. Basically I want the content of the old post included first and then the whole thing rendered. – Morgan Oct 18 '11 at 6:23
Insert the shortcode on a line of its own and make sure it's not surrounded by <p> in the HTML editor, that will include your post as is without the extra stuff. Does this solve your problem? – soulseekah Oct 18 '11 at 6:27