Skip to content

Instantly share code, notes, and snippets.

View dazecoop's full-sized avatar

Daze dazecoop

View GitHub Profile
@dazecoop
dazecoop / nginx.conf
Last active October 5, 2023 09:21
Prevent .git & .md file access with Ngnix
server {
location ~ /\.git {
return 404;
}
location ~\.(git|md)$ {
return 404;
}
}
@dazecoop
dazecoop / vanilla-js-number-format.js
Created October 3, 2023 16:17
Vanilla JS number format, useful for currency formatting. Similar to PHP's number_format()
// Format number to X decimal places.
function numberFormat(number, minimumFractionDigits = 0) {
return new Intl.NumberFormat('en', { minimumFractionDigits }).format(number);
}
@dazecoop
dazecoop / manually-test-wordpress-database-connection.php
Last active March 1, 2023 11:29
A few lines of code to manually test a WordPress database connection. Add below DB define's in your wp-config.php
/**
* Manually test WordPress database connection. Return useful errors upon failure.
*/
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno) {
die("❌ Failed to connect to MySQL: {$mysqli->connect_error}");
}
if ($result = $mysqli->query("SELECT * FROM `{$table_prefix}options`")) {
echo "✅ Connection OK. Returned rows are: {$result->num_rows}";
$result->free_result();
@dazecoop
dazecoop / Setup remote git repository.md
Last active February 23, 2023 12:13
Setup a remote git repository on server for git pull latest changes

git init
git remote add origin git@github.com:...GITHUB_URL...
git fetch
git checkout -f main
git remote -v

Then
git pull

@dazecoop
dazecoop / vanilla-js-days-of-week-array-of-objects.js
Last active November 21, 2022 16:04
Vanilla JS days of week as an array of objects. (Mon, Tues, Weds) or (Monday, Tuesday, Wednesday) format. Localization support
const days = Array(7)
.fill(new Date())
.map((el, i) =>
new Date(el.setDate(el.getDate() - el.getDay() + i))
.toLocaleString('en', { weekday: 'short' })) // change 'en' for different locales, or 'short'/'long' for short/long weekday titles
.map((label, value) => {
return {
value,
label,
}
@dazecoop
dazecoop / vanilla-js-full-current-date.js
Last active November 8, 2022 10:22
Vanilla JS full current date, with no dependencies & has localization support. One-liner
// Formatted in D MMMM YYYY
// Eg; 18 October 2022
`${(new Date().getDate())} ${(new Date().toLocaleString("en", { month: "long" }))} ${(new Date().getFullYear())}`
// Formatted in D{ordinal} MMMM YYYY
// Eg; 18th October 2022
`${(new Date().getDate())}${{one:'st',two:'nd',few:'rd',other:'th'}[new Intl.PluralRules('en-GB', { type: 'ordinal' }).select(new Date().getDate())]} ${(new Date().toLocaleString("en", { month: "long" }))} ${(new Date().getFullYear())}`
// Formatted in DD-MM-YYYY
// Eg; 18-09-2022
@dazecoop
dazecoop / Github markdown image cache clear with PHP cron.md
Last active October 7, 2022 08:34
Clear image cache for Github markdown images with PHP cron script

Clear image cache for Github markdown images with PHP cron script

🤔 What?

As per this post dated back from 2015, there is an issue with Github caching images you've embedded in markdown, eg README.md files.

This script is a PHP cron that accepts an array of images & clears their cache.

It should be run at a fairly sensible interval that clears the cache of the image(s) you've specified.

🔥 How to run

sudo nano .zshrc
@dazecoop
dazecoop / Github highlight approved & request change PR's.md
Last active October 4, 2023 15:39
Highlight approved PR's on Github with some custom CSS, apply using browser extension

Github highlight approved & requested changes PR's with Custom CSS

I find PR's that have been approved or have requested changes difficult to spot in the list of other PR's on Github. This CSS fixes that, by highlighting those that are approved in green & requested changes in red.

The custom CSS can be applied by a Chrome or other browser extenion such as Stylebot or Amino Editor.

🤔 Comparison

Before After
before after
@dazecoop
dazecoop / form.js
Created February 23, 2022 11:26
Vanilla JS form submit listener, preventDefault with native browser required fields valid check
document.addEventListener('submit', function (e) {
const form = e.target;
let isValid = form.checkValidity();
if (!isValid) {
return false;
}
e.preventDefault();
let data = new FormData(form);