Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Till Krüss tillkruss

🏠
Working from home
View GitHub Profile
@tillkruss
tillkruss / worker.js
Created Jul 17, 2018
A Cloudflare worker that allow cookie/path based caching.
View worker.js
/**
* Time-to-live in seconds for cached requests.
*/
const cacheTtl = 300;
/**
* List of request paths to cache.
*/
const cachedPaths = [
@tillkruss
tillkruss / disable-search.php
Last active Jul 6, 2020
Disable WordPress front-end search functionality.
View disable-search.php
<?php
if ( ! is_admin() ) {
add_action( 'parse_query', function( $query ) {
if ( $query->is_search ) {
unset( $_GET['s'], $_POST['s'], $_REQUEST['s'] );
$query->set( 's', '' );
$query->is_search = false;
$query->set_404();
header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );
@tillkruss
tillkruss / paste-as-plain-text.php
Last active Jun 22, 2020
Force the WordPress editor to always paste as plain text.
View paste-as-plain-text.php
<?php
// always paste as plain text
foreach ( array( 'tiny_mce_before_init', 'teeny_mce_before_init') as $filter ) {
add_filter( $filter, function( $mceInit ) {
$mceInit[ 'paste_text_sticky' ] = true;
$mceInit[ 'paste_text_sticky_default' ] = true;
return $mceInit;
});
}
@tillkruss
tillkruss / README.md
Last active Jun 22, 2020
AWS Lambda function that optimizes images uploaded to a S3 bucket using Kraken.io's API.
View README.md

Lambda S3 Kraken

Trigger

  • Event type: ObjectCreated
  • Suffix: jpg

Environment variables

  • AWS_KEY
  • AWS_SECRET
  • KRAKEN_SECRET
@tillkruss
tillkruss / stream-s3-as-zip.php
Last active Apr 6, 2020
Stream files from S3 as ZIP file.
View stream-s3-as-zip.php
<?php
use Aws\S3\S3Client;
use ZipStream\ZipStream; // https://github.com/maennchen/ZipStream-PHP
use GuzzleHttp\Client as HttpClient;
protected function streamAsZip($files)
{
$s3 = S3Client::factory('...');
$zip = new ZipStream("foobar.zip");
@tillkruss
tillkruss / force-color-scheme.php
Last active Jan 21, 2020
Enforce a specific WordPress admin color scheme.
View force-color-scheme.php
<?php
add_action( 'admin_init', function() {
// remove the color scheme picker
remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
// force all users to use the "Ectoplasm" color scheme
add_filter( 'get_user_option_admin_color', function() {
return 'ectoplasm';
@tillkruss
tillkruss / AppServiceProvider.php
Last active Jul 27, 2019
ElasticSearch engine for Laravel Scout
View AppServiceProvider.php
<?php
resolve(EngineManager::class)->extend('elasticsearch', function ($app) {
return new ElasticsearchEngine(
ElasticBuilder::create()->setHosts(config('scout.elasticsearch.hosts'))->build()
);
});
@tillkruss
tillkruss / app\Http\Kernel.php
Last active Nov 9, 2018
Running Laravel 5 on Heroku behind CloudFlare
View app\Http\Kernel.php
<?php
class Kernel extends HttpKernel
{
protected $middleware = [
\App\Http\Middleware\TrustedProxies::class,
];
}
@tillkruss
tillkruss / AuthServiceProvider.php
Last active Jul 18, 2018
Case-insensitive PostgreSQL eloquent user provider for Laravel 5.
View AuthServiceProvider.php
<?php
namespace App\Providers;
use Auth;
use App\Support\EloquentUserProvider;
class AuthServiceProvider extends ServiceProvider
{
public function boot(GateContract $gate)
@tillkruss
tillkruss / filename-based-cache-busting.php
Last active Jan 23, 2018
Filename-based cache busting for WordPress.
View filename-based-cache-busting.php
<?php
// changes CSS and JavaScript URLs from `css/style.css?ver=1.3.3.7` to `css/style.1.3.3.7.css`
if ( ! is_admin() ) {
foreach ( array( 'style_loader_src', 'script_loader_src' ) as $filter ) {
add_filter( $filter, function( $url ) {
// abort if `$url` doesn't start with `WP_CONTENT_URL`