Skip to content

Instantly share code, notes, and snippets.

@broskees
broskees / CleanWordBreaks.php
Last active November 1, 2023 21:37
PHP function to break words on special characters
<?php
if (! function_exists('cleanWordBreaks')) {
function cleanWordBreaks(string $text): string
{
$cleanText = $text;
$cleanup = [];
$makeReplacement = function ($regex, $replacement = '') use (&$cleanup, &$cleanText) {
@broskees
broskees / acf-required.php
Last active August 24, 2023 14:52
Force Site to use ACF to Function (MU-Plugin)
<?php
! defined('ABSPATH') && exit;
add_action('plugins_loaded', function () {
if (// is network install and main site is not set up
(is_multisite() && ! get_blog_option(1, 'siteurl'))
// is single site install and site is not set up
|| (! is_multisite() && ! is_blog_installed())
// ACF is already installed
@broskees
broskees / anonymous-singleton.php
Last active July 20, 2023 16:09
PHP — Anonymous Singleton Class
<?php
$singleton = get_class(
new class
{
protected static $instance;
public static function getInstance()
{
if (! isset(self::$instance)) {
@broskees
broskees / get-private.php
Last active June 13, 2023 15:58
A function for getting a private property from an object — for use in Roots Acorn & Laravel (SHOULD BE USED FOR DEBUGGING ONLY)
<?php
/**
* Get a private property from an object.
*
* @param object $that
* @param string $var
* @return mixed
*/
function &getPrivateProp(&$that, $var): mixed
@broskees
broskees / remove-wp-logo.php
Created May 18, 2023 17:40
Remove the WordPress logo from the Admin Bar
<?php
add_action('admin_bar_menu', function ($wp_admin_bar) {
$wp_admin_bar->remove_node('wp-logo');
}, 999);
@broskees
broskees / server-side-error-detection.php
Created May 18, 2023 17:37
Server side error detection WordPress
<?php
/*
* Some plugins set 5xx status when code runs through expected flows.
* This is to track this since it does not trigger any PHP error logs,
* so that you have some idea why a site fails with 5xx.
*/
add_action ('status_header', function ($status_header, $code, $description, $protocol) {
if ( $code >= 500 ) {
$backtrace = Logger::debugBacktrace();
$this->logger->error( "Server-side error detected: $status_header. Backtrace: $backtrace" );
@broskees
broskees / hide-mu-plugins.php
Created May 18, 2023 17:32
Hide Must Use Plugins WordPress
<?php
add_filter( 'show_advanced_plugins', function ($default, $type) {
return $type == 'mustuse' ? false : $default;
}, 10, 2 );
@broskees
broskees / PostHasTerm.php
Created January 11, 2023 17:04
A custom ACF Location to be able to check if a post or custom post type (CPT) has a specific taxonomy term (supports categories). You can remove the namespace bit if you like.
<?php
namespace App;
class PostHasTerm extends \ACF_Location
{
public function initialize()
{
$this->name = 'post_has_term';
$this->label = __('Post Has Term');
@broskees
broskees / wp_debug_var.php
Last active October 13, 2022 19:34
A nice wp_debug_var() as a mu-plugin to include in your projects
<?php
/*
Plugin Name: WP Debug Function
Description: A Must-Use plugin that makes the wp_debug_var available to your WordPress Installation
Plugin URI: https://gist.github.com/broskees/18c6b10a491d2946fbbfed96a8dd6dc4
Version: 1.0.0
Author: Joseph Roberts
Author URI: https://github.com/broskees
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
@broskees
broskees / don't-break-my-homepage.php
Last active September 10, 2022 17:43
A WordPress mu-plugin to disable visual editing of the homepage and only allow text editing
<?php
/**
* Don't break my homepage mu-plugin
*
*/
add_filter('user_can_richedit', function ($can) {
global $post;
if ($post_ID === (int) get_option('page_on_front')) {
return false;