Read about this function on the WordPress.org code reference.
define('SBRP_PLUGIN_PATH', plugins_url('', __FILE__));
function function_name() {
wp_enqueue_script( 'handle_name', SBRP_PLUGIN_PATH . '/utility.js' );
// A registered script is not appended until you call wp_enqueue_script on the handle
wp_register_script( 'my_script', SBRP_PLUGIN_PATH . '/other_script.js' );
}
// Hook into wp_enqueue_scripts and call our function when executed
add_action( 'wp_enqueue_scripts', 'function_name' );
// If we are on the home page, link the registered script
if ( is_home() ) {
wp_enqueue_script( 'my_script' );
}
Add Javascript from your PHP code with wp_add_inline_script(). The example below adds Javascript stored in the $script variable to handle_name.
function function_name() {
wp_enqueue_script( 'handle_name', SBRP_PLUGIN_PATH . '/utility.js' );
}
add_action( 'wp_enqueue_scripts', 'function_name' );
$script = "jQuery(document).ready(function($) {
// my code
});";
wp_add_inline_script( 'handle_name', $script );
$handle (string)(Required)
A unique name identifying the script.
$src (string)(Optional) = ''
The source destination of the file to be enqueued as a URL or path relative to the WordPress root directory.
$deps (array)(Optional) = array()
An array of registered handle names that the enqueued file requires to run.
$ver (string|bool|null)(Optional) = false
The script version number to be added to the $src as a query string. If false, the WordPress version number is appended to the $src. If null, no version is applied.
$in_footer (bool)(Optional) = false
Insert the script at the end of the page or in the head of the page (default).
SmugBits is all about web development and related fields. From web pages to web apps, we are always following the pursuit of learning something new and exciting. Along our adventure, we publish reviews, tutorials, and other bits of information we find along the way.