Skip Navigation

[Resolved] Enqueue script / style only when needed

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.21 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by dieterM 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1678743

I want a javascript / css library to load only when a view is loaded.

Since there's no php allowed in Views, I created a shortcode [dmo_enqueue_splide] which loads the necessary files using the wp_enqueue_script and wp_enqueue_style function. I registered this shortcode as a third party shortcode. But views doesn't run it, it just outputs [dmo_enqueue_splide].

I'm aware I can load the files using <script> and <link> tags.
But then I have no control over the location of the files (I want them in the footer) and the files can be loaded multiple times of multiple views are loaded, or other components which use the same libraries.

Any suggestions?

#1678907

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

If you get [dmo_enqueue_splide] in the frontend instead of what it should do, I would suspect that the shortcode is not correctly registered. To confirm that, please switch to a default theme and deactivate all plugins including Toolset ones, then add that shortcode to a page or post and check if it works as expected. If it works, we can investigate why is it not working with Toolset views. If it does not, it would mean that the shortcode is not correctly registered.

I hope this helps. Otherwise, please explain where did you add the shortcode code/definition and share the code here to let us check it too.

#1679053

Indeed, there was a typo in my function that registered the shortcode. I wrongly assumed that views wasn't processing the shortcode. It works fine now.

Thanks!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.