Skip to content

Instantly share code, notes, and snippets.

View nlemsieh's full-sized avatar

Nabil Lemsieh nlemsieh

View GitHub Profile
@nlemsieh
nlemsieh / class-settings.php
Last active January 17, 2020 13:31
Settings class utility for WP plugins/themes
<?php
class Settings {
/**
* @var string OPTION_NAME
*/
const OPTION_NAME = 'myplugin_settings';
@nlemsieh
nlemsieh / script.js
Last active August 25, 2019 00:09
Create a toggle button with Javascript
const toggleButtons = document.getElementsByClassName('as-toggle');
for(let toggle of toggleButtons){
const span = document.createElement('span');
if(toggle.checked){
span.classList.add('is-checked');
}
span.classList.add('toggle-button');
@nlemsieh
nlemsieh / delete-transient-prefix.php
Last active October 29, 2018 22:02
Delete WP transients using a prefix
<?php
global $wpdb;
$prefix = "foo";
// We get all the transients prefixed by our `$prefix`
$sql = 'select option_name from ' . $wpdb->prefix . 'options where option_name like "_transient_' . $prefix . '%"';
$results = $wpdb->get_results( $sql, ARRAY_A );
$transients = [];
array_walk_recursive( $results, function ( $value ) use ( &$transients ) {
@nlemsieh
nlemsieh / check-form-state-change.js
Last active October 29, 2018 20:35
Check if a form has changed
const form = document.forms["foo"];
// Save initial state
const initialFormData = new FormData(form);
const initialFormState = JSON.stringify(Array.from(initialFormData.entries()));
// Change form controls' values
form.elements["text"].value = "foo";
// Get new form state
@nlemsieh
nlemsieh / bulk_rename_shell.md
Last active May 21, 2017 23:27
Rename .jpg and .png files to a given string
#!/bin/bash
if [ "$1" == "" ]; then
exit 0
fi
REPLACE_FILENAME=`echo $1|sed 's/ /\-/g'` 
FILES_PATH='./';
if [ "$2" != "" ]; then
FILES_PATH=$2
fi
@nlemsieh
nlemsieh / functions.php.md
Last active May 14, 2017 17:56
Remove MailChimp for Wordpress plugin's comments
// Remove Mailchimp for Wordpress plugin's comments
 add_action('get_footer',function (){ ob_start(function ($o){
    return preg_replace('/\n?<!--.*?mailchimp.*?>/mi','',$o); }); });
 add_action('wp_footer',function (){ ob_end_flush(); }, 999);
@nlemsieh
nlemsieh / lumen_packages.md
Last active March 21, 2017 19:38
User Laravel packages in Lumen

To add support for Laravel packages in Lumen, follow these instructions

To support Laravel Package in Lumen:

1. Enable Facades

Go to bootstrap/app.php and uncomment this line:

$app->withFacades();

2. Install package

composer require package-name

3. Register package class

emmet.require('filters').add('php', function process(tree) {
_.each(tree.children, function(node) {
// define variable name
if (node.name() == 'data' && node.parent == ''){
node.start = '\\$this->request->data';
}else{
node.start = (node.parent == '' ? '\\$' : '') + node.name();
<?php
/**
* Create a web friendly URL slug from a string.
*
* Although supported, transliteration is discouraged because
* 1) most web browsers support UTF-8 characters in URLs
* 2) transliteration causes a loss of information
*
* @author Sean Murphy <sean@iamseanmurphy.com>
* @copyright Copyright 2012 Sean Murphy. All rights reserved.