Combine+Minify styles of parent theme and add as inline-style in wordpress


Combine+Minify styles of parent theme and add as inline-style in wordpress

In a recent project, one of my tasks was to optimize and minify the styles included by the child-theme by combining and minify the styles of the parent theme into one file or include as custom inline style.

While working on the task, I ended up reading multiple resources to and writing the function below.

The function first fetches all the styles that are being included on page, and then for the handles belonging to the parent theme, the script then concatenates the scripts and then minifies by using preg_replace().
The generate combined CSS is stored in a database table and updated every 24 hours(triggered on page load).

It then de-registers the style handles we have combined and includes the custom style as an inline style.