Skip to content

Instantly share code, notes, and snippets.

@elhardoum
elhardoum / attachDomEvent.js
Last active May 18, 2018 12:55
Attach multiple events (click, load, etc) to a DOM element
(function(){
'use strict';
Object.prototype.attachDomEvent = function(eventName, callback)
{
eventName = eventName.replace(/^on/g, '');
if ( window.addEventListener ) {
this.addEventListener(eventName, callback, false);
} else if ( window.attachEvent ) {
@elhardoum
elhardoum / alexa.py
Last active October 3, 2017 20:58 — forked from masnun/alexa.py
Python One-liner to get your site's Alexa Rank - https://samelh.com/blog/2017/10/01/get-alexa-site-rank-python-php-javascript/
#!/usr/bin/env python
import urllib, sys, re
xml = urllib.urlopen('http://data.alexa.com/data?cli=10&dat=s&url=%s'%sys.argv[1]).read()
try: rank = int(re.search(r'<POPULARITY[^>]*TEXT="(\d+)"', xml).groups()[0])
except: rank = -1
print 'Your rank for %s is %d!\n' % (sys.argv[1], rank)
<?php
/**
* Get the user country code with package
*
* Requires geoiplookup package installed
* @link http://manpages.ubuntu.com/manpages/trusty/man1/geoiplookup.1.html
* @param $ip user IP string
* @param $args Array (optional parameters)
*/
<?php
// prevent direct access, only index.php can include this file
defined ( 'STRIPE_SECRET_KEY' ) || exit ( 'Direct access not allowed' . PHP_EOL );
use \Stripe\Stripe;
use \Stripe\Customer;
use \Stripe\Charge;
Stripe::setApiKey(STRIPE_SECRET_KEY);
@elhardoum
elhardoum / AmIFromContinent.php
Last active May 5, 2017 23:55
Check if current user is from certain continent
<?php
/**
* Get client IP
*/
function whatismyip() {
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
@elhardoum
elhardoum / hashScrollFix.js
Last active April 29, 2017 13:16
Adjust scroll when you have a fixed menu and a hash URL
var hashScrollFix = function() {
var hash = location.hash
, menu = $('.main-navi.fixed') // your fixed menu selector
, itm;
// I added menu.position().top >= 0 because I fade my menu to above viewport
// with a negative top position
if ( menu.is(':visible') && menu.position().top >= 0 ) {
itm = $(hash);
<?php
/**
* @link https://github.com/bainternet/PHP-Hooks
*/
function hooks() {
global $hooks;
if ( !isset($hooks) || !($hooks instanceof \Hooks) ) {
$hooks = new \Hooks;
<?php
/** @link https://samelh.com/blog/2016/12/22/add-custom-fields-bbpress-profile/ **/
// add field to bbP profile edit
add_action( "bbp_user_edit_after_contact", "se_add_city_field" );
function se_add_city_field() {
// a random selection of cities
$cities = array( "Seoul", "Mexico City", "Amsterdam", "Mumbai", "Agadir", "Egypt" );
// selected city, if any
<?php
if ( defined('BM_CUSTOM_LOAD') && isset($BM_Loader) && method_exists($BM_Loader, "init") ) {
if ( true /* my custom preferences */ ) {
$BM_Loader->init();
}
}
@elhardoum
elhardoum / modifier.relative_date.php
Last active October 25, 2016 00:11 — forked from dodyw/modifier.relative_date.php
Smarty relative date ... (time) agoSave to plugins folderUsage: <%$date|relative_date%>
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: modifier
* Name: relative_date
* Version: 1.1
* Date: November 28, 2008
* Author: Chris Wheeler <[email protected]>