Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@heyawhite
Last active March 15, 2017 18:40
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 heyawhite/1a2d244c4544951371082a043ed51df3 to your computer and use it in GitHub Desktop.
Save heyawhite/1a2d244c4544951371082a043ed51df3 to your computer and use it in GitHub Desktop.
Add scripts to WP
<?php
/** General add scripts **/
function adding_scripts_to_footer() {
// jQuery is already a part of the WP library. See: https://developer.wordpress.org/reference/functions/wp_enqueue_script/
wp_enqueue_script('jquery');
// Additional scripts, it's best to always register and then enqueue
wp_register_script('my_amazing_script', plugins_url('../js/amazing_script.js', _FILE_), array('jquery'), '1.1', true);
wp_enqueue_script('my_amazing_script');
wp_register_script('main-js', plugins_url('../js/script.js', _FILE_), array('jquery'), '1.0', true);
wp_enqueue_script('main-js');
}
add_action ('wp_enqueue_scripts', 'adding_scripts_to_footer');
/** Add a script on a specific page **/
function adding_page_scripts() {
if (is_home() || is_front_page()) {
wp_register_script('my_home_script', plugins_url('../js/home_script.js', _FILE_), array('jquery'), '1.05', true);
wp_enqueue_script('my_home_script');
}
if (is_home() || is_front_page()) {
wp_register_script('my_home_script', plugins_url('../js/home_script.js', _FILE_), array('jquery'), '1.05', true);
wp_enqueue_script('my_home_script');
}
if (is_page( array( 'about-us', 'contact', 'management' ) ) {
wp_register_script('pagescript', plugins_url('../js/page_script.js', _FILE_), array('jquery'), '1.75', true);
wp_enqueue_script('pagescript');
}
}
add_action ('wp_enqueue_scripts', 'adding_page_scripts');
/** add a script if a shortcode exists**/
function script_for_shortcode($content) {
if ( has_shortcode($content, "hello-shortcode") ) {
wp_register_script( 'plugin_script', plugins_url('../js/hello-shortcode.js'), array(), '1.0', true );
wp_enqueue_script('plugin_script');
}
}
add_filter( 'the_content', 'script_for_shortcode');
/** Add stylesheets **/
function theme_stylesheets() {
wp_enqueue_style( 'carousel-sheet', get_template_directory_uri() .'/css/carousel.css', array(), null, 'all' );
wp_enqueue_style( 'mailstyle', get_stylesheet_uri(), '', null, 'all' );
}
add_action( 'wp_enqueue_scripts', 'theme_stylesheets' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment