Skip to content

Instantly share code, notes, and snippets.

View roytanck's full-sized avatar

Roy Tanck roytanck

View GitHub Profile
@roytanck
roytanck / generate_id.php
Last active October 14, 2021 12:48
Create a short, fairly unique, urlsafe id string for a given input. This uses (almost) all allowed urlsafe characters to maximize the possible number of values.
/**
* Create a short, fairly unique, urlsafe hash for the input string.
*/
function generate_id( $input, $length = 8 ){
// Create a raw binary sha256 hash and base64 encode it.
$hash_base64 = base64_encode( hash( 'sha256', $input, true ) );
// Replace non-urlsafe chars to make the string urlsafe.
$hash_urlsafe = strtr( $hash_base64, '+/', '-_' );
// Trim base64 padding characters from the end.
$hash_urlsafe = rtrim( $hash_urlsafe, '=' );
@roytanck
roytanck / rt_user_has_published_content.php
Created April 14, 2020 12:48
Find out if a user has published content on a multisite WordPress install.
/**
* Find out if a user has published content anywhere on the (multi-)site
*/
function rt_user_has_published_content( $user_id ) {
// Arguments for get_posts, requesting 1 post of any type.
$args = array(
'post_type' => 'any',
'posts_per_page' => 1,
'author' => $user_id,
'fields' => 'ids',
@roytanck
roytanck / autoautoptimize.php
Last active January 25, 2023 06:45
Auto-configure Autoptimize across a WordPress network
<?php
/**
* Plugin Name: RT Autoautoptimize
* Plugin URI: http://www.this-play.nl
* Description: Automatically configures default settings for the Autoptimize plugin across a WordPress network
* Version: 0.9
* Author: Roy Tanck
* Author URI: http://www.this-play.nl
* License: GPL2
*/
@roytanck
roytanck / rt-add-link-target.php
Created April 1, 2015 14:08
Open external links inside WordPress comments in a new tab/window
@roytanck
roytanck / rt-force-cache-refresh.php
Last active August 29, 2015 14:02
WordPress plugin to force cleaning of expired WP Super Cache files
<?php
/**
* Plugin Name: RT Force Cache Refresh
* Plugin URI: http://www.this-play.nl
* Description: Periodically deletes expired WP Super Cache files using WP-Cron
* Version: 0.8
* Author: Roy Tanck
* Author URI: http://www.this-play.nl
* License: GPL2
*/
@roytanck
roytanck / image_get_intermediate_size_possiblefix
Created October 29, 2013 14:57
Possible approach to handling aspect ration differences when finding the best match for a requested image resolution in WordPress's image_get_intermediate_size function in media.php.
/**
* Retrieve the image's intermediate size (resized) path, width, and height.
*
* The $size parameter can be an array with the width and height respectively.
* If the size matches the 'sizes' metadata array for width and height, then it
* will be used. If there is no direct match, then the nearest image size larger
* than the specified size will be used. If nothing is found, then the function
* will break out and return false.
*
* The metadata 'sizes' is used for compatible sizes that can be used for the
@roytanck
roytanck / image_get_intermediate_size
Created October 29, 2013 08:10
Modified version of image_get_intermediate_size (WordPress, media.php) which checks for an exact match (width and height) before attempting to find a matching width OR height. This fixes the function's behavior in some cases, but a better solution is to add proper aspect handling to the function.
/**
* Retrieve the image's intermediate size (resized) path, width, and height.
*
* The $size parameter can be an array with the width and height respectively.
* If the size matches the 'sizes' metadata array for width and height, then it
* will be used. If there is no direct match, then the nearest image size larger
* than the specified size will be used. If nothing is found, then the function
* will break out and return false.
*
* The metadata 'sizes' is used for compatible sizes that can be used for the
@roytanck
roytanck / wceu-test.php
Created October 29, 2013 08:06
Simple test plugin to trigger the WordPress issue described in Trac ticket #17626 (http://core.trac.wordpress.org/ticket/17626). The image ID is hardcoded, so you'll have to edit the plugin so it matches your upload.
<?php
/*
Plugin Name: WCEU test plugin
Plugin URI: http://core.trac.wordpress.org/ticket/17626
Description: Test plugin to trigger bug #17626
Version: 1.0
Author: Roy Tanck
Author URI: http://www.this-play.nl
Licence: GPL
*/
@roytanck
roytanck / remove_gforms_caps.php
Last active December 14, 2015 15:29
Remove gravity forms capabilities for non-admin users, through Gravity Form's 'gform_cap_full_access' hook.
if( ! function_exists( 'rt_filter_gform_caps' ) ) {
function rt_filter_gform_caps( $caps ){
if( !current_user_can( 'manage_options' ) ){
if( current_user_can( 'gform_full_access' ) ){
$user = wp_get_current_user();
$user->remove_cap('gform_full_access');
}
return '';
} else {
return $caps;
@roytanck
roytanck / walker-page-parent-only.php
Created October 29, 2012 09:31 — forked from markoheijnen/walker-page-parent-only.php
This walker for WordPress gives you the ability to only show the items that are selected
<?php
/**
* Create HTML list of pages.
*
* @package WordPress
* @since 2.1.0
* @uses Walker
*/
class Walker_Page_Parent_Only extends Walker {