Skip to content

Instantly share code, notes, and snippets.

@scriptex
scriptex / rename.js
Last active April 10, 2023 23:30
Rename all files in a folder with NodeJS
const { join } = require('path');
const { readdirSync, renameSync } = require('fs');
const [dir, search, replace] = process.argv.slice(2);
const match = RegExp(search, 'g');
const files = readdirSync(dir);
files
.filter(file => file.match(match))
.forEach(file => {
const filePath = join(dir, file);
@hagemann
hagemann / slugify.js
Last active September 4, 2024 02:45
Slugify makes a string URI-friendly
function slugify(string) {
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìıİłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;'
const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------'
const p = new RegExp(a.split('').join('|'), 'g')
return string.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word characters
@yusinto
yusinto / updateCountry_fetch.js
Last active October 3, 2024 22:20
Graphql mutation with fetch
const updateCountryFetch = async (countryId, happinessFactor, population) => {
const query = JSON.stringify({
query: `mutation {
updateCountry(
id: "${countryId}"
happinessFactor: ${happinessFactor}
population: ${population}) { id }
}
`
});
@jazibsawar
jazibsawar / delete_custom_post_type.php
Created October 2, 2017 12:28
WordPress $wpdb: Delete all posts of a custom post type with its associated meta data (taxonomies, post meta) using SQL query & $wpdb
<?php
function delete_custom_posts($post_type = 'post'){
global $wpdb;
$result = $wpdb->query(
$wpdb->prepare("
DELETE posts,pt,pm
FROM wp_posts posts
LEFT JOIN wp_term_relationships pt ON pt.object_id = posts.ID
LEFT JOIN wp_postmeta pm ON pm.post_id = posts.ID
WHERE posts.post_type = %s
@efalayi
efalayi / webpack.config.js
Last active June 22, 2020 07:32
Full webpack config file for bootstrap and fontawesome
const autoprefixer = require('autoprefixer');
const webpack = require('webpack');
const path = require('path');
const precss = require('precss');
const TransferWebpackPlugin = require('transfer-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
devtool: 'eval',
@jessepearson
jessepearson / change_incart_bookings_expiry_minutes.php
Last active October 4, 2020 09:07
Change the amount of minutes for when an In Cart booking expires and gets deleted in WooCommerce Bookings.
<?php // do not copy this line
/**
* Will change the minutes it takes an In Cart booking to expire.
* This example reduces the number from 60 to 30.
*
* @param int $minutes 60 is the default passed
* @return int The amount of minutes you'd like to have In Cart bookings expire on.
*/
function change_incart_bookings_expiry_minutes_20170825( $minutes ) {
@jessepearson
jessepearson / adding_new_webhook_topics.php
Last active February 10, 2025 22:56
How to add a new custom Webhook topic in WooCommerce, with example of order filtering.
<?php // do not copy this line
/**
* add_new_topic_hooks will add a new webhook topic hook.
* @param array $topic_hooks Esxisting topic hooks.
*/
function add_new_topic_hooks( $topic_hooks ) {
// Array that has the topic as resource.event with arrays of actions that call that topic.
@jfarsen
jfarsen / functions.php
Created August 9, 2017 01:37
Remove Divi shortcodes from content
<?php
/**
* Remove Divi shortcodes from content
*
* @see http://victorfont.com/remove-divi-shortcodes-changing-themes/
*/
function remove_divi_shortcodes( $content ) {
$content = preg_replace('/\[\/?et_pb.*?\]/', '', $content);
return $content;
@vxhviet
vxhviet / regex.md
Created August 8, 2017 04:03
Regular Expression to find a string included between two characters while EXCLUDING the delimiters

Source: StackOverflow

Question: Regular Expression to find a string included between two characters while EXCLUDING the delimiters

Answer:

Easy done:

(?<=\[)(.*?)(?=\])