Skip to content

Instantly share code, notes, and snippets.

@JPry
Created January 10, 2022 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPry/2d5db5cc7995eb9724f706037cd4bfdf to your computer and use it in GitHub Desktop.
Save JPry/2d5db5cc7995eb9724f706037cd4bfdf to your computer and use it in GitHub Desktop.
An example for including a JavaScript file into WordPress
<?php
/**
* Plugin Name: Example including a JS file into WordPress.
*/
add_action( 'wp_enqueue_scripts', 'jpry_example_include_scripts' );
function jpry_example_include_scripts() {
wp_enqueue_script(
'jpry-example-script', // This is an arbitrary name to reference this particular script
'https://example.com/wp-content/plugins/jpry-example/example.js', // This is the URL. For example purposes I've put the whole URL, but in WordPress, you'd usually have some other method for generating the URL for you
array(), // An array of other script names, if any, that this script depends on. These will be loaded prior to this script
'1.0.0', // This is a version string for browser caching purposes
);
}
'use strict';
console.log('Successfully loaded the example.js file!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment