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 / roshHashanah.js
Created July 27, 2017 21:36
Calculate when Rosh Hashanah is
var roshHashanah = function(y) {
var g = (y % 19) + 1
var n = (Math.floor(y / 100) - Math.floor(y / 400) - 2) + ((765433 / 492480 * ((12 * g) % 19)) + ((y % 4) / 4) - ((313 * y + 89081) / 98496));
var r = Math.floor(n);
var m = 9;
var h = m;
var d = r;
if (r > 30) { h = 10; d = r - 30; }
var a = Math.floor((14 - h) / 12);
var i = y - a;
<?php
function base64_upload($base64, $filename = null) {
if (!$filename) $filename = md5($base64);
if (!pathinfo($filename, PATHINFO_EXTENSION)) $filename .= '.jpg';
$content = base64_decode($base64);
$upload_dir = wp_upload_dir();
$file = $upload_dir['path'] . DIRECTORY_SEPARATOR . $filename;
file_put_contents($file, $content);
<?php
function sideroad_login_logo() {
$ratio = (get_custom_header()->height / get_custom_header()->width);
$width = 320;
$height = ($width * $ratio);
?>
<style type="text/css">
#login h1 a, .login h1 a {
background-image: url('<?php header_image(); ?>');
width: <?php echo $width; ?>px;
@kodie
kodie / meteor-generate-password.sh
Last active May 12, 2021 16:55
Generates a bcrypt string of a sha256sum of a password to be manually entered into your Meteor app's database
bcrypt $(echo -n "PASSWORD HERE" | sha256sum | cut -d " " -f 1) && echo
# or this
htpasswd -bnBC 10 "" $(echo -n "PASSWORD HERE" | sha256sum | cut -d " " -f 1) | tr -d ':\n' | sed 's/$2y/$2a/'
# orrrr this
htpasswd -bnBC 10 "" $(echo -n "PASSWORD HERE" | shasum -a 256 | cut -d " " -f 1) | tr -d ':\n' | sed 's/$2y/$2a/'
@kodie
kodie / Modify_HTML.php
Last active November 16, 2018 13:55
Use PHP to modify an HTML fragment with the proper encoding
<?php
$html = '<article class="grid-item"><p>Hello World</p></article><article class="grid-item"><p>Goodbye World</p></article>';
$head = '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML($head . $html);
$xpath = new DOMXPath($dom);
// Loop through all article.grid-item elements and add the "invisible" class to them
@kodie
kodie / wp_acf_tax_migrate.php
Created December 5, 2018 20:24
Migrate ACF field data to a taxonomy
<?php
$query = new WP_Query(array(
'post_type' => 'employee',
'posts_per_page' => -1
));
while($query->have_posts()) {
$query->the_post();
$taxonomy = 'department';
@kodie
kodie / wp_range_post_count.php
Last active July 23, 2019 19:35
WordPress pagination indicator "Showing 20 – 29 of 138 posts"
<?php
$paged = max(1, (int) $wp_query->query_vars['paged']);
$first_post_index = $paged === 1 ? 1 : ((($paged - 1) * (int) $wp_query->query_vars['posts_per_page']) + 1);
$last_post_index = (($first_post_index + (int) $wp_query->post_count) - 1);
echo "Showing {$first_post_index} – {$last_post_index} of {$wp_query->found_posts} posts";
?>
eyJ2ZXJzaW9uIjoxLjMsImZlYXR1cmVzIjp7ImxvY2F0aW9uIjp7InJvb20iOnRydWUsIm91dHNpZGUiOnRydWV9fSwic3RvcmVzIjp7Indvb2QiOjI0MiwiZnVyIjoxMzUuNSwidGVldGgiOjU2LCJiYWl0Ijo0NywibWVhdCI6MTYuNSwic2NhbGVzIjoxNCwiY2hhcm0iOjMsImN1cmVkIG1lYXQiOjgzLCJjbG90aCI6NSwibGVhdGhlciI6MjZ9LCJjaGFyYWN0ZXIiOnt9LCJpbmNvbWUiOnsiZ2F0aGVyZXIiOnsiZGVsYXkiOjEwLCJzdG9yZXMiOnsid29vZCI6Mn0sInRpbWVMZWZ0Ijo0fSwiYnVpbGRlciI6eyJkZWxheSI6MTAsInN0b3JlcyI6eyJ3b29kIjoyfSwidGltZUxlZnQiOjEwfSwiaHVudGVyIjp7ImRlbGF5IjoxMCwic3RvcmVzIjp7ImZ1ciI6NiwibWVhdCI6Nn0sInRpbWVMZWZ0Ijo1fSwidHJhcHBlciI6eyJkZWxheSI6MTAsInN0b3JlcyI6eyJtZWF0IjotMSwiYmFpdCI6MX0sInRpbWVMZWZ0Ijo1fSwiY2hhcmN1dGllciI6eyJkZWxheSI6MTAsInN0b3JlcyI6eyJtZWF0IjotNSwid29vZCI6LTUsImN1cmVkIG1lYXQiOjF9LCJ0aW1lTGVmdCI6MX0sInRhbm5lciI6eyJkZWxheSI6MTAsInN0b3JlcyI6eyJmdXIiOjAsImxlYXRoZXIiOjB9LCJ0aW1lTGVmdCI6MX19LCJ0aW1lcnMiOnt9LCJnYW1lIjp7ImJ1aWxkZXIiOnsibGV2ZWwiOjR9LCJ0ZW1wZXJhdHVyZSI6eyJ2YWx1ZSI6NCwidGV4dCI6ImhvdCJ9LCJmaXJlIjp7InZhbHVlIjo0LCJ0ZXh0Ijoicm9hcmluZyJ9LCJidWlsZGluZ3MiOnsiY2FydCI6MSwidHJhcCI6NywiaHV0
@kodie
kodie / wp_custom_orderby.php
Last active November 24, 2020 06:49
Create a custom query 'orderby' type that uses data from other tables in WordPress
<?php
// Fetch data required for custom orderby types and left join it into the query
add_filter('posts_join_paged', 'custom_orderby_joiner', 10, 2);
function custom_orderby_joiner($join_paged_statement, $wp_query) {
$orderby = $wp_query->query_vars['orderby'];
if ($orderby === 'popularity') {
$join_paged_statement .= " INNER JOIN (SELECT postid, pageviews FROM wp_popularpostsdata) popularpostsdata ON popularpostsdata.postid = wp_posts.ID";
}
@kodie
kodie / add_quotes_to_serialized_string.php
Created September 28, 2019 16:04
Adds double quotes back to a serialized string after they have been removed and then unserializes it to return an array
<?php
$str = unserialize(str_replace('s:0:;', 's:0:"";', preg_replace('/(.*?)(s:\d+:)([^{};]*?)([;}]+)/', '$1$2"$3"$4', $str)));
?>