Looking for some help on something that has me really contemplating not using blockquotes for the moment!

I have this code:

    $show_after_p = 1; $content = apply_filters( 'the_content', get_the_content() );
    if(substr_count($content, '<p>') > $show_after_p) {
        $contents = explode('<p>', $content); $p_count = 1;
        foreach($contents as $content) {
            $content = str_replace('</p>', '', $content);
            echo wpautop($content);
            if($p_count == $show_after_p) { get_template_part('_templates/box'); } $p_count++;

The reason for the wpautop is to fix a short code in the content, and the way that inserting the box in before the first <p> rather than after. Anyway, since the box is addded before the first <p> tag, in a blockquote, it inserts in-between:

    <!-- The inserted box -->
    <p>The quote</p>

So I'm looking for a way to strip the <p> from the <blockquote> (I realise it's not semantically correct, but the internet will have to heal with it!) OR something better..

Also I've added the functions tag in case someone from there could turn this into a function since I do have more than one post type this would apply to :]

  • Would it not be better to fix the problem shortcode instead of writing hacky code to work-around whatever that issue is? – t31os May 2 '13 at 10:20
  • This bug is like 8 years old and they haven't solved this yet: core.trac.wordpress.org/ticket/3833 – imrek Jun 15 '15 at 13:41

Manipulating HTML with regular expressions is not a good idea. I suggest you use DOMDocument:

// input
$html = apply_filters('the_content', get_the_content());

$dom = new \DomDocument();

$blockquotes = $dom->getElementsByTagName('blockquote');
foreach($blockquotes as $blockquote){
  foreach($blockquote->childNodes as $e){
    if($e->nodeName === 'p'){
      // create a text node with the contents of the <p>
      $blockquote->insertBefore($dom->createTextNode($e->textContent), $e);

      // remove <p>

// remove doctype
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);  

print $dom->saveHtml();
  • Two items: 1. Where would I put this? 2. No way to do it with WP? – markb May 2 '13 at 9:24
  • In your template file, where you want your blockquote? – onetrickpony May 2 '13 at 9:26
  • But the blockquote is apart of the_content – markb May 2 '13 at 9:27
  • Just replace the code you have above with this – onetrickpony May 2 '13 at 9:31
  • And I just realised I only have 5.3.16 -,- – markb May 2 '13 at 9:33

Maybe you can use a simple preg_replace, but you have to remove spaces around P in this example

$result = preg_replace('< p [^>]>(?:\s+|(?: )+|(?:)+)', '', $content);

If you will not use any tags inside blockquote, maube you can use strip_tags($content); to remove all html tags

  • At line do you suggest this to be added? – markb May 2 '13 at 9:14
  • sorry man, the comment split some characters, the code is this $result = preg_replace('<p[^>]*>(?:\s+|(?:&nbsp;)+|(?:<br\s*/?>)+)*</p>', '', $content); – Santoro May 2 '13 at 9:18
  • You have to change the '<p[ and </p>, the first one is the beginner tag and the last '</p>' I think you have to delete, sorry, I'm not an expert in this things – Santoro May 2 '13 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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