PHP code example of wearerequired / h2push

1. Go to this page and download the library: Download wearerequired/h2push library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wearerequired / h2push example snippets


// Force H2 Push to always use the `<link>` element.
add_filter( 'h2push.as_header', '__return_false' );

/**
 * Add web font and hero image to the list of resources to push/preload.
 *
 * @param array $resources List of resources.
 * @return array List of resources.
 */
function my_theme_push_resources( array $resources ): array {
	$relative_template_directory_uri = wp_parse_url( get_template_directory_uri(), PHP_URL_PATH );

	// Push web font.
	$resources[] = [
		'href' => $relative_template_directory_uri . '/assets/fonts/fancy.woff2',
		'as'   => 'font',
		'type' => 'font/woff2',
		'crossorigin',
	];

	if ( is_front_page() && ! is_paged() ) {
		// Push hero image.
		$resources[] = [
			'href' => $relative_template_directory_uri . '/assets/images/hero.webp',
			'as'   => 'image',
			'type' => 'image/webp',
		];
	}

	return $resources;
}
add_filter( 'h2push.push_resources', 'my_theme_push_resources' );

/**
 * Allow resources from example.org to be pushed/preloaded too.
 *
 * @param bool   $is_allowed Whether the host should be allowed. Default true for local resources.
 * @param string $host       The host name of the resource.
 * @return bool Whether the host should be allowed.
 */
function my_theme_is_allowed_push_host( $is_allowed, $host ) {
	return $is_allowed || 'example.org' === $host;
}
add_filter( 'h2push.is_allowed_push_host', 'my_theme_is_allowed_push_host' );