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 / 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 / fluid-math-sass-function.scss
Last active March 14, 2024 16:29
Sass function for calculating fluid values using clamp() for padding or font-sizes or anything really. Example: font-size: fluid(16px, 32px) --> font-size: clamp( 1rem, 0.143rem + 2.857vw, 2rem );
@use "sass:math";
// via https://css-tricks.com/snippets/sass/px-to-em-functions/
$browser-context: 16;
@function rem($pixels) {
@if (unitless($pixels)) {
$pixels: $pixels * 1px;
}
@kingkool68
kingkool68 / get_attachment_id_by_filename.php
Created December 29, 2023 04:00
Get a WordPress attachment ID by it's filename. Searches '_wp_attached_file' post_meta values looking for matching filenames. If multiple are found the most recent ID by post_date is returned.
<?php
/**
* Search post attachments where the given filename matchs the attached file path
*
* @link https://wordpress.stackexchange.com/a/405142/2744
*
* @param string $filename The filename to search
*
* @return int The attachment ID of the first result sorted by post_date in reverse chronological order (most recent first)
*/
@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();
<?php
function rh_filter_the_content( $the_content = '' ) {
$blocks = parse_blocks( $the_content );
$new_block = array(
'blockName' => 'core/paragraph',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '<p>Hello World</p>',
'innerContent' => array(
'<p>Hello World</p>',
@kingkool68
kingkool68 / index.php
Created October 23, 2023 00:55
Log $_GET or $_POST data to a CSV file. Example: index.php?bar=bar&foo=foo&baz=baz&bad-data=dont-save would append the following to the data.csv file: 1698022463,foo,bar,baz
<?php
// Merge the expected data with the data recieved
$expected_data = array(
'timestamp' => '',
'foo' => '',
'bar' => '',
'baz' => '',
);
$data = array_merge( $expected_data, $_REQUEST );
<?php
function rh_filter_the_content( $the_content = '' ) {
$blocks = parse_blocks( $the_content );
$new_block = array(
'blockName' => 'core/paragraph',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '<p>Hello World</p>',
'innerContent' => array(
'<p>Hello World</p>',
<?php
function rh_filter_the_content( $the_content = '' ) {
$blocks = parse_blocks( $the_content );
$new_block = array(
'blockName' => 'core/paragraph',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '<p>Hello World</p>',
'innerContent' => array(
'<p>Hello World</p>',
@kingkool68
kingkool68 / Dockerfile
Last active September 27, 2023 16:40
Compiling Photon OpenCV PHP extension via Docker
FROM php:8.1-apache
RUN apt-get update --fix-missing \
&& apt-get install -y --no-install-recommends \
vim \
git \
subversion \
libopencv-dev \
libwebp-dev \
libgif-dev \
@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,