WP: Change headings en-masse for WordPress.com posts

Preamble

Following on from my WordPress.com Perl extension tools, how about changing all heading levels, en-masse?

See also

Scenario

You have a long WP document. Suddenly you remove a top level heading, so now all of your heading level 5 need to become heading level 4. You don’t want to have to trawl through the entire document and manually change each occurrence. OK, you could do a search and replace in a text editor, on the HTML. Hmmm…

However, what search and replace can not do is intelligently, look at the current heading hierarchy and adjust or correct it.

So let’s say that you aren’t particularly organised with your headings, and your top level headings are level 1 and then (having omitted level 2 headings completely) followed by sub headings of level 3, then which themselves contain sub headings which are mostly of level 4 (but some are mistakenly level 5 (which don’t have parents of level 4)). Finally, for argument’s sake, the level 4 and 5 headings all have sub headings which are then level 6.

So visually:

1
  3
    4
  3
       5
           6
  3
    4
          6
1
    4  
          6

what we want to have is:

1
  2
    3
  2
    3
      4
  2
    3
      4
1
  2
    3

Also, you can state which heading is to be your top level heading to work down from. For example, I don’t like the WP heading 1 size, so I always start my top level headings with level 2.

So, the above example, if starting from level 2, would be:

2
  3
    4
  3
    4
      5
  3
    4
      5
2
  3
    4

Additional options are, skipping one (or more) particular heading level(s), let’s say 4, in which case the last example (starting with heading level 2) would be:

2
  3
      5
  3
      5
        6
  3
      5
        6
2
  3
      5

Confused? You should be..! However, it really is rather simple.

 

Code

Code to follow

This is the end my friend

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s