Skip to content

Instantly share code, notes, and snippets.

@IronGhost63
IronGhost63 / generate-json.php
Created September 1, 2025 08:20
Fuzzy Search inside JSON data using Fuse.js
<?php
/**
* File use to generate gzipped JSON data
*/
$data = connect_and_fetch_data();
$formatted = array_map( function( $item ) {
// Format data anyway you want. Then adjust 'keys' option in Fuse.js accordingly
$item = format_your_data( $item );
@IronGhost63
IronGhost63 / variable-event.js
Created September 1, 2025 07:43
Dispatch and Listen to events when variable change value.
const stateStorage = {};
window.STATE = new Proxy( stateStorage, {
set: ( target, key, value ) => {
const stateUpdateEvent = new CustomEvent( 'stateUpdated', {
detail: {key, value}
});
target[key] = value;
/*
* Grid container. Based on Bootstrap 4.
*
* Partially works. No offset support.
*/
.grid-container {
display: grid;
grid-template-columns: repeat($grid-columns, 1fr);
margin: 0 auto;
@IronGhost63
IronGhost63 / query.php
Last active September 30, 2020 12:18
WP_Query only parent categories
<?php
namespace App;
use \WP_Query;
$categories = get_categories( ['parent' => 0] );
$ids = array_map( function( $category ) {
return $category->term_id;
}, $categories);
@IronGhost63
IronGhost63 / date.php
Last active August 9, 2020 19:08
Find first and last date of given week
<?php
// Get timestamp
$date_string = 'June 19, 2020';
$timestamp = strtotime( $date_string );
// Get weekday for given date
// Return 0-6 for Sun to Sat
$weekday = date( 'w', $timestamp );
// Calculate first and last date of given week
@IronGhost63
IronGhost63 / functions.php
Last active May 29, 2020 10:14
hook init
<?php
add_action( 'init', function() {
// Header will contains HTTP_TRUE_CLIENT_IP if accessing via Akamai
if( isset( $_SERVER['HTTP_TRUE_CLIENT_IP'] ) ) {
ob_start( function( $output ) {
return str_replace( 'wpengineurl.com', 'realdomain.com/sub' $output );
} );
}
} );
@IronGhost63
IronGhost63 / clock.php
Last active March 19, 2020 07:32
reformat clock-in clock-out
<?php
// ประกาศตัวแปรรอทิ้งไว้ จริงๆ ใช้ $data อันเดียวก็ได้ แล้ว map ทับเอา
$raw = [];
$data = [];
// ข้อมูลตัวอย่าง
$timein = [
[
'id' => 100287,
'date' => '2020-02-26',
@IronGhost63
IronGhost63 / upload.js
Last active February 28, 2020 15:10
upload.js
jQuery('.my-form').on('submit', (e) => {
e.preventDefault();
let form = e.target;
let formData = new FormData( form );
let userdata_access_token = getUserdataAccessToken();
// ถ้าต้องการเพิ่มข้อมูล ใช้ .append ได้เลย
formData.append('userdata_access_token', userdata_access_token);
// แล้วเอา formData ส่งไปเป็น data ของ $.ajax
@IronGhost63
IronGhost63 / app.js
Last active February 16, 2020 04:20
Add or Update product (use Array.prototype.findIndex())
let products = [
{
id: 1,
quantity: 10,
},
{
id: 2,
quantity: 2,
}
];
@IronGhost63
IronGhost63 / app.js
Created February 16, 2020 02:55
Add or Update product
let products = [
{
id: 1,
quantity: 10,
},
{
id: 2,
quantity: 2,
}
];