Skip to content

Instantly share code, notes, and snippets.

View ahmu83's full-sized avatar
🎯
Focusing

Ahmad Karim ahmu83

🎯
Focusing
View GitHub Profile
<?php
// This will break if the ENUM values have comma (,) in them
function get_enum_values($wpdb, $table, $field) {
$values = array();
$table = "{$wpdb->prefix}{$table}";
$query = "SHOW COLUMNS FROM {$table} WHERE Field = '{$field}'";
$results = $wpdb->get_results($query, ARRAY_A);
@ahmu83
ahmu83 / dispatchAnEvent.js
Created June 12, 2023 15:00
Dispatch an event using CustomEvent constructor, that can be listened to using addEventListener method
/**
* Dispatch an event using CustomEvent constructor,
* that can be listened to using addEventListener method
*
* Example usage:
*
* dispatchAnEvent('my-custom-event', {data: 'The data'})
*
* window.addEventListener('my-custom-event', function(event) {
*
@ahmu83
ahmu83 / redirect.js
Created June 12, 2023 15:10
Function to redirect to a url with the all the current URL params
/**
* Function to redirect to a url with the all the current URL params
*
* @param string url
* @param object params
* @param boolean dontCarryParams
*
* usage:
* redirect('http://example.com', {param1: 111, param2: 222})
* will redirect to http://example.com/?param1=111&param2=222
@ahmu83
ahmu83 / elementInViewport.js
Created June 12, 2023 15:23
Check if an element is in the viewport using getBoundingClientRect method
/**
* Check if an element is in the viewport using getBoundingClientRect method
*
* Copied from: https://www.30secondsofcode.org/js/s/element-is-visible-in-viewport/
*
* @param object el Element object
* @param boolean partiallyVisible If element is partially visible or not
* @return boolean
*/
function elementInViewport(el, partiallyVisible = true) {
@ahmu83
ahmu83 / Javascript Cookie Helper.js
Last active June 15, 2023 10:45
Javascript Cookie Helper for easy cookie manipulation
@ahmu83
ahmu83 / enqueueScript.js
Created September 20, 2023 16:49
Dynamic Script Enqueuing for the DOM
/**
* Enqueue scripts dynamically into DOM.
* Example usage
*
* enqueueScript({
* 'html2canvas': {
* id: 'html2canvas',
* url: 'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js',
* onload: () => {
* // code
@ahmu83
ahmu83 / enqueueStyle.js
Created September 20, 2023 16:52
Dynamic Style Enqueuing for the DOM
/**
* Enqueue styles dynamically into DOM.
* Example usage
*
* enqueueStyle({
* 'splide-styles': {
* url: 'https://cdn.jsdelivr.net/npm/@splidejs/splide@4.1.4/dist/css/splide.min.css',
* },
* });
*
@ahmu83
ahmu83 / US_States_and_Cities.json
Created July 20, 2016 16:50
List of US States and Cities in JSON format.
{
"New York": [
"New York",
"Buffalo",
"Rochester",
"Yonkers",
"Syracuse",
"Albany",
"New Rochelle",
"Mount Vernon",
@ahmu83
ahmu83 / bulk-search-replace.sh
Last active May 20, 2024 13:55
A Bash script designed to automate the search and replacement of strings in SQL files (or any large text file)
#!/bin/bash
# USAGE:
# sh bulk-search-replace.sh dbfile.sql
# OR
# chmod +x bulk-search-replace.sh
# ./bulk-search-replace.sh dbfile.sql
#
# sed or gsed
#
@ahmu83
ahmu83 / import-mysql-db.sh
Created May 20, 2024 13:58
This is a Bash script to import a MySQL database using either standard TCP/IP connections or Unix sockets
#!/bin/bash
# USAGE:
#
# sh import-mysql-db.sh
# bash import-mysql-db.sh
#
# OR
#
# chmod +x import-mysql-db.sh