Skip to content

Instantly share code, notes, and snippets.

View adamsilverstein's full-sized avatar
💭
Working remotely, as usual

Adam Silverstein adamsilverstein

💭
Working remotely, as usual
View GitHub Profile
@adamsilverstein
adamsilverstein / wp_enqueue_non_blocking_script.php
Last active August 9, 2023 21:00
WordPress `wp_enqueue_script`: use `async` or `defer` in a backwards compatible manner
<?php
/**
* Register scripts with a `defer` or `async` strategy in a backwards compatible manner.
*
* From WordPress 6.3 onwards, the `wp_register_script` function accepts an `$args` array that
* can include a `strategy` key with a value of either `async` or `defer`.
*
* This helper function handles the backwards compatibility for older versions of WordPress. When a
* `strategy` key is present in the `$args` array (and is either `defer` or `async`), the
@adamsilverstein
adamsilverstein / add-lazy-loading-to-oembeds.php
Last active November 22, 2023 22:16
Add lazy loading to oEmbed iframes (in WordPress)
<?php
/**
* Add lazy loading to oEmbed iframes.
*
* @wordpress-plugin
* Plugin Name: Lazy Load oEmbeds.
* Description: Add lazy loading to oEmbed iframes.
* Plugin URI:
* Version: 1.0.0
* Author: Adam Silverstein, Google
CREATE TEMP FUNCTION getFeatures(payload STRING)
RETURNS STRING
LANGUAGE js
AS '''
try {
var $ = JSON.parse(payload);
var almanac = JSON.parse($._almanac);
var generators = almanac['meta-nodes'].nodes.find(node => node.name == 'generator' && node.content.startsWith("Elementor"));
if ( generators.length == 0 ) {
return "";
CREATE TEMP FUNCTION IS_GOOD(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64)
RETURNS BOOL
AS (
good / (good + needs_improvement + poor) >= 0.75
);
CREATE TEMP FUNCTION IS_NON_ZERO(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64)
RETURNS BOOL
AS (
good + needs_improvement + poor > 0
class Custom_REST_Users_Controller extends WP_REST_Users_Controller {
/**
* Registers the routes for the controller.
*/
public function register_routes() {
parent::register_routes();
add_filter( 'rest_user_query', array( $this, 'filter_user_query_by_meta_fields' ), 10, 2 );
}
#standardSQL
# Breakdown of scripts using Async, Defer, Module or NoModule attributes. Also breakdown of inline vs external scripts
CREATE TEMPORARY FUNCTION getScripts(payload STRING)
RETURNS
STRUCT<
total INT64,
inline INT64,
src INT64,
async INT64,
# Breakdown of scripts using Async, Defer, Module or NoModule attributes. Also breakdown of inline vs external scripts
CREATE TEMPORARY FUNCTION getScripts(payload STRING)
RETURNS
STRUCT<
total INT64,
inline INT64,
src INT64,
async INT64,
defer INT64,
async_and_defer INT64,
@adamsilverstein
adamsilverstein / drupal-cwv-by-version.sql
Created October 26, 2022 17:04
Drupal Core Web Vitals by Version
CREATE TEMP FUNCTION IS_GOOD(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64) RETURNS BOOL AS (
good / (good + needs_improvement + poor) >= 0.75
);
CREATE TEMP FUNCTION IS_NON_ZERO(good FLOAT64, needs_improvement FLOAT64, poor FLOAT64) RETURNS BOOL AS (
good + needs_improvement + poor > 0
);
#standardSQL
# Breakdown of scripts using Async, Defer, Module or NoModule attributes. Also breakdown of inline vs external scripts
CREATE TEMPORARY FUNCTION getScripts(payload STRING)
RETURNS
STRUCT<
total INT64,
inline INT64,
src INT64,
async INT64,
defer INT64,
WITH wordpress AS (
SELECT DISTINCT
url
FROM
`httparchive.technologies.2022_09_01_*`
WHERE
app = 'WordPress'
)
SELECT