Skip to content

Instantly share code, notes, and snippets.

@onwp
Last active February 25, 2023 15:26
Show Gist options
  • Save onwp/e1a388eed972b3029bae98fa22a72d37 to your computer and use it in GitHub Desktop.
Save onwp/e1a388eed972b3029bae98fa22a72d37 to your computer and use it in GitHub Desktop.
Find unused CSS classes in html,php,js files for multiple themes. Run this script in "themes" directory of Wordpress
#!/bin/bash
for dir in $(ls -d */ | grep -v cache | grep -v node_modules | cut -f1 -d'/'); do
(
echo "checking $dir :";
pushd "$dir" &>/dev/null || {
printf "error: failed to change to %s\n" "$dir" >&2
continue
}
echo "--------------------------------";
cat $(find app/styles/ -type f) |
grep -Eo '\.[a-z]+[a-z0-9_-]*' | sort | uniq | sed s/.// |
while read CSS; do
if ! grep -Erqi "([^(remove|has)]?class[(:|=|[:space:]*=>[:space:]*)]*[[:space:]\W]*[(\"|')]*[-a-z0-9[:space:]]*$CSS|\\.$CSS\b)" app/scripts/ assets/ campaigns/ components/; then
echo $CSS >> unused.scss;
fi
done
echo "--------------------------------";
)
done
@onwp
Copy link
Author

onwp commented Jan 10, 2023

This is to scan multiple themes. You can use it for one theme only as well.

All you need to do is getting rid of for loop and $dir related lines.

And don't forget to update folder names to scan.

Tested on mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment