Skip to content

Instantly share code, notes, and snippets.

View brianlayman's full-sized avatar

Brian Layman brianlayman

View GitHub Profile
// Paste this at the end of functions.php
// or add as a separate file in mu-plugins including a leading opening < ? php tag in front.
// Activate WordPress Maintenance Mode
function wp_maintenance_mode() {
if ( ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) && !( is_admin() || stripos( wp_login_url(), $_SERVER['SCRIPT_NAME'] ) ) ) {
$message = '<!doctype html>
<title>Site Maintenance</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet">
<style>
@brianlayman
brianlayman / Force Hide Metaboxes on CPT Editor Page
Last active October 15, 2022 05:05
How to force hide metaboxes on specific post type editor pages: RevSlider WPSEO by Yoast, whatever.
The do_meta_boxes filter is run 3 times for every editor page load. Once for each context.
So you only need to remove the metabox if you are in an editor you care about.
There are 3 parameters sent in the do_meta_boxes filter. So add the action this way:
`add_action( 'do_meta_boxes', 'remove_unwanted_metaboxes', 10, 3 );`
And then do something like this:
```
@brianlayman
brianlayman / gist:0f6c79866f17b0e64cf4d35a42787d48
Created June 6, 2019 22:15
How to ensure a child theme's style.css is always refreshed when a new style sheet is deployed.
// This gets the modified time for the current style sheet. It caches the check for 30 seconds.
// That's the longest you'd have to wait for a new style sheet to be forced on all visitors.
// This micro-caching helps limit drive access when the site is busy.
// You could change it to 5 seconds on a less busy site or any value you like
function enqueue_versioned_child_style() {
// StyleSheet date is only checked once every 30 seconds
if ( false === ( $stylesheetmtime = get_transient( 'stylesheetmtime' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$stylesheetmtime = filemtime( get_stylesheet_directory() . '/style.css');
set_transient( 'stylesheetmtime', $stylesheetmtime, 30 );
@brianlayman
brianlayman / gist:9ba6ecab9e180dfa1da7d6d98db452c5
Created June 6, 2019 19:08
A quick WordPress "Tools" menu option to Save the Values and Do the Thing.
class THING_DOER
{
/**
* Holds the values to be used in the fields callbacks
*/
private $option_name;
private $page_name;
private $options;
private $action;
@brianlayman
brianlayman / amazon_regex.md
Created October 19, 2018 22:12 — forked from GreenFootballs/amazon_regex.md
A PHP regular expression to match Amazon links and extract the ASIN identifier
~
    (?:(smile\.|www\.))?    # optionally starts with smile. or www.
    ama?zo?n\.              # also allow shortened amzn.com URLs
    (?:
        com                 # match all Amazon domains
        |
        ca
        |
        co\.uk
@brianlayman
brianlayman / gist:b43e82e61b2fc67829cab6565684d839
Created August 19, 2016 14:57
trailingampit ensures a URL ends with a /amp/
<?php
/**
* Ensures the passed text, presumably an url, ends with '/amp/'
*/
if ( !function_exists( 'trailingampit' ) ) {
function trailingampit( $string ) {
return untrailingampit( $string ) . 'amp/';
}
}
<?php
/*
Plugin Name: DenyHosts
Plugin URI: http://pross.org.uk
Description: Block bad login attempts.
Version: 1.0
Author: Pross
*/
class DenyHosts {
@brianlayman
brianlayman / dl-file.php
Created April 5, 2012 19:26 — forked from hakre/dl-file.php
Wordpress login to download uploaded files
<?php
/*
* dl-file.php
*
* Protect uploaded files with login.
*
* @link http://wordpress.stackexchange.com/questions/37144/protect-wordpress-uploads-if-user-is-not-logged-in
*
* @author hakre <http://hakre.wordpress.com/>
* @license GPL-3.0+
@brianlayman
brianlayman / dl-file.php
Created April 5, 2012 19:26 — forked from hakre/dl-file.php
Wordpress login to download uploaded files
<?php
/*
* dl-file.php
*
* Protect uploaded files with login.
*
* @link http://wordpress.stackexchange.com/questions/37144/protect-wordpress-uploads-if-user-is-not-logged-in
*
* @author hakre <http://hakre.wordpress.com/>
* @license GPL-3.0+
@brianlayman
brianlayman / dl-file.php
Created April 5, 2012 19:23 — forked from hakre/dl-file.php
Wordpress login to download uploaded files
<?php
/*
* dl-file.php
*
* Protect uploaded files with login.
*
* @link http://wordpress.stackexchange.com/questions/37144/protect-wordpress-uploads-if-user-is-not-logged-in
*
* @author hakre <http://hakre.wordpress.com/>
* @license GPL-3.0+