Skip to content

Instantly share code, notes, and snippets.

@LeanSeverino1022
Last active April 5, 2020 04:21
Show Gist options
  • Save LeanSeverino1022/dd5bccef75d391689f40a24ac0d16021 to your computer and use it in GitHub Desktop.
Save LeanSeverino1022/dd5bccef75d391689f40a24ac0d16021 to your computer and use it in GitHub Desktop.
[Enqueue scripts and styles ] #wordpress

Add custom CSS / JS file in theme

add_action( 'wp_enqueue_scripts', 'custom_site_files');

function custom_site_files() {
	wp_enqueue_script('main-university-js', get_theme_file_uri('/js/scripts-bundled.js'))
    wp_enqueue_style('font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
    wp_enqueue_style( 'another-style', get_template_directory_uri() . '/css/reset.css');
    
    //if your style.css is just in the main them folder, you can also try..
    wp_enqueue_style('main-style', get_stylesheet_uri());
}

add inline javascript

wp_add_inline_script

ex 1

	function mytheme_enqueue_typekit() {
  		 wp_enqueue_script( 'mytheme-typekit', 'https://use.typekit.net/.js', array(), '1.0' );
   		 wp_add_inline_script( 'mytheme-typekit', 'try{Typekit.load({ async: true });}catch(e){}' );
	}
	add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_typekit' );

result: Which results in:

<script type="text/javascript" src="https://use.typekit.net/.js?ver=1.0"></script> <script type="text/javascript"> try{Typekit.load({ async: true });}catch(e){} </script>

ex 2 - by billerickson

https://www.billerickson.net/code/add-inline-script/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment