Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

ʞↃ∀ɾ thagxt

🎯
Focusing
View GitHub Profile
@thagxt
thagxt / dark-mode.js
Last active Jan 10, 2020
Detect if OS is in dark mode with Vanilla JS and add class to BODY
View dark-mode.js
document.addEventListener("DOMContentLoaded", function(){
// Detect dark mode with Vanilla JS and add class to BODY
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.classList.add("dark-mode"); // your class here
}
// Turn Dark Mode on/off, manually
document.querySelector('.lightsoff').onclick = function() {
document.querySelector('body').classList.toggle('dark-mode');
}
@thagxt
thagxt / dark-mode.css
Last active Oct 27, 2019
Detect if the OS is in dark mode in browsers
View dark-mode.css
/* Light mode */
@media (prefers-color-scheme: light) {
body {
background-color: #FFFFFF;
color: #000000;
}
}
/* Dark mode */
@media (prefers-color-scheme: dark) {
@thagxt
thagxt / url-check.php
Last active Oct 27, 2019
Check if URL exists in PHP - With this function you can check if a URL is really 404 or else
View url-check.php
<?php
// With this function you can check response code if it really is 404 or nah
function isValidUrl($url){
// first do some quick sanity checks:
if(!$url || !is_string($url)){
return false;
}
// quick check url is roughly a valid http request: ( http://blah/... )
if( ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url) ){
@thagxt
thagxt / dl-ex.php
Last active Oct 24, 2020
Download & Extract .zip with cURL directly on your server
View dl-ex.php
<?php
$url = "https://wordpress.org/latest.zip"; // URL of what you wan to download
$zipFile = "wordpress.zip"; // Rename .zip file
$extractDir = "extracted"; // Name of the directory where files are extracted
$zipResource = fopen($zipFile, "w");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
@thagxt
thagxt / .htaccess
Created Aug 31, 2016
Make a redirect match with or without closing /
View .htaccess
# Make a redirect match with or without closing /
# example.com/highlights is the same as example.com/highlights/
# something that doesn't happen with Redirect 301
RedirectMatch 301 ^/highlights/?$ /highlights.html
@thagxt
thagxt / upgrade.cli
Created Aug 31, 2016
Magento 2 > Upgrade to next version via composer
View upgrade.cli
composer require magento/product-community-edition 2.1.1 --no-update
composer update
rm -rf var/di var/generation
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento indexer:reindex
@thagxt
thagxt / get-product-info.php
Created Aug 25, 2016
Magento 2 > product page > get Product URL, get Product Name and get Product ID
View get-product-info.php
<?php
// get current page URL
$URL = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
// get current product name & ID
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); //get current product
$id = $product->getId();
$title = $product->getName();
@thagxt
thagxt / mage2-multiweb-subdir.md
Last active May 18, 2021
Set up Magento 2 multiple websites in sub directories
View mage2-multiweb-subdir.md

Set up Magento 2 multiple websites in sub directories

  1. Go to Admin > Stores > All Stores
  2. Click > Create Web Site
  3. In the Name field, enter store name.
    • e.g. Japan
  4. In the Code field, enter a unique string without spaces and > Save Web Site
    • e.g. super_jp
  5. Create Store
  6. Create Store View
@thagxt
thagxt / strip-https
Last active Jun 28, 2017
Remove both http:// & https:// from URL
View strip-https
<?php
$url = "https://example.com/";
$url = preg_replace('/^https?:\/\//', '', $url);
// output: example.com/
?>
View regular-expressions.md

Regular expressions syntax

Regular Expression Matches
foo The string “foo”
^foo “foo” at the start of a string
foo$ “foo” at the end of a string
^foo$ “foo” when it is alone on a string
[abc] a, b, or c
[a-z] Any lowercase letter