Skip to content

Instantly share code, notes, and snippets.


Russell Heimlich kingkool68

View GitHub Profile
kingkool68 / loading-csv-data-for-google-charts.php
Last active Oct 26, 2021
James Taylor wanted to load CSV data and display it as a gauge data visualization in WordPress. See
View loading-csv-data-for-google-charts.php
* Plugin Name: River Height Levels
* Plugin URI:
* Description: Fetch the current river height values and display it as different data visualization.
* Version: 0.0.2
* Author: Russell Heimlich
* Author URI:
* License: GPL v2 or later
* License URI:
View loading-csv-data-for-google-charts.php
* Load an external CSV file and store it as a transient
function get_the_csv_data() {
$transient_key = 'the-csv-data';
$data = get_transient( $transient_key );
// If the data already exists then return it. We're done here!
if ( false !== $data ) {
View archive-post-type.php
// Expects a page with the slug (aka post_name) set to `post-type` (the same post-type key as the custom post type)
$post = RH_Post_Type_Archives::get_post_type_archive_post();
setup_postdata( $post );
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
kingkool68 / virtual-page.php
Created Sep 21, 2021
Make WordPress respond to a request for a URL that doesn't actually exist
View virtual-page.php
* Make WordPress respond to a request for a URL that doesn't actually exist
if ( ! function_exists( 'str_starts_with' ) ) {
* Polyfill for PHP 8's str_starts_with
* @link
kingkool68 / maybe-sideload.php
Created Nov 21, 2020
Two WordPress functions for maybe side loading URLs to the media library. Useful for content migrations that need to be run multiple times without producing duplicate downloads.
View maybe-sideload.php
* Example useage:
* maybe_sideload_image( '' ); // Downloads image to the media library and returns an attachment ID
* maybe_sideload_image( '' ); // Returns an attachment ID as the image has already been downloaded and added to the media library
View sso-logout.php
<!doctype html>
<html class="no-js" lang="en-US">
<meta charset="UTF-8" />
<title>English Learning Programs SSO Logout</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name='robots' content='noindex,nofollow' />
<link rel='stylesheet' id='google-fonts-css' href=';1&#038;family=Work+Sans:wght@100;200;300;400;500;600;700;800;900&#038;display=swap' type='text/css' media='all' />

Table of Contents WordPress Plugin

This WordPress plugin creates a table of contents based on the heading structure. It automatically adds anchor links for every heading.


Use the f1_toc() function to render the table of contents with markup. An optional paramater called limit can be used to only show headings up to a certain level. Example:

// Only show <H1> and <H2> headings in the table of contents
kingkool68 / csv-download.php
Last active Jul 29, 2020
How to dynamically generate a CSV with PHP
View csv-download.php
$filename = 'test.csv';
$now = gmdate( 'D, d M Y H:i:s' );
header( 'Expires: Tue, 03 Jul 2001 06:00:00 GMT' );
header( 'Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate' );
header( "Last-Modified: {$now} GMT" );
// force download
header( 'Content-Type: application/force-download' );
header( 'Content-Type: application/octet-stream' );
kingkool68 / use-remote-media.php
Last active Oct 7, 2021
Check if a local file exists in the WordPress media library and if it doesn't exist, replace the URL with a different URL. Helpful when working with a local site that doesn't have all of the media downloaded as the production site. See
View use-remote-media.php
// Put this in wp-config.php and replace with the URL of the production site.
define( 'RH_USE_REMOTE_MEDIA_URL', '' );
// Put the rest of this in functions.php or a custom plugin or somewhere else.
if ( defined( 'RH_USE_REMOTE_MEDIA_URL' ) && ! empty( RH_USE_REMOTE_MEDIA_URL ) ) {
add_filter( 'wp_get_attachment_image_src', 'filter_wp_get_attachment_image_src' );
add_filter( 'wp_calculate_image_srcset', 'filter_wp_calculate_image_srcset' );
add_filter( 'wp_get_attachment_url', 'filter_wp_get_attachment_url' );
kingkool68 / linear-interpolation-of-array.php
Created Apr 4, 2020
Take an array of values and interpolate missing numbers
View linear-interpolation-of-array.php
function interpolate_row( $row = array() ) {
if ( ! is_array( $row ) || empty( $row ) ) {
return $row;
$items = array();
$item = array(
'start' => null,
'end' => null,