Skip to content

Instantly share code, notes, and snippets.

View soderlind's full-sized avatar

Per Søderlind soderlind

View GitHub Profile
@soderlind
soderlind / mu-super-admin-all-sites-menu.php
Last active April 7, 2022 11:37
Fast REST Response for Super Admin All Sites Menu (https://github.com/soderlind/super-admin-all-sites-menu), save in mu-plugins
<?php
/**
* name: Fast REST Response for Super Admin All Sites Menu
*
* @package Soderlind\Multisite\MU
* @author Per Soderlind
* @copyright 2021 Per Soderlind
* @license GPL-2.0+
*
* @wordpress-plugin
<?php
add_action( 'muplugins_loaded',function() : void {
$my_rest_endpoint = '/wp-json/super-admin-all-sites-menu/v1/sites/';
$request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
/**
* Bail if not the correct request.
*/
@soderlind
soderlind / change-theme-root.php
Last active April 2, 2022 15:50
Change WordPress theme root to a folder in your plugin (src/assets)
<?php
/**
* Sample use:
* WordPres Customizer is dependant on functionality in the theme. Just in case
* the current theme doesn't support WordPress Customizer we'll use a theme
* that supports it.
*/
class NN {
private $theme_name = 'twentysixteen';
@soderlind
soderlind / webpack.config.js
Last active February 28, 2022 16:58
Add a banner to build/index.js, when running "wp-scripts build"
"use strict";
const defaultConfig = require("@wordpress/scripts/config/webpack.config");
const packageJSON = require("./package.json");
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
...defaultConfig,
module: {
...defaultConfig.module,
@soderlind
soderlind / run-wp-cron.sh
Last active February 23, 2022 20:16 — forked from bjornjohansen/run-wp-cron.sh
Run all due cron events for WordPress with WP-CLI. Works with both single sites and multisite networks.
#!/bin/bash
# Copyright © 2015 Bjørn Johansen
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
# Modified by Per Søderlind
WP_PATH="/path/to/wp"
MAIN_SITE="http://www.domain.tld" # --url="$MAIN_SITE" below, prevents the Undefined index: HTTP_HOST error.
@soderlind
soderlind / deploy-develop.yml
Last active December 22, 2021 08:59
GitHub Action: Deploy WordPress to Azure Container Registry
name: Development deploy
on:
repository_dispatch:
types: trigger-development-deploy
workflow_dispatch:
push:
branches:
@soderlind
soderlind / README.md
Last active December 15, 2021 17:06
VS Code: PHP Code Beautifier and Fixer (phpcbf) and Short Array Syntax Converter task runners.

PHP Code Beautifier and Fixer (phpcbf)

phpcbf is included when you install PHP_CodeSniffer. Tom McFarlin has written a nice guide on how to install PHP_CodeSniffer and the WordPress Coding Standards Rules for VSCode. If you follow this guide, phpcbf will be installed in the project root, in ./vendor/bin/phpcbf

PHP 5.4 Short Array Syntax Converter

You'll find the convert.php script at GitHub

@soderlind
soderlind / insert-link-to-pdf.php
Last active October 21, 2021 23:17
WordPress: In the editor, using "Insert Link", insert link to a PDF file in the media library
@soderlind
soderlind / example.php
Last active September 25, 2021 20:33
WordPress Multisite, gets all site transient keys in the database with a specific prefix.
<?php
foreach ( get_site_transient_keys_with_prefix( 'my_tranients_prefix_' ) as $transient ) {
delete_site_transient( $transient );
}
@soderlind
soderlind / get-ip.php
Last active July 12, 2021 09:14
PHP, Get IP behind trusted proxy
<?php
/**
* Inspired by https://raw.githubusercontent.com/zendframework/zend-http/master/src/PhpEnvironment/RemoteAddress.php
*/
//PHP 5.6
function get_ip( $trusted_proxies = [] ) {
if ( ! empty( $trusted_proxies ) && isset( $_SERVER['REMOTE_ADDR'] ) && ! in_array( $_SERVER['REMOTE_ADDR'], $trusted_proxies ) ) {
if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
} elseif ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {