Skip to content

Instantly share code, notes, and snippets.

Nico Martin nico-martin

View GitHub Profile
@nico-martin
nico-martin / .htaccess
Last active Jun 13, 2019
Cache-Control headers
View .htaccess
<ifModule mod_headers.c>
# One year for image and video files
<filesMatch ".(flv|gif|ico|jpg|jpeg|mp4|mpeg|png|svg|swf|webp)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>
# One month for JavaScript and PDF files
<filesMatch ".(js|pdf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
@nico-martin
nico-martin / ServerSideRender.jsx
Last active Jun 26, 2019
ServerSideRender block
View ServerSideRender.jsx
const {ServerSideRender} = wp.components;
wp.blocks.registerBlockType('test/ssr', {
title: 'Post Archiv',
icon: 'grid-view',
category: 'my-category',
edit(props) {
return <ServerSideRender block="test/ssr"/>
},
save() {
@nico-martin
nico-martin / block-ssr.js
Last active Jun 26, 2019
This is an example of a server side rendered block without any build step. It's all plain JS/PHP and it uses the helpers and components WordPress already offers in the block admin interface.
View block-ssr.js
(function (blocks, components, element) {
blocks.registerBlockType('test/ssr', {
title: 'Server Side Rendered Block',
icon: 'admin-site-alt3',
category: 'test',
edit: function (props) {
return element.createElement(components.ServerSideRender, {block: 'test/ssr'});
},
save: function () {
return null;
@nico-martin
nico-martin / InnerBlock-renderAppender-block.jsx
Last active Jul 9, 2019
Gutenberg InnerBlock with renderAppender
View InnerBlock-renderAppender-block.jsx
const {TextControl, IconButton} = wp.components;
const {InnerBlocks} = wp.editor;
const {__} = wp.i18n;
const {dispatch} = wp.data;
const {createBlock, registerBlockType} = wp.blocks;
registerBlockType('prefix/container', {
title: 'Container',
icon: 'category',
category: 'category',
@nico-martin
nico-martin / gtag-gdpr-ready.html
Last active Feb 24, 2020
This little snippet provides a simple way to a) implement Google Tag Manager with anonymized IP and b) provide a Opt-Out / Opt-In mechanism: gtagOptOutIn();
View gtag-gdpr-ready.html
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
<script>
window.gtagTrackingID = 'UA-XXXXXXXX-X';
window.gtagDisableStr = 'gtag-disable-' + gtagTrackingID;
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
@nico-martin
nico-martin / Polylang.php
Last active Apr 15, 2020
A PHP-Class to make fields translatable
View Polylang.php
<?php
namespace SayHello\Theme\Package;
/**
* @author Nico Martin <nico@sayhello.ch>
*/
class Polylang
{
public $translatedNames = [];
@nico-martin
nico-martin / PostList.jsx
Last active Jun 16, 2020
useApi.jsx - the correct way to fetch data with react hooks: extension of https://dev.to/nicomartin/the-right-way-to-fetch-data-with-react-hooks-48gc
View PostList.jsx
// ./PostList.jsx
import React from 'react';
import {apiStates, useApi} from './useApi.jsx'
const PostList = () => {
const { state, error, data, reload } = useApi('https://api.mysite.com');
switch (state) {
case apiStates.ERROR:
return (
You can’t perform that action at this time.