Skip to content

Instantly share code, notes, and snippets.

@lloc
Last active December 24, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lloc/6715833 to your computer and use it in GitHub Desktop.
Save lloc/6715833 to your computer and use it in GitHub Desktop.
WordPress: How to include CSS and JS in a smart way
<?php
function my_enqueue_scripts() {
wp_enqueue_style( 'my-style', get_stylesheet_uri() );
wp_enqueue_script(
'my-script',
get_template_directory_uri() . '/js/script.js',
array( 'jquery' ),
false,
true
);
wp_localize_script(
'my-script',
'objectL10n',
array(
'str_hello_world' => __( 'Hello world!', 'my-textdomain' ),
)
);
if ( is_front_page() ) {
wp_enqueue_style(
'front-style',
get_template_directory_uri() . '/css/front.css'
);
wp_enqueue_script(
'front-script',
get_template_directory_uri() . '/js/front.js',
array(),
false,
true
);
}
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment