Skip to content

Instantly share code, notes, and snippets.

Nico Martin nico-martin

Block or report user

Report or block nico-martin

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@nico-martin
nico-martin / PostList.jsx
Last active Apr 25, 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 (
@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 / 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 / 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 / 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 / .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>
View loadfonts.js
function loadFont(t, e, n) {
return new Promise(resolve => {
const a = navigator.userAgent;
if (window.addEventListener && (!a.match(/(Android (2|3|4.0|4.1|4.2|4.3))|(Opera (Mini|Mobi))/) || a.match(/Chrome/))) {
let o = {};
try {
o = localStorage || {}
} catch (t) {}
const r = t, i = r + "url", s = r + "css", d = o[i], c = o[s], l = document.createElement("style");
if (l.rel = "stylesheet", document.head.appendChild(l), !c || d !== e && d !== n) {
@nico-martin
nico-martin / nm-media-sideload.php
Last active May 19, 2019
Sideload an Image from a Post-Meta to your attachments
View nm-media-sideload.php
<?php
namespace NicoMartin;
/**
* Media Sideload
* Plugin Name: NM Media Sideload
* Description: Sideload an Image from a Post-Meta to your attachments
* Version: 0.1.0
* Author: Nico Martin
@nico-martin
nico-martin / post-as-frontpage.php
Last active Aug 2, 2018
Set post as WP Front-Page
View post-as-frontpage.php
<?php
add_filter( 'get_pages', 'add_posts_to_frontpage_dropdown', 20, 2 );
add_action( 'pre_get_posts', 'update_frontpage_post_query' );
function add_posts_to_frontpage_dropdown( $pages, $r ) {
if ( array_key_exists( 'name', $r ) && 'page_on_front' == $r['name'] ) {
$args = [
'post_type' => 'post',
@nico-martin
nico-martin / class-navwalker.php
Last active Jul 10, 2018
A WordPress Navigation Walker that adds a clean BEM CSS Structure
View class-navwalker.php
<?php
namespace HelloTheme;
class NavWalker extends \Walker_Nav_Menu {
public $css_base = 'menu';
public function __construct( $base = '' ) {
You can’t perform that action at this time.