GaryJones / plugin-boilerplate

PSR-4 + PHP 7.4 WordPress plugin boilerplate.
GNU General Public License v2.0
94 stars 11 forks source link

Best practices for running scripts #20

Open steveadamsfixedit opened 3 years ago

steveadamsfixedit commented 3 years ago

I've used the boilerplate to create a custom plugin to track user engagement analytics on live streamed video assets. included in the plugin within the /public directory are some fairly robust JavaScripts that mesh with a webhook from the stream provider and a AJAX call to track real time player events. It's my understanding that these scripts added to the enqueue_scripts() function in the public/class-plugin-name-public.php run on every single page in the rather vast WP site I'm intending to deploy it on. This seems inefficient and rather insecure... What's best practice as far as only getting the scripts to run on specific pages...for instance, making a custom theme template and only applying that template file to a page containing video assets?

GaryJones commented 3 years ago

For front-end JS, you can conditionally enqueue with https://developer.wordpress.org/themes/basics/conditional-tags/, and for admin pages, you can check against get_current_screen().