Skip to content

Instantly share code, notes, and snippets.

View mattheu's full-sized avatar

Matthew Haines-Young mattheu

View GitHub Profile
@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:
@mattheu
mattheu / block.js
Created April 18, 2018 09:41
Gutenberg - Test case custom block for passing content to render callback
const el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
InnerBlocks = wp.blocks.InnerBlocks;
registerBlockType( 'gutenberg-boilerplate-es5/hello-world-step-01', {
title: 'Hello World (Step 1)',
icon: 'universal-access-alt',
category: 'layout',
!function(a,b,c,d,e,f,g,h,i){function j(a){var b,c=a.length,e=this,f=0,g=e.i=e.j=0,h=e.S=[];for(c||(a=[c++]);d>f;)h[f]=f++;for(f=0;d>f;f++)h[f]=h[g=s&g+a[f%c]+(b=h[f])],h[g]=b;(e.g=function(a){for(var b,c=0,f=e.i,g=e.j,h=e.S;a--;)b=h[f=s&f+1],c=c*d+h[s&(h[f]=h[g=s&g+b])+(h[g]=b)];return e.i=f,e.j=g,c})(d)}function k(a,b){var c,d=[],e=typeof a;if(b&&"object"==e)for(c in a)try{d.push(k(a[c],b-1))}catch(f){}return d.length?d:"string"==e?a:a+"\0"}function l(a,b){for(var c,d=a+"",e=0;e<d.length;)b[s&e]=s&(c^=19*b[s&e])+d.charCodeAt(e++);return n(b)}function m(c){try{return o?n(o.randomBytes(d)):(a.crypto.getRandomValues(c=new Uint8Array(d)),n(c))}catch(e){return[+new Date,a,(c=a.navigator)&&c.plugins,a.screen,n(b)]}}function n(a){return String.fromCharCode.apply(0,a)}var o,p=c.pow(d,e),q=c.pow(2,f),r=2*q,s=d-1,t=c["seed"+i]=function(a,f,g){var h=[];f=1==f?{entropy:!0}:f||{};var o=l(k(f.entropy?[a,n(b)]:null==a?m():a,3),h),s=new j(h);return l(n(s.S),b),(f.pass||g||function(a,b,d){return d?(c[i]=a,b):a})(function(){