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.

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

| improve this answer | |
  • +1, but note it only works for 3.3+ – Stephen Harris Oct 11 '12 at 10:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.