0

There are lot of unused js & stylesheets in my theme.

here is wp_register function in my functions.php

wp_register_script( 'moder',    base_url . 'js/modtom.js', array(), false, false );
wp_register_script( 'videojs',  base_url . 'js/vin.js',    array(), false, false );
wp_register_script( 'selevizr', base_url . 'js/sesin.js',  array('jquery'), false, true );
wp_register_script( 'easi',     base_url . 'js/gmin.js',  array('jquery'), false, true );

wp_register_style( 'styles',     base_url . 'style.css', null, false );

i want these scripts to be run when required or when shortcode use.

1

1 Answer 1

2

You can create a shortcode:

function load_scripts() {
  wp_enqueue_script('moder');
  wp_enqueue_style('styles');
}

add_shortcode('load_scripts', 'load_scripts');

Then use the shortcode to enqueue the scripts/styles:

[load_scripts]

Or so:

do_shortcode('[load_scripts]');

In terms of running when required, you have to make sure that if you're loading something that requires these, that you either execute the shortcode or enqueue those scripts/styles, since WordPress won't automatically know when you need them.

As a side note, wrap the function either in a class or give it a unique name to avoid collisions.

Resources:

Shortcode API

do_shortcode function

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.