How to cleanup HTML? Any WP plugins?

fasttrak

Elite Member
Joined
Sep 12, 2016
Messages
3,514
Reaction score
3,988
I have imported a site from squarespace to WP and the code is a MESS, anyone know of a WP plugin or another quick way of stripping out all the crap inline styles and junk from the code?

This is the sort of code I mean.

<p class="" style="white-space:pre-wrap;">

<table class="custom-table-block custom-table-block-even-rows

<figure class="
sqs-block-image-figure
intrinsic
" style="max-width:1500px;">

I would like to get the code back to basic HTML and let the theme format the content.
 
I don't think that something as powerful as you want exists.... The only thing I found after 10 minutes of googling is this thing, but I'm not sure that it's what you need but I can't find anything else:

https://wordpress.org/plugins/wp-asset-clean-up/

Thanks for taking a look and replying. There are lots of plugins which manage files (CSS, JS) and delay/defer rendering but nothing for a VERY long time that actually cleans up HTML. I did find a couple of old plugins which strip out tags and attributes but even downgrading to WP 3 & 4 they were far too unstable to work properly for 350 pages.

I have spent most of the day looking at this and I cannot for the life of me find any WP plugin or shortcut, which is a pain, but also a huge gap in the market for someone to create one!

I do have some manual work arounds which do the job but means it will take 10hrs+ to clean up the code on all those pages one by one.

Maybe someone knows of something to save the day...
 
but also a huge gap in the market for someone to create one!
maybe there's not big enough of a demand. For me personally for example, you're the 1st person that I hear needs this :)

I do have some manual work arounds which do the job but means it will take 10hrs+ to clean up the code on all those pages one by one.
well, if there's no other option...

Maybe @BlurryBit knows something, he's quite good at this stuff and he's helped me a bunch in the past...
 
maybe there's not big enough of a demand. For me personally for example, you're the 1st person that I hear needs this :)

That is very true, I do seem to have odd needs :D and I need to stop saying yes to customers.
 
Perhaps try a regex based solution?
For classes:
Code:
https://regex101.com/r/ZNeRQD/1
For inline styles:
Code:
https://regex101.com/r/NA79RF/1

May be write a simple script that searches the wp-content folder for those regexes, and replaces them with "" (BE SURE TO BACKUP FIRST. DO IT IN YOUR LOCAL MACHINE.).

The problem is, that regex may not work for every case, and it can have bugs as well (i have not tested the regexes on a large dataset, please test and modify them as needed)..

This is one of the reasons why I don't like third party plugins and themes (or wp in general). If you want more control, you have to have custom code. :p
 
Just a thought - have you thought of asking an AI LLm for help - say Claude2 or OpenAI - I Claude with it's 100k token offers the best avenue for your structured prompts.

Or ask can they create a custom plugin to meet you needs

Just my $0.02
 
Depending on how much html you need to parse I think this would be a good job for GPT. You could ask it to remove all custom style classes or similar.
 

Thank you, that is actually really helpful. I found an old plugin which works with WP 5 that takes the regex in your examples, I will give that a try. My manual method is to run the code through https://html-cleaner.com/ which does actually do the job perfectly. Shame that does not show the regex for each of the cleaning options.
Just a thought - have you thought of asking an AI LLm for help - say Claude2 or OpenAI - I Claude with it's 100k token offers the best avenue for your structured prompts.

Or ask can they create a custom plugin to meet you needs

Just my $0.02

Depending on how much html you need to parse I think this would be a good job for GPT. You could ask it to remove all custom style classes or similar.

I never thought of trying ChatGPT or AI for this. Will have a look at that.
 
Perhaps try a regex based solution?
For classes:
Code:
https://regex101.com/r/ZNeRQD/1
For inline styles:
Code:
https://regex101.com/r/NA79RF/1

May be write a simple script that searches the wp-content folder for those regexes, and replaces them with "" (BE SURE TO BACKUP FIRST. DO IT IN YOUR LOCAL MACHINE.).

The problem is, that regex may not work for every case, and it can have bugs as well (i have not tested the regexes on a large dataset, please test and modify them as needed)..

This is one of the reasons why I don't like third party plugins and themes (or wp in general). If you want more control, you have to have custom code. :p

Sorry for the stupid question but how can I get that regex to find matches with the Search Regex plugin?

I searched with (?<=<div.*?)(?<!=\t*?"?\t*?)(class|style)=".*?" but it did not find any matches. I am obviously doing something wrong.

Screenshot 2023-08-07 at 11.46.57.png
 
cleaning this way going to be complex, with regex you can strip all html tags but then you will end up with plain texts without formatting and without links, with this regex <(?!\/?(h[1-6]|p|a|ul|li)\b)[^>]*> you can preserve h,p,a,ul,li tags but even then you will have some unwanted codes. does squarespace allow .xml export? if so then you can import .xml in wordpress, and for images you have to create paths/folders in wp manually. Not sure just giving you idea
 
cleaning this way going to be complex, with regex you can strip all html tags but then you will end up with plain texts without formatting and without links, with this regex <(?!\/?(h[1-6]|p|a|ul|li)\b)[^>]*> you can preserve h,p,a,ul,li tags but even then you will have some unwanted codes. does squarespace allow .xml export? if so then you can import .xml in wordpress, and for images you have to create paths/folders in wp manually. Not sure just giving you idea

It is the XML export from Squarespace that creates all the crap code. I have the posts and images, just need to clean up the code for 300+ posts.
 
then try with this regex <(?!\/?(h[1-6]|p|a|ul|li)\b)[^>]*> in notepad++ with one of your file and see what else need to be removed
 
I have imported a site from squarespace to WP and the code is a MESS, anyone know of a WP plugin or another quick way of stripping out all the crap inline styles and junk from the code?

This is the sort of code I mean.

<p class="" style="white-space:pre-wrap;">

<table class="custom-table-block custom-table-block-even-rows

<figure class="
sqs-block-image-figure
intrinsic
" style="max-width:1500px;">

I would like to get the code back to basic HTML and let the theme format the content.

There is a tool for this ;)

https://html-cleaner.com/
 
Sorry for the stupid question but how can I get that regex to find matches with the Search Regex plugin?

I searched with (?<=<div.*?)(?<!=\t*?"?\t*?)(class|style)=".*?" but it did not find any matches. I am obviously doing something wrong.

View attachment 275347
Try adding / before the regex, and add /gmi after the regex.

So it becomes something like
/regexstring/gmi

That might do it. I am not sure what kind of regex engine the plugin uses though. Depending on implementation it might or might not work.
 
These Plugins Will Help You:
  • Clean HTML
  • Remove Unused CSS and JS
  • TinyMCE Advanced
  • HTML Minifier
  • WordPress Code Cleanup
 
Run it through chatgpt and ask it to clean it for you. Or if the file is too big, use claude2
 
Back
Top