Is it possible to call wp_enqueue_scripts dynamically?

For example, I want to use in function.php my own function

addThemeJS( true, 'modernizr', THEME_JS_DIR . "/plugins/modernizr.custom.js", array(), false, false );
addThemeJS( true, 'somthing-else', THEME_JS_DIR . "/plugins/somthing-else.js", array(), false, false );

In my core function file:

function addThemeJS( $enqueueThemeJS, $handle, $src, $deps, $ver, $in_footer ) {

    function addJS( $handle, $src, $deps, $ver, $in_footer ) {

        wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );


     add_action('wp_enqueue_scripts', 'addJS');


... but I can not pass variables ( $handle, $src ) here that I need

Suggest me a solution to this problem, please

I'm just trying to make something like a mini framework for creating basic things in WordPress. for example, to create the menu, I use this function

/ / Create menu:
function addThemeMenu ($ showThemeMenu, $ location, $ description) {

if ($ showThemeMenu):

register_nav_menu ($ location, $ description);



and then call

addThemeMenu (true, 'main', __ ('Main Navigation', 'Theme'));

it's very fast and do not have to rewrite from project to project.

  • 2
    Can you please explain what you're actually trying to accomplish? It's not readily apparent from reading your question why you want to create such a wrapper function, pass variables, and enqueue dynamically. Commented Nov 30, 2012 at 15:42
  • I belive rezonans whants to read about lambda-style functions in php ( e.g. php.net/create-function ). Commented Nov 30, 2012 at 15:49
  • Please, update the very Question for clarifications of this type. You can edit it freely whenever needed.
    – brasofilo
    Commented Nov 30, 2012 at 16:06
  • The point of a framework would be to save time in creating. Your wrapper function just saves a call to add_action and writing a callback function -- not really worth it perhaps? I wrote a framework to automate a lot of WP stuff you may want to check it out for some ideas (PHP 5.3+ required to use it). Commented Nov 30, 2012 at 16:06
  • Your clarification uses register_nav_menu() rather than wp_enqueue_script(), resulting in the question still not especially clear or answerable. Commented Nov 30, 2012 at 17:29

1 Answer 1


You could use anonymous functions in PHP 5.3+ to do this.

function addThemeJS($enqueueThemeJS, $handle, $src, $deps, $ver, $in_footer)
    add_action('wp_enqueue_scripts', function() use ($handle, $src, $deps, $ver, $in_footer) {
         wp_enqueue_script($handle, $src, $deps, $ver, $in_footer);

If you're using this a personal project where you know the server has 5.3+ you would be okay if a bit weird. If it's something you want to release in the WP.org repo, you probably want to find another way.

You might be better of creating some sort of wrapper object and using this.

// add the action

// register scripts...
WPSE74479_Enqueue::register('modernizr', THEME_JS_DIR . "/plugins/modernizr.custom.js", array(), false, false);

class WPSE74479_Enqueue
    private static $ins = null;

    private $scripts = array();

    public static function instance()
        is_null(self::$ins) && self::$ins = new self;
        return self::$ins;

    public static function init()
        add_action('wp_enqueue_scripts', array(self::instance(), 'enqueue'));

    public static function register($hndl, $src, $deps=array(), $ver=null, $footer=false)
        self::instance()->scripts[$hndl] = array(
            'src'       => $src,
            'deps'      => $deps,
            'ver'       => $ver,
            'footer'    => $footer,

    public function enqueue()
        foreach($this->scripts as $hdnl => $script)
  • 1
    Please don't use anonymous functions in filter/action hooks. Doing so makes the callbacks unable to be unhooked. Commented Nov 30, 2012 at 17:29
  • thanks for the help, but the solution without using an anonymous function not found
    – rezonans
    Commented Nov 30, 2012 at 18:14

Your Answer

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

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