Skip to content

Instantly share code, notes, and snippets.

View erikyo's full-sized avatar

Erik Golinelli erikyo

View GitHub Profile
@erikyo
erikyo / wordpress_oembed_utils.php
Created February 16, 2022 10:15
A filter for Wordpress oembed Block. Will set Youtube url to "youtube-nocookie.com" and will disable misc related videos (rel=). Enables Vimeo subtitles automatically using the browser language.
<?php
function oembed_utils( $html ) {
// set the default youtube url to youtube-nocookie.com
$html = str_replace("www.youtube.com/embed","www.youtube-nocookie.com/embed", $html);
// remove youtube random related videos
$html = str_replace( "feature=oembed", "feature=oembed&rel=0", $html );
@erikyo
erikyo / scss_custom_prop.scss
Last active January 28, 2022 11:55
Generates HSL color schemes with SCSS/SASS dynamically
/*
* Erik Codekraft - 2022
*/
/// Output the value for the property
/// @param {number or string} $val - the base value
/// @param {number} $val2 - the value to sum
/// @output the result or the css string calc(val1 + val2)
@function set_value($val, $val2) {
@if (type-of($val) == string) {
@erikyo
erikyo / webp_uploads_filter_clone_image.php
Last active February 1, 2022 12:36
Save a copy webp copy of the original image then update post metadata
<?php
function webp_uploads_filter_image_copy( $image_meta, $attachment_id ) {
$file = wp_get_original_image_path( $attachment_id );
$image_mime = wp_getimagesize( $file )['mime'];
if ( 'image/jpeg' === $image_mime ) {
@erikyo
erikyo / wordpress_preload_featured_image.php
Last active July 10, 2024 10:24
Wordpress preload featured image (src and srcset)
@erikyo
erikyo / fast_minify_css_html.php
Created December 28, 2021 18:54
Fast Minify - a php fast and time-effective minification function for css and html files
<?php
// A php fast and time-effective minification function for css and html files
// I don't pretend to perform a complete minification but this will reduce the file size considerably
// and without consuming many resources.
function toKB( $bytes ) {
return number_format( $bytes / 1024, 2 );
}
function compress_notice( $raw, $compressed ) {
@erikyo
erikyo / wordpress_upload_webp_handler.php
Last active September 10, 2024 20:47
While uploading a media into WordPress media library this script automatically generate a webp copy of the uploaded media. Works with jpg, png and pdf previews, add the .webp extension after the original name
<?php
// README: you need GD installed
function myprefix_explode_filepath( $filepath, $attachment_id ) {
$uploads = wp_upload_dir();
if ( strpos( $filepath, "/" ) == false ) {
return array( $uploads['basedir'], "", $filepath );
} else {
list( $year, $month, $filename ) = explode( '/', $filepath );
@erikyo
erikyo / explode_accept_language.php
Last active September 12, 2021 11:34
Returns an array of 2 digit languages codes from the user $_SERVER['HTTP_ACCEPT_LANGUAGE']
<?php
// a modified version of https://stackoverflow.com/a/33748742/5735847
function get_accept_language_array( $languages ) {
return array_values(
array_reduce(
explode(',', str_replace(' ', '', $languages) ),
function ($res, $el) {
if (strlen($el) === 5) {
$l = explode('-', $el);
@erikyo
erikyo / dequeue_gutenberg_custom_var.php
Created August 31, 2021 12:03
Dequeue global wordpress style (dequeue custom vars)
<?php
/**
* Dequeue global wordpress style
*/
if ( ! function_exists( 'my_remove_global_style' ) ) :
function my_remove_global_style() {
wp_deregister_style( 'global-styles' );
wp_dequeue_style( 'global-styles' );
}
endif;
@erikyo
erikyo / cf7.scss
Created August 23, 2021 18:06
contact form 7 sass style
// the default palette
$wpcf7-white: #fff !default; // Blue
$wpcf7-light-gray: #505b66 !default; // Light Gray
$wpcf7-dark-gray: #23282d !default; // Dark Gray
$wpcf7-blue: #00a0d2 !default; // Blue
$wpcf7-green: #46b450 !default; // Green
$wpcf7-red: #dc3232 !default; // Red
$wpcf7-orange: #f56e28 !default; // Orange
$wpcf7-yellow: #ffb900 !default; // Yellow
@erikyo
erikyo / woocommerce_switch_tab_via_url.php
Last active August 11, 2021 19:03 — forked from corsonr/gist:0e0a4dfaacf59fdca314
WooCommerce: activate product tabs from URL (support with history states, share link to tab with hash)
<?php
/**
* wc_direct_link_to_product_tabs
*
* Allows you to create custom URLs to activate product tabs by default, directly from the URL.
* ex: http://mysite.com/my-product-name#reviews
* this fork add history.pushState and each tab will be treated as a page
* so you can use browser history back / forward to move along tabs
*/
add_action( 'wp_footer', function () {