Skip to content

Instantly share code, notes, and snippets.

View markhowellsmead's full-sized avatar

Mark Howells-Mead markhowellsmead

View GitHub Profile
@markhowellsmead
markhowellsmead / .htaccess
Last active March 19, 2024 20:25
Detect browser language and redirect to appropriate language version of the website
# Redirect visitors who request the root domain path (e.g. www.mywebsite.ch) to the appropriate language version
# Fallback to English version if no matching browser language defined
# Based on language version being at e.g. www.mywebsite.ch/de/
# This has no effect on any subpaths of the website, and therefore has no effect on e.g. WordPress admin.
# Using a 302 temporary redirect header stops the redirection from being cached in the browser.
# language is ES-MX (Mexico)
RewriteCond %{HTTP:Accept-Language} ^es-mx [NC]
RewriteRule ^$ /mx/ [L,R=302]
@markhowellsmead
markhowellsmead / operatingsystem.js
Last active March 7, 2024 20:57
Detect a device operating system using JavaScript. Windows OS will be more precisely recognized.
if(navigator.appVersion.indexOf("Windows ")!=-1){
os = getWindowsOS();
}else{
os = navigator.platform;
}
function getWindowsOS(){
// http://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx#PltToken
if(navigator.appVersion.indexOf("Windows NT 10.")!=-1){
return 'Windows 10';
@markhowellsmead
markhowellsmead / DownloadPDF.html
Created April 9, 2015 12:26
TYPO3: Link to a PDF in Fluid using a FAL field
# BE
<flux:field.inline.fal name="downloadFile" multiple="FALSE" allowedExtensions="pdf" />
# FE
<v:variable.set name="downloadFile" value="{v:content.resources.fal(field: 'downloadFile') -> v:iterator.first()}"/>
<a class="downlow" href="{downloadFile.url}">Download</a>
@markhowellsmead
markhowellsmead / event_query.php
Last active November 22, 2023 09:45
Complex WordPress meta query by start and end date (custom meta fields)
<?php
/**
* Complex WordPress meta query by start and end date (custom meta fields)
* Intended for use on the `pre_get_posts` hook.
* Caution; this makes the query very slow - several seconds - so should be
* implemented with some form of caching.
*
* mark@sayhello.ch 22.10.2019, based on code from 201 onwards
*/
@markhowellsmead
markhowellsmead / email_not_specific_domain.txt
Last active November 9, 2023 04:21
Regex for valid email address excluding specific free domains, like Gmail, GMX and Yahoo. Extend the list of domains according to your own requirements.
^([\w.-]+)@(\[(\d{1,3}\.){3}|(?!hotmail|gmail|googlemail|yahoo|gmx|ymail|outlook|bluewin|protonmail|t\-online|web\.|online\.|aol\.|live\.)(([a-zA-Z\d-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$
@markhowellsmead
markhowellsmead / update-post-template.php
Last active August 9, 2023 19:20
Update post template for posts matching a certain criteria
<?php
$args = [
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'any',
'meta_query' => [
[
'key' => 'hide_thumbnail',
'compare' => 'EXISTS'
@markhowellsmead
markhowellsmead / index.js
Last active March 9, 2023 15:12
First example of GitHub Copilot. Gets posts from the WordPress REST API and appends them to the postContainer.
// get the current site's REST API base url
const restApiBaseUrl = wpApiSettings.root;
// get the element with the class name '.posts-container' from the document
const postsContainer = document.querySelector('.posts-container');
// use restApiBaseUrl to get all of the posts from the rest api in a recursive async function. wrap the function in an IIFE.
// append the posts to the posts container as objects. add a class name to each element using the classNameBase 'shp-my-posts' and the post id.
(async function getPosts() {
// stop processing if there is no posts container
@markhowellsmead
markhowellsmead / index.js
Created June 10, 2021 14:17
WordPress Gutenberg - transform core list block to custom block with multiple InnerBlocks.
transforms: {
from: [
{
type: 'block',
blocks: ['core/list'],
transform: ({ values }) => {
let entries = values.split('</li><li>');
const link_pattern = /<a href=['"]#endnote[0-9]+['"]>([0-9]+)<\/a>/gi;
entries.forEach((entry, index) => {
entries[index] = entry.replace('<li>', '').replace('</li>', '');
@markhowellsmead
markhowellsmead / array_find.js
Created April 20, 2016 14:49
Polyfill JavaScript Array.prototype.find for older browsers (e.g. IE 10, IE 11)
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
@markhowellsmead
markhowellsmead / full-codes.js
Created January 16, 2021 15:51 — forked from phpbits/full-codes.js
Extend core Gutenberg blocks with custom attributes and settings. View full tutorials here : https://jeffreycarandang.com/extending-gutenberg-core-blocks-with-custom-attributes-and-controls/
/**
* External Dependencies
*/
import classnames from 'classnames';
/**
* WordPress Dependencies
*/
const { __ } = wp.i18n;
const { addFilter } = wp.hooks;