Parsing So You Don't Split Up H2 Subtitles From Paragraphs

I am using a plugin called Automatically Paginate Posts to parse our posts and to split them into multiple pages by automatically inserting the nextpage tag after a certain number of words. It always rounds up though to the next whole paragraph so that paragraphs do not get cut in the middle. A lot of the paragraphs in the posts have h2 subtitles above them though, but they are often getting parsed separately from the paragraph that they go with (this is an example: http://www.pantryspa.com/home-and-garden-remedies/bedroom-remedies/konmari-method-for-organization-declutter-your-life-with-the-joy-test/) ... that last h2 subtitle "GMA: KonMari Method for Organization" should really be on the next page above the paragraph it is about. So I'm trying to figure out how to get the h2 subtitles to be considered "part" of the following paragraph. I figured this was easiest by replacing <h2> tags with <p><h2> and by replacing </h2><p> with just </h2>, but something isn't working about that (perhaps because of the new line or space between the </h2> and the <p> on the next line)?

This is the php that I wrote and edited the plugin's php to contain:

$content = preg_replace( '#</h2>\r\n<p>#', "</h2>", $content );
					$content = preg_replace( '#<h2>#', "<p><h2>", $content );

but that doesn't seem to work.

I have granted support access via the wpmu plugin, in case that helps.

Thanks for your help!

Here is the code that I added to the plugin:

$content = preg_replace( '#</h2>\r\n<p>#', "</h2>", $content );
$content = preg_replace( '#<h2>#', "<p><h2>", $content );

  • Michael Bissett

    Hey @d-invent, hope you're doing well this evening! :slight_smile:

    I do get the idea with the code (which the above would require the \r\n to be surrounded in brackets, you can test this sort of scenario in RegExr), but I'm not seeing that the tag after the closing h2 tag is always a paragraph tag here, you're going to run into problems with that down the line.

    If you're wanting a sure fire way for h2 tags to be included in the paragraph, you'd be looking at wrapping the <h2> tag & the content in question in paragraph tags manually.

    Kind Regards,
    Michael

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.