Skip to content

Instantly share code, notes, and snippets.

View kingkool68's full-sized avatar
💻
Coding.

Russell Heimlich kingkool68

💻
Coding.
View GitHub Profile
@kingkool68
kingkool68 / center-iframe.css
Created November 27, 2016 22:02
Look, Patti Look!
iframe {
display: block;
margin-left: auto;
margin-right: auto;
max-width: 1000px;
}
@kingkool68
kingkool68 / filter.php
Last active September 21, 2017 21:57
Using `wp_debug_backtrace_summary()` to only filter something when called from a particular function or method
/*
Here we're filtering the site_url() and get_site_url() functions which are called dozens and dozens of times during a request.
In this case we only want to modify $url if the filter was called from a particular PHP class (rtCamp\WP\Nginx\Helper)
*/
add_filter( 'site_url', function( $url = '' ) {
$backtrace = wp_debug_backtrace_summary();
if ( stripos( $backtrace, 'rtCamp\WP\Nginx\Helper' ) ) {
$url = str_replace( 'https://', 'http://', $url );
}
return $url;
@kingkool68
kingkool68 / boagworld-lazy-load-images.php
Created March 21, 2017 15:58
Lazy Load Images WordPress Plugin for Boagworld
<?php
/*
Plugin Name: Bogworld's Special Neat-o Plugin
Description: See https://twitter.com/boagworld/status/844200551017562112
Author: kingkool68
Version: 0.0.1
Author URI: https://twitter.com/kingkool68
*/
/*
@kingkool68
kingkool68 / instagram-fetch.php
Last active April 8, 2023 22:43
A brief example of working with an external API in WordPress
<?php
// In this brief example we'll scrape an Instagram post and save it as a WordPress post
// Go to a URL and get the contents back
// See https://developer.wordpress.org/reference/functions/wp_remote_get/
$instagram_request = wp_remote_get( 'https://www.instagram.com/p/BSBvNVIF8tI/' );
// If it's succesful, the payload of the request will be in $instagram_request['body']
$instagram_html = $instagram_request['body'];
@kingkool68
kingkool68 / filter-db-queries.php
Created June 27, 2017 15:46
Prevent MySQL from caching database queries when debugging database performance issues.
<?php
// Force MySQL to not cache queries. Helpful for database performance testing.
// DO NOT LEAVE THIS ON IN PRODUCTION!
add_filter( 'query', function( $query ) {
$query = preg_replace( '/SELECT /', 'SELECT SQL_NO_CACHE ', $query, 1 );
return $query;
});
@kingkool68
kingkool68 / svg-functions.php
Created August 4, 2017 03:22
SVG helper functions for WordPress
<?php
// Throw this in your theme and include it in your functions.php file
/**
* Helper function for fetching SVG icons
*
* @param string $icon Name of the SVG file in the icons directory
* @return string Inline SVG markup
*/
function wp_svg_icon( $icon = '' ) {
@kingkool68
kingkool68 / strip-google-docs-formatting.php
Created August 17, 2017 18:12
Fixing Google Docs formatting
<?php
add_filter( 'wp_kses_allowed_html', function( $allowed_tags, $context ) {
if ( isset( $allowed_tags['span'] ) ) {
unset( $allowed_tags['span'] );
}
foreach ( $allowed_tags as $tag => $attrs ) {
$allowed_tags[ $tag ]['style'] = false;
$allowed_tags[ $tag ]['dir'] = false;
}
return $allowed_tags;
<?php
// Add `SQL_NO_CACHE` to every SELECT statement
// Helpful for debugging query performance
add_filter( 'query', function( $query ) {
$query = preg_replace( '/SELECT(\s)/i', 'SELECT SQL_NO_CACHE$1', $query, 1 );
return $query;
});
@kingkool68
kingkool68 / index.js
Created November 14, 2017 18:33
AWS Lambda Function for Proxying Requests to S3
/**
* This is a simple AWS Lambda function that will look for a given file on S3 and return it
* passing along all of the headers of the S3 file. To make this available via a URL use
* API Gateway with an AWS Lambda Proxy Integration.
*
* Set the S3_REGION and S3_BUCKET global parameters in AWS Lambda
* Make sure the Lambda function is passed an object with `{ pathParameters : { proxy: 'path/to/file.jpg' } }` set
*/
var AWS = require('aws-sdk');
@kingkool68
kingkool68 / wp_dump.php
Last active September 5, 2018 03:09
A better dumping function that preservers whitespace making things easier to read.
<?php
if ( ! function_exists( 'wp_dump' ) ) :
/**
* Dump variables preserving whitespace so they are easier to read.
*/
function wp_dump() {
$is_xdebug = false;
if ( function_exists( 'xdebug_dump_superglobals' ) ) {
$is_xdebug = true;