Skip to content

Instantly share code, notes, and snippets.

View kodie's full-sized avatar
💻
#doworkson

Kodie Grantham kodie

💻
#doworkson
View GitHub Profile
@kodie
kodie / get_images_sizes.php
Created October 21, 2020 20:11
A function to get with, height, and if crop is available for all image sizes in WordPress regardless if they are built-in sizes or custom set ones
<?php
// Get information about available image sizes
function get_image_sizes($size = null) {
$sizes = array();
$size_data = wp_get_additional_image_sizes();
$size_names = get_intermediate_image_sizes();
$default_sizes = array_diff($size_names, array_keys($size_data));
if ($size) $size_names = array($size);
@kodie
kodie / _grid.scss
Last active January 18, 2021 22:22
SCSS grid @mixin
@mixin grid($cols: 4, $gap: 2rem, $item: '> *') {
&:after {
clear: left;
content: '';
display: block;
}
#{$item} {
box-sizing: border-box;
float: left;
@kodie
kodie / wp_get_account_info.php
Last active September 22, 2020 18:11
Gets user data in the form of an array with user meta attached, empty results filtered, and single results collapsed into a single value in WordPress
<?php
// Gets user data in the form of an array with user meta attached that has empty results filtered,
// and optionally single results collapsed into a single value and/or only single values returned.
function get_account_info($user_id = null, $collapse = true, $single = false) {
if (!$user_id) $user_id = get_current_user_id();
$user = get_userdata($user_id);
if (!$user) return false;
@kodie
kodie / wp_preview_emails.php
Created September 9, 2020 19:19
Stops any emails from sending and instead prints the email message out on the page in WordPress
<?php
// Stops any emails from sending and instead prints the email message out on the page
add_filter('wp_mail', 'preview_emails', 1, 99);
function preview_emails($args) {
echo $args['message'];
die();
}
?>
@kodie
kodie / wp_get_term_filter_options.php
Created September 4, 2020 14:44
Get available term combinations for filtering in WordPress
<?php
// Takes an array with a term taxonomy as the item keys and term ids as their values and
// returns the same array but with the values set as term ids that have posts associated
// with the term combination. (PHP7 and above only)
function get_term_filter_options($taxonomies, $query_args = array()) {
array_walk($taxonomies, function(&$value, $taxonomy) use($taxonomies, $query_args) {
$posts = null;
$others = array_filter($taxonomies, function($value, $key) use($taxonomy) {
return $key !== $taxonomy && !empty($value);
}, ARRAY_FILTER_USE_BOTH);
@kodie
kodie / lowest_highest_multidimensional_value.php
Created August 29, 2020 00:37
Get the key of the array with the lowest or highest value inside of a multidimensional array
<?php
$arr = array(
array(
'id' => 123,
'amount' => 39
),
array(
'id' => 789,
'amount' => 27
),
@kodie
kodie / functions.php
Last active August 27, 2020 19:55
Allows for a custom post detail page in WordPress
<?php
// Allows for a custom post detail page
add_action('init', 'hm_alternative_post_page');
function alternative_post_page() {
add_rewrite_rule('story/([^/]+)/?$', 'index.php?page_id=13317&child_post_name=$matches[1]', 'top');
}
// Setup our custom `child_post_slug` query var for custom post detail pages
add_filter('query_vars', 'alternative_post_page_var');
function hm_alternative_post_page_var($vars) {
@kodie
kodie / wp_custom_curl_timeout.php
Created August 21, 2020 17:57
Allows you to set a custom timeout for WordPress cURL requests
<?php
// Set HTTP Request Timeout
add_filter('http_request_args', 'my_http_request_args', 100, 1);
function my_http_request_args($r) {
$r['timeout'] = 30;
return $r;
}
// Set HTTP Request Timeout
add_filter('http_request_timeout', 'my_custom_http_request_timeout', 101);
@kodie
kodie / kill_all_processes.sql
Created August 21, 2020 17:51
A MySQL command to kill all processes
# Run this on a MySQL server and it will echo out a command for you to copy, paste, and run to kill all currently running MySQL processes
SELECT GROUP_CONCAT(CONCAT('KILL ',id,';') SEPARATOR ' ') 'Paste the following query to kill all processes' FROM information_schema.processlist WHERE user<>'system user'\G
@kodie
kodie / wp_add_pagination_to_pages.php
Created August 21, 2020 16:59
This makes it possible to use `/page/` URLs with non-archive pages such as single pages in WordPress
<?php
// This makes it possible to use `/page/` URLs with non-archive pages such as single pages
add_action('init', 'add_pagination_to_pages');
function add_pagination_to_pages() {
$pages = array(
'flex/assessments' => 220460,
'flex/collections' => 215682,
'flex/lesson-plans' => 215685,
'flex/my-standards' => 220768,
'flex/resources' => 220383,