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 / examples.php
Created June 23, 2022 20:45
Helper functions to make generating WordPress post type labels and taxonomy labels easier
<?php
// Register a Book post type
$args = array(
'labels' => generate_post_type_labels( 'book', 'books' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
@kingkool68
kingkool68 / class-coderpad-text-image-block.php
Last active May 19, 2023 15:24
How I use Advanced Custom Fields' Blocks to make custom components for WordPress sites. Logic is all in PHP, HTML templating is done in Twig via https://github.com/kingkool68/sprig This is in response to a tweet https://twitter.com/Gravnetic/status/1517511382102540288
<?php
/**
* A block with an image and text on the side
*/
class CoderPad_Text_Image_Block {
/**
* Get an instance of this class
*/
public static function get_instance() {
@kingkool68
kingkool68 / class-coderpad-text-image-block.php
Created April 22, 2022 15:31
How I use Advanced Custom Fields' Blocks to make custom components for WordPress sites. Logic is all in PHP, HTML templating is done in Twig via https://github.com/kingkool68/sprig
<?php
/**
* A block with an image and text on the side
*/
class CoderPad_Text_Image_Block {
/**
* Get an instance of this class
*/
public static function get_instance() {
@kingkool68
kingkool68 / merge-current-branch-to-target-branch.sh
Created February 1, 2022 19:55
Quick little helper script to speed up your Git workflow if you ever need to merge a branch into another frequently (like your current feature branch into a `staging` branch). Useful as a custom command in the Git GUI Fork (https://fork.dev/)
#!/bin/bash
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
TARGET_BRANCH=staging
git fetch origin $TARGET_BRANCH
git checkout $TARGET_BRANCH
git merge "$CURRENT_BRANCH" -m "Merge branch '$CURRENT_BRANCH' into $TARGET_BRANCH"
git push origin $TARGET_BRANCH
git checkout "$CURRENT_BRANCH"
@kingkool68
kingkool68 / simple-google-apis.php
Created January 20, 2022 22:05
Interacting with Google API's using their PHP libraries is a nightmare. Skip the headache by authorizing a Guzzle client and make raw HTTP requests instead!
<?php
$key_file_path = __DIR__ . '/service-account-credentials.json';
$google_client = new \Google_Client();
$google_client->setAuthConfig( $key_file_path );
// Set the scopes of whatever you need access to
// See https://developers.google.com/identity/protocols/oauth2/scopes
$google_client->setScopes( array( 'https://www.googleapis.com/auth/analytics.readonly' ) );
$http_client = $client->authorize();
@kingkool68
kingkool68 / wp-config.environments.php
Created November 27, 2021 04:10
Managing `wp-config.php` files for different environments. This allows you to version control your configurations.
<?php
/** Loads environment specific config files for WordPress.
* See https://github.com/studio24/wordpress-multi-env-config and/or http://abandon.ie/notebook/wordpress-configuration-for-multiple-environments
*
* Environment can be:
* 1. Directly loaded via an Environment variable (Name: WP_ENV)
* 2. Looks the ServerName attribute
* 3. Default to production (if no other one is found)
*
* ** Usage **
@kingkool68
kingkool68 / loading-csv-data-for-google-charts.php
Last active October 26, 2021 16:59
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/
<?php
/**
* Plugin Name: River Height Levels
* Plugin URI: https://gist.github.com/kingkool68/567527d28f82bd3a0107970b624fea0a
* Description: Fetch the current river height values and display it as different data visualization.
* Version: 0.0.2
* 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
<?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 ) {
<?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 / 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