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 / 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;
@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 / 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 / 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 / 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 / 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 / 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 / example.php
Created November 22, 2016 02:39
Grouping the results of a WordPress loop by month and year
<?php
$args = array(...);
$my_posts = new WP_Query( $args );
$the_year = '';
$the_month = '';
if ( $my_posts->have_posts() ) :
while ( $my_posts->have_posts() ) :
$my_posts->the_post();
// Get the year of the post in YYYY format
$post_year = get_the_time( 'Y' );
<?php
include 'simple_html_dom.php';
$html = file_get_html('http://www.asia.si.edu/exhibitions/past.asp');
$output = array();
foreach( $html->find( '#filterarea li' ) as $elem ) {
$h1 = $elem->find( 'h1', 0 );
if ( $h1 ) {
continue;
}
@kingkool68
kingkool68 / tally-email-subscribers-via-slack
Created September 21, 2016 21:28
PHP Script to Parse Slack Messages and Tally Data from Bots
<?php
/**
* This script will parse our Slack history and pull email subscriber counts that were posted by our bots.
* Place in the root of a WordPress install, add a Slack token for API access, and visit the URL
*
* A CSV file of the results will be downloaded.
* For further analys import into a database (See Below)
*
*/