Skip to content

Instantly share code, notes, and snippets.

View rmpel's full-sized avatar

Remon Pel rmpel

View GitHub Profile
@rmpel
rmpel / ga4.php
Last active November 17, 2021 14:06
GA4 Tracker mu-plugin
<?php // wp-content/mu-plugins/ga4.php - GA4 tracker boilerplate.
define('GA_NOT_LIVE', true); // remove for go
define('GA4_MEASUREMENT_ID', 'G-XXXXXXXXXX'); // required
define('UA_PROPERTY_ID', 'UA-XXXXXXX-YY'); // optional
define('GA_ANONYMIZE_IP', true); // optional
define('GA_TRACK_ADMINS', false); // optional, defaults to true
define('GA_TRACK_EDITORS', false); // optional, defaults to true
// we need GA as first thing in the body
@rmpel
rmpel / mu-plugin-migrate-webp-express-htaccess.php
Created October 16, 2020 11:09
WebP .htaccess files in wrong position causes problems with uploads-redirects
<?php
function migrate_webp_express_htaccess_files() {
$extra = [];
$uploads = wp_upload_dir();
$uploads = $uploads['basedir'];
$themes = WP_CONTENT_DIR .'/themes';
$plugins = WP_CONTENT_DIR .'/plugins';
@rmpel
rmpel / database-backup.sh
Last active September 23, 2020 11:39
Backup a WordPress database to a GIT repository to track changes over time
#!/usr/bin/env bash
###
# This script creates a backup using the WP-CLI tool and then stores it in a GIT repository.
# As GIT only saves the changes, you have a nice history of stuff that happened on your website.
# This is mostly an investigation tool and I have yet to determine the usefulness :)
#
# p.s., this does not work if git or wp-cli is installed; ergo; does NOT work with Local by Flywheel (unless you ssh into the docker and `sudo apt-get install git`.
##
@rmpel
rmpel / readme.md
Last active August 25, 2020 09:08
Migrate website out of multisite

To migrate a website out of a multisite (Yes, this is WordPress!!!!)

  1. determine site ID (for example: 5)
  2. create a spot where the new website will live (for example: /www/mywebsite.nl/public)
  3. install a new WordPress
cd /www/mywebsite.nl/public ; wp core download
  1. copy theme(s) and plugins
@rmpel
rmpel / _fuck-small-screens.scss
Last active February 27, 2020 10:28
Trouble making your website responsive below 375px?? fuck those small screens!
/** SCSS */
/** Fuck those small screens! */
@for $i from 0 through 16 {
@media all and (max-width: ( 375px - ($i*10px) )) {
body {
zoom: ( 375px - ($i*10px) ) / 385px;
}
}
}
@rmpel
rmpel / seo-sitemap-with-wpml-content-url-fix.php
Last active February 12, 2020 07:24
Fix the output of the XML sitemap so they show the correct localised URLs. This is an add-on for when you change the URLs using other techniques, see README.
<?php
// supports Yoast SEO and Yoast SEO Premium
add_filter('wpseo_sitemap_entry', 'wpseo_sitemap_entry', 10, 3);
// Supports SEO by Rank Math
add_filter('rank_math/sitemap/entry', 'wpseo_sitemap_entry', 10, 3);
function wpseo_sitemap_entry ( $url, $type, $object ) {
global $sitepress;
@rmpel
rmpel / translate-custom-post-type-archive-slug.php
Last active December 23, 2024 13:11
Translate post-type-archive-slug when different from post-type-slug. WPML allows translating custom post-type slug, but not the custom post-type-archive slug
<?php
/**
* Translate post-type-archive-slug when different from post-type-slug.
*
* You can have your archive slug set to, for example /books and the singles on /book/title by setting
* $args['rewrite'] => [ 'slug' => 'book', ... ];
* $args['has_archive'] => 'books';
* when registering your post_type
*
@rmpel
rmpel / allow-username-change.php
Last active March 17, 2023 07:10
Allo WordPress administrators to update username
<?php
/**
* Hook in the 'errors' hook as this is just in time and allows us to return an error in case of conflict.
*
* @package rmpel/WordPress
* @subpackage rmpel/WordPress/AllowUsernameChange
*/
add_action(
'user_profile_update_errors',
@rmpel
rmpel / translated-page-for-posts-post-url-base.php
Last active February 12, 2020 07:25
Translated POSTS urls in WPML WordPress website with a "Page for Posts" as POST-URL-base
<?php
/**
* Notes on the following section
*
* Correct set-up is:
*
* Set your page-for-posts to a certain WordPress Posts, for example /nl/nieuws/
* Set your permalink structure to the same; /nieuws/%postname%/
*
@rmpel
rmpel / redirect-to-browser-language-wp-wpml.php
Last active December 18, 2019 13:52
Redirect to browser language in WordPress + WPML when the default language is in a subfolder (as the rest) and there is no real homepage.
<?php
// redirect to language if no language
add_action( 'after_setup_theme', function () {
if ( is_admin() ) {
// we are on admin; do nothing
return;
}
if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {