Skip to content

Instantly share code, notes, and snippets.

@esedic
esedic / git.txt
Last active January 22, 2024 09:26
Git clone repo at certain commit
Step 1: clone the main repo:
git clone sample.com/component.git
Get inside the cloned repo cd folder_name. Now you have a working directory with latest version pulled
and you want to move back to a specific commit id, here's how you can do it:
Step 2:
git reset --hard 9a36b9e79tbb9132c7 # (takes you back to that commit)
Step 3:
git clean -df # (cleans any untracked files/folders)
@esedic
esedic / url.js
Created January 17, 2024 15:14
Remove URL parameters and reload page
function removeParamAndRefresh(paramToRemove) {
const url = new URL(window.location.href);
if (url.searchParams.has(paramToRemove)) {
url.searchParams.delete(paramToRemove);
// Update the URL in the address bar without reloading
window.history.pushState({}, '', url);
// Now, reload the page
window.location.reload();
}
}
// VALUE UNIT STRIPPING AND CONVERSION FUNCTION TOOLS
// By Nicolas Giethlen (aka Chmood)
// VARIABLES
$browser-fontsize-base: 16 !default; // Default browser font-size in px (unitless)
$pixels-per-rem: 10 !default; // Root font-size in px (unitless)
// FUNCTIONS
@esedic
esedic / extractHexBackgroundColorFromElement.js
Created December 5, 2023 11:45
Extract computed background color hex value from given element object
var el = document.getElementById('target');
function extractHexBackgroundColorFromElement(element) {
return getComputedStyle(element)['background-color'].split('(')[1].split(')')[0].split(',').map((x) => x.trim()).map((x) => { x = parseInt(x).toString(16); return (x.length === 1) ? '0' + x : x }).join('');
}
console.log(extractHexBackgroundColorFromElement(el));
@esedic
esedic / em2px.js
Created December 5, 2023 11:42
Calculate em to pixel value with JavaScript
function em2px(em) {
return Number(em) * Number(window.getComputedStyle(document.body).getPropertyValue('font-size').match(/\d+/)[0]);
}
console.log(em2px(2));
@esedic
esedic / bots.php
Created November 20, 2023 23:34
Prevent PHP execution if USER_AGENT is bot
<?php
// While not foolproof, you can check the USER_AGENT string and only run that code if 'bot' does not exist.
if(stripos($_SERVER['HTTP_USER_AGENT'],'bot') === false){ }
// This would stop any bot that actually has bot in the user agent string that also does not pay attention to robots.txt.
// Source: https://stackoverflow.com/a/17600047/1090042
@esedic
esedic / wp_comments.php
Created November 2, 2023 10:20
Disable WordPress comments
<?php
/***********************************
*
* ! DISABLE COMMENTS
*
* *********************************/
function df_disable_comments_post_types_support()
{
@esedic
esedic / wpsvg.php
Created November 2, 2023 10:17
Add SVG support to WordPress
<?php
class WPSVG{
function __construct(){
add_action( 'admin_init', array( $this, 'add_svg_support' ) );
add_action( 'admin_footer', array( $this, 'fix_svg_thumbnail_size' ) );
add_filter( 'upload_mimes', array( $this, 'add_svg_mime' ) );
add_filter( 'wp_check_filetype_and_ext', array( $this, 'wp_check_filetype_and_ext' ), 100, 4 );
add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_generate_attachment_metadata' ), 10, 2 );
add_filter( 'fl_module_upload_regex', array( $this, 'fl_module_upload_regex' ), 10, 4 );
@esedic
esedic / wpdata.php
Last active October 30, 2023 13:51
Access WordPress plugin data in theme template file
<?php
// ChatGPT generated answer
// To access data from a WordPress plugin PHP class in a page template file, you need to follow a few steps:
/* Step 1: Instantiate the Plugin Class in Your Template File
In your WordPress page template file (usually named page-{template_name}.php), you need to instantiate the plugin class. First, make sure the plugin is active. Then, instantiate the class using the global keyword.*/
global $your_plugin_class_instance;
$your_plugin_class_instance = new Your_Plugin_Class();
// Replace Your_Plugin_Class with the actual name of your plugin class.
@esedic
esedic / gist:ca79b1338908261bdcb085eb240a8069
Created October 23, 2023 15:33 — forked from spivurno/gist:3708752
Gravity Wiz // Limit How Many Checkboxes Can Be Checked
<?php
/**
* Limit How Many Checkboxes Can Be Checked
* http://gravitywiz.com/2012/06/11/limiting-how-many-checkboxes-can-be-checked/
*/
class GFLimitCheckboxes {
public static $field_limits;