Skip to content

Instantly share code, notes, and snippets.

@robneu
Last active December 15, 2015 15:09
Show Gist options
  • Save robneu/5280112 to your computer and use it in GitHub Desktop.
Save robneu/5280112 to your computer and use it in GitHub Desktop.
Register a general javascript file using the EXAMPLE_JS_URL constant defined in the main plugin file
<?php
/**
* Enqueue a JavaScript file using a constant
* called EXAMPLE_JS_URL to get the plugin's js directory url.
*
* @author FAT Media <http://youneedfat.com>
* @copyright Copyright (c) 2013, FAT Media, LLC
* @license GPL-2.0+
*/
/**
* Enqueue with hook 'wp_enqueue_scripts',
* which can be used for front end CSS and JavaScript
*/
add_action( 'wp_enqueue_scripts', 'example_load_javascript' );
/**
* Enqueue plugin js-file
*/
function example_load_javascript() {
// Add a trailing slash to the js url
$js_url = trailingslashit( EXAMPLE_JS_URL );
// Enqueue general.js file which depends on jQuery
wp_enqueue_script( 'example-general', $js_url . 'general.js' , array( 'jquery' ), '1.0', true );
}
@robneu
Copy link
Author

robneu commented Jun 22, 2013

A full tutorial on defining and using plugin URLs in a WordPress Must Use plugin can be found at http://youneedfat.com/get-the-mu-plugin-directory-url/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment