This topic contains 5 replies, has 4 voices, and was last updated by Sofia Moss Sofia Moss 4 years ago.

Load plugin assets (js and css files) only when needed

  • Hi, I’m already using this plugin and is working fine, but the js and css files are being loaded in every page. I want to load them only in pages where this plugin is used (actually is only one), is there any way to do it? I haven’t seen any option in the settings menu…

    Thanks in advance!

    Alex
    StaffUser Level

    Hi there,

    Thank you for your inquiry, you can use Plugin Organizer which can achieve this, more details available in this article here.

    Cheers,
    Alex

    User Level

    Hello Alex,

    Plugin Organizer no longer works for this issue, map is loading fine but no stores are actually seen, 0 of 0 outlets.

    I tried dequeue and deregister too, but it gets ignored completely… :(

    Please advise!

    User Level

    After lots of tries with wp_enqueue_scripts, wp_print_styles etc… I managed to remove all scripts with the following code if anyone needs it:


    function deregister_store_locator() {
    if ( !is_page('dealer-locator') ) {
    wp_dequeue_style('mega-font-awesome');
    wp_deregister_style('mega-font-awesome');
    wp_dequeue_style('mega-normalize');
    wp_deregister_style('mega-normalize');
    wp_dequeue_style('mega-superstorefinder');
    wp_deregister_style('mega-superstorefinder');

    wp_dequeue_script( 'mega-modernize' );
    wp_deregister_script( 'mega-modernize' );
    wp_dequeue_script( 'mega-polyfills' );
    wp_deregister_script( 'mega-polyfills' );
    wp_dequeue_script( 'mega-homebrew' );
    wp_deregister_script( 'mega-homebrew' );
    wp_dequeue_script( 'mega-fastclicks' );
    wp_deregister_script( 'mega-fastclicks' );
    wp_dequeue_script( 'mega-init' );
    wp_deregister_script( 'mega-init' );
    wp_dequeue_script( 'mega-openclose' );
    wp_deregister_script( 'mega-openclose' );
    wp_dequeue_script( 'mega-Cluster' );
    wp_deregister_script( 'mega-Cluster' );
    wp_dequeue_script( 'mega-superstorfinder' );
    wp_deregister_script( 'mega-superstorfinder' );
    wp_dequeue_script( 'mega-comment' );
    wp_deregister_script( 'mega-comment' );
    wp_dequeue_script( 'mega-rating' );
    wp_deregister_script( 'mega-rating' );
    }
    }

    add_action( 'wp_head', 'deregister_store_locator', 9999 );

    Cheers!

    Alex
    StaffUser Level

    Hi Iggy,

    That’s great, thanks for the code snippet which might be useful to anyone that needs it.

    Cheers,
    Alex

    Sofia Moss
    StaffSuper ContributorUser Level

    This option to load plugin assets has been added in patch 6.1 here.

The topic ‘Load plugin assets (js and css files) only when needed’ is closed to new replies.