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 / maybe-sideload-image.php
Created August 29, 2024 02:42
Maybe sideload an image URL? This function will download an image URL to your media library but it will only do it if the URL hasn't been downloaded before.
<?php
/**
* Given a URL to an image maybe sideload into the media library
*
* @param string $url The URL to sideload into the media library
* @param string $name The title to associate with the image
* @param integer $associated_post_id The post ID to associate the sideloaded image to
*/
function maybe_sideload_image( $url = '', $name = '', $associated_post_id = 0 ) {
require_once ABSPATH . 'wp-admin/includes/media.php';
@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 / 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>',
<?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 / all-post-type-archives.php
Created June 2, 2023 14:49
Adds /all/ URL after a post type archive to be able to show all items for that archive.
<?php
/**
* Plugin Name: Show All Post Type Archives
* Description: Add support for /all/ after a post type archive
* Version: 0.0.1
* Plugin URI: https://gist.github.com/kingkool68/efa01ecff91bc722cd3f667ae509a37b
* Author: Russell Heimlich
* Author URI: https://github.com/kingkool68
*/
class RH_Show_All_Archives {
@kingkool68
kingkool68 / .htaccess
Last active October 12, 2022 17:50
Remove the User endpoints from the WordPress Rest API
<IfModule mod_rewrite.c>
RewriteEngine On
# Block _method=GET query string used for compatibility in the REST API
# See https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/#_method-or-x-http-method-override-header
RewriteCond %{QUERY_STRING} \b_method=GET\b [NC]
RewriteRule ^ - [F]
</IfModule>