Skip to content

Instantly share code, notes, and snippets.

View mattheu's full-sized avatar

Matthew Haines-Young mattheu

View GitHub Profile
<?php
/**
* Plugin Name: Speculative Loading Eager Prerender
* Description: Overrides WordPress speculative loading rules to use "eager" and "prerender" for better perceived performance.
* Version: 1.0
* Author: Your Name
*/
add_filter(
'wp_speculation_rules_configuration',
<?php
/**
* Plugin Name: Speculative Loading Eager Prerender Demo Plugin
* Description: Overrides WordPress speculative loading rules to use "eager" and "prerender" for better perceived performance.
* Version: 1.0
* Author: Matthew Haines-Young
*/
// Hook into the filter WordPress uses to generate the speculationrules script.
add_filter('wp_speculation_rules', function($rules) {
@mattheu
mattheu / gist:21481c374a207acfe8c7ca3846763e87
Last active December 1, 2023 17:52
Altis consent - Defer loading of scripts.
<?php
/**
* Defer loading of scripts until consent given (using Altis consent API).
*
* Note this requires the script to be enqueued in the normal manner.
* Scripts are loaded async so must be compatible with this, can't have dependencies etc.
*
* @package
*/
import withUniqueId from './with-unique-id';
function FormField({
id,
label,
value,
onChange,
}) {
return (
<div>
@mattheu
mattheu / annotation-example.js
Last active February 23, 2023 13:00
Example of how to add annotations to Gutenberg
// Make sure you specify the script "wp-annotations" as a dependency of your code that uses the WP annotations API.
const { select, dispatch } = wp.data;
const ANNOTATION_NS = 'core/annotations';
const ANNOTATION_SOURCE = 'testing-annotations';
// Default attributes for blockTypes.
// richTextIdentifier - this is the ID of the richTextField we want to add the annotation to.
const blockTypes = {
@mattheu
mattheu / block-ad.js
Last active February 1, 2021 21:39
Automatic Gutenberg Ad Placement
/* global wp */
import edit from './edit';
import defaultOptions from '../default';
const { __ } = wp.i18n;
export const name = 'rbmh/ad';
export default Edit;
const { subscribe } = wp.data;
const { createBlock } = wp.blocks;
const createAdBlock = () => createBlock( AdBlock.name, { size: 'mpu' } );
subscribe( e => {
const {
getBlocks,
} = wp.data.select( 'core/editor' );
@mattheu
mattheu / gist:7ebc5efd2b2571b928f0495dc6684fd0
Created October 31, 2018 17:14
Remove gutenberg core blocks.
// Array of blocks to hide.
// This could come from anywhere - we use localize script and change the blocks to hide depending on post type.
const hiddenBlocks = [ 'hm/myBlock' ];
// Use function below to filter block settings.
// Will hide all 'hiddenBlocks' from the inserter UI
// But if those blocks are already present, or somehow get into the content, they will still work.
wp.hooks.addFilter(
'blocks.registerBlockType',
'hm-gb-blocks/modify-core-blocks',
/* global wp */
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'proptypes';
const { Component } = wp.element;
class Iframe extends Component {
propTypes = {
@mattheu
mattheu / README.md
Last active July 13, 2018 14:16
Gutenberg Bug - dynamic block with loop in render_callback.

If you have a dynamic gutenberg block, and in the PHP render_callback, your code includes a custom query and loop, then it will break the editor.

This test plugin demonstrates the issue.

  • Create a new page.
  • Add the 'Demo Dynamic Post Block' block
  • Add one or more image IDs. Comma separated (e.g. 12, 523 )
  • Save a draft.
  • If you preview the page - this will work as expected. You will see the image titles and IDs.
  • If you reload the page in the editor, the page will break with the following JS error: