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
*/
@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 / gist:7127778
Created October 23, 2013 22:18
## Fix login loop. * When WordPress is installed in a subdirectory, wp-admin/ links don't work correctly. We fix this with an nginx rewrite. * But... it appends reauth=1 to the url which forces a reauthentication when submitted... and you have to log in again!
/**
* Fix login loop.
*
* When WordPress is installed in a subdirectory, wp-admin/ links don't work correctly. We fix this with an nginx rewrite.
* But... it appends reauth=1 to the url which forces a reauthentication when submitted... and you have to log in again!
*/
add_filter( 'login_url', function( $url ) {
if ( '/wp-admin/' === add_query_arg( array() ) ) {
@mattheu
mattheu / gist:508d3246eea566e6fa33
Last active May 15, 2021 19:44
Tidy Up WordPress TinyMCE 4 editor buttons
<?php
// Modify Tiny_MCE init
add_filter('tiny_mce_before_init', 'tmy_modify_tinyMCE4', 10, 2 );
/**
* Filter TinyMCE4 Init args.
*/
function tmy_modify_tinyMCE4( $mceInit, $editor_id ) {
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 / 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;
@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',
import withUniqueId from './with-unique-id';
function FormField({
id,
label,
value,
onChange,
}) {
return (
<div>
@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 = {