Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
Coding.

Russell Heimlich kingkool68

💻
Coding.
View GitHub Profile
@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
*/
/**
View sso-logout.php
<!doctype html>
<html class="no-js" lang="en-US">
<head>
<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='https://fonts.googleapis.com/css2?family=Lato:ital@0;1&#038;family=Work+Sans:wght@100;200;300;400;500;600;700;800;900&#038;display=swap' type='text/css' media='all' />
View README.md

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.

Usage

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
kingkool68 / csv-download.php
Last active Jul 29, 2020
How to dynamically generate a CSV with PHP
View csv-download.php
<?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
kingkool68 / use-remote-media.php
Last active Sep 16, 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 / linear-interpolation-of-array.php
Created Apr 4, 2020
Take an array of values and interpolate missing numbers
View linear-interpolation-of-array.php
<?php
function interpolate_row( $row = array() ) {
if ( ! is_array( $row ) || empty( $row ) ) {
return $row;
}
$items = array();
$item = array(
'start' => null,
'end' => null,
@kingkool68
kingkool68 / class-mailchimp.php
Last active Feb 13, 2020
A MailChimp API wrapper for WordPress
View class-mailchimp.php
<?php
/**
* Let's nail down some concepts
* - Each site has it's own list of contacts, all subscribers belong to a list
* - Contacts can be part of groups and we can send a campaign to a group of subscribers
* - Groups can be categorized (a requirement of MailChimp)
*/
namespace Pedestal\Objects;
View 1st-map.html
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.21.0/mapbox-gl.js'></script> <section class="inter-map"> <div class="inter-map__inner"> <style> <!-- @font-face {
font-family: DINRoundOT;
src: url('https://a.americares.org/mapbox/fonts/FontFont - DINRoundOT.otf');
}
@font-face {
font-family: BlockBQBol;
src: url('https://a.americares.org/mapbox/fonts/BlockBQBol.otf');
}
View tags.php
<?php
class CTF_Register_Taxonomies {
/**
* Initialize the class
*/
public function __construct() {
add_action( 'init', array( $this, 'tags_support_all' ) );
add_action( 'pre_get_posts', array( $this, 'tags_support_query' ) );
}
View schema.xml
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="wpdata" version="1.1">
<types>
<!-- Geo location fields -->
<fieldType name="latitude_longitude" class="solr.LatLonType" subFieldSuffix="_coordinate"/>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="integer" class="solr.TrieIntField" omitNorms="true"/>
<fieldType name="long" class="solr.TrieLongField" omitNorms="true"/>
<fieldType name="float" class="solr.TrieFloatField" omitNorms="true"/>