月別アーカイブ: 2017年12月

wp JS defer

/*	
* Getting script tags
* Thanks http://wordpress.stackexchange.com/questions/54064/how-do-i-get-the-handle-for-all-enqueued-scripts
*/

add_action( 'wp_print_scripts', 'wsds_detect_enqueued_scripts' );
function wsds_detect_enqueued_scripts() {
	global $wp_scripts;
	foreach( $wp_scripts->queue as $handle ) :
		echo $handle . ' | ';
	endforeach;
}

 

admin-bar | contact-form-7 | maxbuttons-front | jquery | twentysixteen-html5 | twentysixteen-skip-link-focus-fix | twentysixteen-script |

 

add_filter( 'script_loader_tag', 'wsds_defer_scripts', 10, 3 );
function wsds_defer_scripts( $tag, $handle, $src ) {

	// The handles of the enqueued scripts we want to defer
	$defer_scripts = array( 
		'admin-bar',
		'contact-form-7',
		'maxbuttons-front',
		' jquery',
		'wpshout-js-cookie-demo',
		'twentysixteen-html5',
		'twentysixteen-skip-link-focus-fix',
		'twentysixteen-script',
	);

    if ( in_array( $handle, $defer_scripts ) ) {
        return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n";
    }
    
    return $tag;
} 

Child theme enqueue function

In funcions.php of child theme:

function my_theme_enqueue_styles() {

$parent_style = ‘parent-style’; // This is ‘twentyfifteen-style’ for the Twenty Fifteen theme.

Replace  ‘parent-style’ with actual parent theme;  like  ‘twentysixteen-style’.

Otherwise, css loading is doubled with strange( parent theme version?)  version name.

SpeedUp jsstour.jp (1)

1.remove render blocking JS (tentative, not ideal)

1.1 Insert following extra script in header.php just above <?php wp_head(); ?>  in both of child2 and child theme.

<script type=’text/javascript’ src=’http://japansightseeingtour.jp/wp-includes/js/jquery/jquery.js?ver=1.12.4′ async></script>

1.2.Insert the following in functions.php in both of child themes.

// Begin Custom Scripting to Move JavaScript from the Head to the Footer

function remove_head_scripts() {
remove_action(‘wp_head’, ‘wp_print_scripts’);
remove_action(‘wp_head’, ‘wp_print_head_scripts’, 9);
remove_action(‘wp_head’, ‘wp_enqueue_scripts’, 1);

add_action(‘wp_footer’, ‘wp_print_scripts’, 5);
add_action(‘wp_footer’, ‘wp_enqueue_scripts’, 5);
add_action(‘wp_footer’, ‘wp_print_head_scripts’, 5);
}
add_action( ‘wp_enqueue_scripts’, ‘remove_head_scripts’ );

// END Custom Scripting to Move JavaScript

2. Activate “autoptimize”.