Skip to content

Instantly share code, notes, and snippets.

View rmpel's full-sized avatar

Remon Pel rmpel

View GitHub Profile
@rmpel
rmpel / build-changelog.sh
Created April 10, 2025 09:30
Build a changelog from the previous tag to head in a composer based website
#!/bin/bash
# A script to generate a changelog by comparing composer packages and project commits.
#
# Disclaimer:
# - Using this script is at your own risk. This script only uses READ operations, but I still
# do not take any responsibility.
#
# License:
# - BSD 2-Clause — Free to use with attribution. No warranty provided.
#
@rmpel
rmpel / Combat CORS issues in WordPress Multisite.php
Created March 17, 2025 07:25
Combat CORS issues in WordPress Multisite
<?php
// Make local URLs domainless, for all styles and scripts, to combat CORS issues.
// Ideally, we fix this in .htaccess using conditional headers, but in case of nginx, that's not available.
add_filter( 'script_loader_src', 'make_local_urls_domainless', 10 );
add_filter( 'style_loader_src', 'make_local_urls_domainless', 10 );
function make_local_urls_domainless( $url ) {
global $wpdb;
$all_domains = $wpdb->get_col( "SELECT domain FROM {$wpdb->blogs} ORDER BY blog_id DESC" );
@rmpel
rmpel / Login a user during a rest call - example-code.php
Last active March 27, 2025 11:49
WORDPRESS REST API - Login a user during a rest call - incompatible with caching
<?php
/**
* Maybe login by cookie.
* This function will log in a user by their authentication cookie, if they have one.
* This will use a full authentication, so it is safe from cookie-faking.
* Cookie hijacking is still a possibility, however, that's a WordPress issue, not a plugin issue.
* (To combat this; prefix the salts in wp-config.php with $_SERVER['REMOTE_ADDR']. This is not watertight,
* but always better than not)
*
* Call this function in your `permission_callback`, or in case of `__return_true` in your `callback` for getting items.
@rmpel
rmpel / LocalWP crash on dns_get_record fix
Last active February 21, 2025 10:29
Is your LocalWP crashing (service unavailable) when using dns_get_record? Try this;
# changes-to--php.ini.hbs
...
disable_functions = dns_get_record
...
auto_prepend_file = /path/to/the/dns_get_record_polyfill.php
...
@rmpel
rmpel / zsh-auto-localwp.sh
Last active April 20, 2025 13:58
zsh automatic LocalWP context switcher when navigating folders of projects on MacOS
# this is a zsh script to automatically switch to the localwp shell when you enter a
# directory that contains a LocalWP website installation
# it is designed to trigger on any directory deeper or exactly the app/ folder of a localwp site.
# this code is FAR FROM perfect, but it works! Feel free to suggest/improve/etc
#
# this file is to be sourced in your .zshrc .
#
# Suggested install method:
# cd ~ ; mkdir -p .zsh ; git clone https://gist.github.com/f5c220b5e49baf0df4f5bbf5b7e76fdf.git .zsh/zsh-auto-localwp
# echo "source ~/.zsh/zsh-auto-localwp/zsh-auto-localwp.sh" >> ~/.zshrc
@rmpel
rmpel / i18n.sh
Created November 22, 2024 08:37
Universal po mo php file script for WordPress plugins
#!/usr/bin/env bash
cd "$(dirname $0)/.."
[ -z "$1" ] && echo "Usage: $0 command" && echo "commands:" && echo "make-pot : re-generate POT file from source" && echo "update-po : update PO files from POT file" && echo "make-mo : generate MO and l10n.php files from PO files" && exit 1
# find textdomain from ./*php
for file in $(find . -name '*.php'); do
if grep -q "Text Domain:" "$file"; then
TEXTDOMAIN=$(grep -o "Text Domain:\s*\([^'\"]*\)" "$file" | sed "s/Text Domain:\s*\([^'\"]*\)/\1/")
@rmpel
rmpel / switch-php.sh
Last active November 12, 2024 14:18
php version switch for pipeline
#!/usr/bin/env bash
[ -z "$1" ] && echo example usage: $0 8.1 >&2 && exit 1
[ ! -f /usr/bin/php${1} ] && echo "No support for this version" >&2 && ls -la /usr/bin/php* >&2 && exit 2
cd ~/.local/bin
rm php
ln -s /usr/bin/php${1} php
hash -r
@rmpel
rmpel / acf-deduplication.php
Last active August 9, 2024 10:16
ACF FieldGroups Deduplication
<?php
/**
* Sometimes, shit happens and you end up with duplication of ACF FieldGroups.
* This mu-plugin will fix that.
* It is also compatible with ACF-to-PHP.
*/
add_action( 'admin_init', 'deduplicate_acf_data' );
@rmpel
rmpel / mu-plugin-dnt-video.php
Created August 9, 2024 06:11
Privacy friendly video embeds in WordPress
<?php
// This will make WordPress ALWAYS use privacy friendly embeds, implemented here are YouTube and Vimeo.
// Other services can be implemented by using this code as inspiration.
add_filter( 'oembed_dataparse', 'myproject_patch_oembed_urls', 11 );
function myproject_patch_oembed_urls( $html ) {
// find vimeo.com urls and add ?dnt=1 to them.
$urls = wp_extract_urls( $html );
foreach ( $urls as $old_url ) {
if ( false !== strpos( $old_url, 'vimeo.com' ) ) {
$new_url = add_query_arg( 'dnt', 1, $old_url );
@rmpel
rmpel / fix-author-dropdown.php
Last active June 3, 2024 14:26
Fix WordPress Author dropdown in multi-role set-up
<?php
/**
* If your website has custom roles and capabilities, and post-types with custom edit_posts capabilities,
* the dropdown on the _overview_ page, _quick edit_ is CORRECT.
* however, the dropdown on the _create new post_ or _edit existing post_ pages is NOT.
* This code fixes that.
*
* @package FixesByRemonPel
* @subpackage FixesByRemonPel/RestUserQuery
*/