Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
Coding.

Russell Heimlich kingkool68

💻
Coding.
View GitHub Profile
@kingkool68
kingkool68 / loading-csv-data-for-google-charts.php
Last active Oct 20, 2021
James Taylor wanted to load CSV data and display it as a gauge data visualization in WordPress. See https://www.facebook.com/groups/advancedwp/posts/4673874846008026/
View loading-csv-data-for-google-charts.php
<?php
/**
* Plugin Name: River Height Gauge
* Description: Fetch the current river height value and display it as a Gauge visualization.
* Version: 0.0.1
* Author: Russell Heimlich
* Author URI: https://twitter.com/kingkool68
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
View loading-csv-data-for-google-charts.php
<?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
<?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
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 https://localwp.com/community/t/proxying-requests-to-wp-content-uploads-to-a-production-site/15701
View use-remote-media.php
<?php
// Put this in wp-config.php and replace https://example.com/ with the URL of the production site.
define( 'RH_USE_REMOTE_MEDIA_URL', 'https://example.com/' );
// 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
kingkool68 / index.js
Created Nov 14, 2017
AWS Lambda Function for Proxying Requests to S3
View index.js
/**
* 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 / 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
<?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 https://php.watch/versions/8.0/str_starts_with-str_ends_with
@kingkool68
kingkool68 / changelog.sh
Last active Jul 1, 2021
Bash script to generate a markdown change log of GitHub pull requests between tagged releases
View changelog.sh
#!/bin/bash
# Generate a Markdown change log of pull requests from commits between two tags
# Author: Russell Heimlich
# URL: https://gist.github.com/kingkool68/09a201a35c83e43af08fcbacee5c315a
# HOW TO USE
# Copy this script to a directory under Git version control
# Make the script executable i.e. chmod +x changelog.sh
# Run it! ./changelog.sh
# Check CHANGELOG.md to see your results
@kingkool68
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
<?php
/**
* Example useage:
*
* maybe_sideload_image( 'https://dummyimage.com/600x400/000/fff.png' ); // Downloads image to the media library and returns an attachment ID
* maybe_sideload_image( 'https://dummyimage.com/600x400/000/fff.png' ); // Returns an attachment ID as the image has already been downloaded and added to the media library
*/
/**
@kingkool68
kingkool68 / svg-functions.php
Created Aug 4, 2017
SVG helper functions for WordPress
View svg-functions.php
<?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 / rh-get-widget-data-for-all-sidebars.php
Last active Feb 15, 2021
WordPress function to get raw widget data for all of the widgets in a given sidebar
View rh-get-widget-data-for-all-sidebars.php
<?php
function rh_get_widget_data_for_all_sidebars() {
global $wp_registered_sidebars;
$output = array();
foreach ( $wp_registered_sidebars as $sidebar ) {
if ( empty( $sidebar['name'] ) ) {
continue;
}
$sidebar_name = $sidebar['name'];