Wordpress enqueue script
(function( $ ) {
"use strict";
// javascript code here. i.e.: $(document).ready( function(){} );
})(jQuery);
/**
* PLUGIN
* Enqueue scripts and styles - for all admin pages.
*/
function plugin_name_scripts()
{
wp_enqueue_style('bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', false, '1.0.0');
wp_enqueue_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', array(), '1.0.0', true);
wp_enqueue_script('bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array(), '1.0.0', true);
// wp_enqueue_style( 'style-name', get_stylesheet_uri() );
// wp_enqueue_script('script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true);
}
add_action('admin_enqueue_scripts', 'plugin_name_scripts');
/**
* PLUGIN
* Enqueue scripts and styles - for specific admin pages.
*/
function plugin_name_scripts($hook)
{
// debug_console('HOOK-> ' . $hook);
if ('toplevel_page_kcq_manual' != $hook) {
return;
}
wp_enqueue_style('bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', false, '1.0.0');
wp_enqueue_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', array(), '1.0.0', true);
wp_enqueue_script('bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array(), '1.0.0', true);
// wp_enqueue_style( 'style-name', get_stylesheet_uri() );
// wp_enqueue_script('script-name', get_template_directory_uri() . '/js/example.js', ['jquery'], '1.0.0', true);
}
add_action('admin_enqueue_scripts', 'plugin_name_scripts');
/**
* New Page admin page
* Enqueue scripts and styles
*/
function add_new_page_script($hook) {
if ( $hook == "post-new.php" && isset($_GET['post_type']) && $_GET['post_type'] == "page" )
{
$pluginPrefix = "my-prefix";
wp_enqueue_script( "{$pluginPrefix}main-js", plugins_url('assets/js/main.js'), ['jquery'] );
}
}
add_action( 'admin_enqueue_scripts', 'add_new_page_script' );
/**
* THEME
* Enqueue scripts and styles
*/
add_action( 'admin_enqueue_scripts', function($hook) {
$pluginPrefix = "my-prefix";
wp_enqueue_script( "{$pluginPrefix}main-js", plugins_url('assets/js/main.js'), ['jquery'] );
} );
/**
* THEME
* Enqueue scripts and styles - only homepage
*/
add_action( 'admin_enqueue_scripts', function($hook) {
if (!is_home())
return;
//if ( !is_page('about'))
// return;
$pluginPrefix = "my-prefix";
wp_enqueue_script( "{$pluginPrefix}main-js", plugins_url('assets/js/main.js'), ['jquery'] );
} );
/**
* Never worry about cache again!
*/
function my_load_scripts($hook) {
// create my own version codes
$my_js_ver = date("ymd-Gis", filemtime( plugin_dir_path( __FILE__ ) . 'js/custom.js' ));
$my_css_ver = date("ymd-Gis", filemtime( plugin_dir_path( __FILE__ ) . 'style.css' ));
//
wp_enqueue_script( 'custom_js', plugins_url( 'js/custom.js', __FILE__ ), array(), $my_js_ver );
wp_register_style( 'my_css', plugins_url( 'style.css', __FILE__ ), false, $my_css_ver );
wp_enqueue_style ( 'my_css' );
}
add_action('wp_enqueue_scripts', 'my_load_scripts');
/* REMOVE Unnecessary scripts
https://www.sitepoint.com/enqueuing-scripts-styles-wordpress/
*/
add_action( 'admin_enqueue_scripts', function($hook) {
$unautorized_styles = [
'script1',
'another-script'
];
foreach ( $unautorized_styles as $handle )
{
wp_deregister_style( $handle );
}
// enqueue my scripts
} );
wp_enqueue_script should be called using the wp_enqueue_scripts action hook if you want to call it on the front-end of the site, like in the examples above. To call it on the administration screens, use the admin_enqueue_scripts action hook. For the login screen, use the login_enqueue_scripts action hook.
wp_deregister_script() and register the script again with the new parameters.
define('VERSON', 1);
Included scripts: https://developer.wordpress.org/reference/functions/wp_enqueue_script/
home_url() Home URL http://www.example.com site_url() Site directory URL http://www.example.com or http://www.example.com/wordpress admin_url() Admin directory URL http://www.example.com/wp-admin includes_url() Includes directory URL http://www.example.com/wp-includes content_url() Content directory URL http://www.example.com/wp-content plugins_url() Plugins directory URL http://www.example.com/wp-content/plugins theme_url() Themes directory URL (#18302) http://www.example.com/wp-content/themes wp_upload_dir() Upload directory URL (returns an array) http://www.example.com/wp-content/uploads