Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
possible duplicate of Where is the right place to register/enqueue scripts & styles –  Chris_O Oct 11 '12 at 10:46
add comment

1 Answer 1

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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