Skip to content

Instantly share code, notes, and snippets.

View markhowellsmead's full-sized avatar

Mark Howells-Mead markhowellsmead

View GitHub Profile
@markhowellsmead
markhowellsmead / event_query.php
Last active March 18, 2025 20:38
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 / download_jimdo_images.js
Last active January 30, 2025 13:14
Download full-size images from a Jimdo page containing galleries
/**
* Run this in the browser's developer console.
* Provided with no guarantees! Worked perfectly when published.
*
* mark-at-sayhello.ch / 30.1.2025
*/
(()=> {
let galleryCounters = {}; // Stores counters for each gallery legend
@markhowellsmead
markhowellsmead / .htaccess
Last active December 27, 2024 13:59
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 / block-bindings.php
Created November 14, 2024 11:40
Example usage of block bindings
<?php
register_block_bindings_source('my-custom-prefix/current-year', [
'label' => __('Current year', 'my-custom-prefix'),
'get_value_callback' => 'my_custom_prefix_current_year',
]);
function my_custom_prefix_current_year()
{
@markhowellsmead
markhowellsmead / email_not_specific_domain.txt
Last active October 30, 2024 10:39
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 / 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 / Block.php
Created June 5, 2024 13:11
Add a do_action using the render_block filter
<?php
namespace PT\MustUse\Blocks\CoreEmbed;
class Block
{
public function run()
{
add_filter('render_block_core/embed', [$this, 'render'], 10, 2);
}
@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 / 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'