Question

Normally, I would hook into the wp_enqueue_scripts action in order to gather a list of enqueue'd scripts and styles.

However, I have a very intensive process I need to run, thus instead of running it through the site itself, I am hooking into WP CLI.

How can I gather the scripts and styles without having the action wp_enqueue_scripts available to me in CLI? Or is there a CLI action that I am just not finding in the documentation for the WP CLI API?

Was it helpful?

Solution

You can't, and it doesn't make sense to do so.

Different pages/URLs enqueue different things, e.g. a WP Admin page won't enqueue the same styles and scripts, widgets might enqueue things conditionally, etc.

But in WP CLI those hooks don't run, and there is no page or frontend. So the question doesn't make sense at a fundamental level. It isn't enough to know the URL either, you need to render the page to know which scripts and styles are enqueued. There is no way to know in advance.

Could you render the page in WP CLI? Unlikely, a CLI environment will be missing a lot of the environment that a browser request has. For example, there is no URL, no GET/POST, no current user, cookies, etc.

The closest you can get to this, is a curl request. Just know that the security policy you generate will be specific to that page, and will be missing other scripts and styles, e.g. things only enqueued for admins/logged in users/other pages/etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top