I am creating an addon for Visual Composer through vc_map and I am adding the javascript/css scripts needed by my addon to work on the website pages through

Load plugin css and javascript files which you may need on front end of your site
public function loadCssAndJs() {
    wp_register_style( 'vc_extend_style', plugins_url('assets/myaddon.css', __FILE__) );
    wp_enqueue_style( 'vc_extend_style' );

    // If you need any javascript files on front end, here is how you can load them.
    wp_enqueue_script( 'vc_extend_js', plugins_url('assets/myaddon.js', __FILE__) );

they seem to work fine, but I have one problem:

they are loaded on every page, and not only in the pages where my addon is used...

How can I make them load only on pages using my addon?

Edit: I found this article that shows how to add some conditions in the place where scripts are enqueued to load them only on specific pages, and one idea (but I'm not really sure) could be to use something similar to:

  • 1) retrieve the page content

  • 2) if certain shortcodes/content is present (my addon specific ones), then

  • 3) load those javascript/css scripts

Even if I am still unsure about how even do this, do you think it Would be a good way to solve this properly? Do you have any other suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.