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 / virtual-page.php
Created September 21, 2021 17:18
Make WordPress respond to a request for a URL that doesn't actually exist
<?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 / maybe-sideload.php
Created November 21, 2020 04:19
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.
<?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
*/
/**
<!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' />

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 July 29, 2020 20:35
How to dynamically generate a CSV with 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 March 24, 2024 11:37
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
<?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 April 4, 2020 03:51
Take an array of values and interpolate missing numbers
<?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 February 13, 2020 19:31
A MailChimp API wrapper for WordPress
<?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;
<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');
}
<?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' ) );
}