Skip to content

Instantly share code, notes, and snippets.

@vanaf1979
Last active January 3, 2020 20:02
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 vanaf1979/47e63bbc9940983e911ef50668524d0f to your computer and use it in GitHub Desktop.
Save vanaf1979/47e63bbc9940983e911ef50668524d0f to your computer and use it in GitHub Desktop.
Add a action to enqueue stylesGist for my "WorpdPress: Css Styles and Javascripts in theme development (In depth)" article: https://since1979.dev/wordpress-css-styles-and-javascripts-in-theme-development-in-depth/
<?php
function my_theme_enqueue_styles()
{
$mytheme = wp_get_theme();
wp_enqueue_style( $mytheme->get('TextDomain') . '-purecss', 'https://unpkg.com/purecss@1.0.0/build/pure-min.css', array(), $mytheme->get('Version') , 'screen' );
wp_enqueue_style( $mytheme->get('TextDomain') . '-purecss-ie8' , 'https://unpkg.com/purecss@1.0.0/build/grids-responsive-old-ie-min.css', array() , $mytheme->get('Version') , 'screen' );
wp_style_add_data( $mytheme->get('TextDomain') . '-purecss-ie8' , 'conditional' , 'lte IE 8' );
wp_enqueue_style( $mytheme->get('TextDomain') . '-purecss-ie9' , 'https://unpkg.com/purecss@1.0.0/build/grids-responsive-min.css', array() , $mytheme->get('Version') , 'screen' );
wp_style_add_data( $mytheme->get('TextDomain') . '-purecss-ie9' , 'conditional' , 'gt IE 8' );
wp_enqueue_style( $mytheme->get('TextDomain') . '-styles', get_template_directory_uri() . '/style.css', array(), $mytheme->get('Version') , 'screen' );
wp_register_style( $mytheme->get('TextDomain') . '-inline-css', false );
wp_enqueue_style( $mytheme->get('TextDomain') . '-inline-css' );
wp_add_inline_style( $mytheme->get('TextDomain') . '-inline-css', '* { color: red; }' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_scripts()
{
$mytheme = wp_get_theme();
wp_deregister_script('jquery');
wp_register_script( $mytheme->get('TextDomain') . '-scripts', get_template_directory_uri() . '/public/js/app.js', array(), $mytheme->get('Version') , true );
$data_array = array(
'some_string' => __( 'Some string to translate', 'textdomain' ),
'themepath' => get_template_directory_uri()
);
wp_localize_script( $mytheme->get('TextDomain') . '-scripts', 'object_name', $data_array );
wp_enqueue_script( $mytheme->get('TextDomain') . '-scripts' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_scripts' );
function my_theme_optimize_style_tags( $html , $handle , $href , $media )
{
return "<link rel='stylesheet' id='{$handle}' href='{$href}' media='{$media}' />" . "\n";
}
add_filter('style_loader_tag', 'my_theme_optimize_style_tags', 10, 4);
function my_theme_optimize_script_tags( $tag , $handle , $src )
{
$defer = ( $handle == 'my-theme-scripts' ) ? 'async defer' : '';
return "<script id='{$handle}' {$defer} src='{$src}'></script>" . "\n";
}
add_filter('script_loader_tag', 'my_theme_optimize_script_tags', 10, 3);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment