Skip to content

Instantly share code, notes, and snippets.

View stefanmm's full-sized avatar
🏠
Working from home

Stefan stefanmm

🏠
Working from home
View GitHub Profile
@stefanmm
stefanmm / usb_reset.sh
Last active June 28, 2024 13:33 — forked from erwin/turn-off-device.sh
Fix: USB keyboard not working in Linux after sleep/suspend
#!/bin/bash
# Thanks to:
# https://gist.github.com/erwin/a66a4c3f8a5d940ab6c434b48568ff39
# https://bbs.archlinux.org/viewtopic.php?pid=1956897#p1956897
# HOW TO:
# 1) Copy this file to /usr/local/bin/usb_reset.sh
# 2) make it executable: sudo chmod +x /usr/local/bin/usb_reset.sh
# 3) Create new service: sudo nano /etc/systemd/system/usb_reset.service
@stefanmm
stefanmm / functions.php
Created December 10, 2023 00:05
Fix "Does not use passive listeners to improve scrolling performance" for WP comment-reply.min.js
function sm_fix_comment_reply_passive_listeners(){
// deregister inside wp_footer (and not inside init) in case other plugins try to register comment-reply after init
wp_deregister_script( 'comment-reply' );
// Do not load JS if not needed
if ( is_single() && comments_open() ) {
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
// Function checks if a given script is already loaded
@stefanmm
stefanmm / wp-auto-open-accordion.js
Created August 8, 2023 23:12
WP 6.3 - Open "Details" block automatically on page load based on URL hash
// First, set a class name inside the "Details" accordion block (because we do not have ability to set the ID)
// If multiple elements with the same class name are detected, the first one will be triggered
// Append that class name after the page URL, like so: site.domain/page/#classname
if(window.location.hash) {
var hash = window.location.hash;
var el = document.getElementsByClassName(hash.slice(1))[0];
if(!el.hasAttribute('open')){
el.setAttribute('open', '');
@stefanmm
stefanmm / functions.php
Created December 24, 2022 11:44
Forbid Woo checkouts outside of a specific country (using Cloudflare)
<?php
// First read: https://support.cloudflare.com/hc/en-us/articles/200168236-Configuring-Cloudflare-IP-Geolocation
function woo_validate_country( $fields, $errors ){
$countryCode = "RS"; // List of codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
if( isset($_SERVER["HTTP_CF_IPCOUNTRY"]) && $_SERVER["HTTP_CF_IPCOUNTRY"] != "" ){
if( $_SERVER["HTTP_CF_IPCOUNTRY"] != $countryCode && !is_user_logged_in() ){ // ...but allow logged-in users to make orders
$errors->add( 'validation', 'Error...' ); // <-- Your error message here
}
}
@stefanmm
stefanmm / functions.php
Created September 25, 2022 10:06
WP enqueue scripts, ignore caching
<?php
// Enqueue JS and CSS
function enqueue_scripts_44587912556() {
$js_ver_stamp = filemtime( plugin_dir_path( __FILE__ ) . 'js/scripts.js' ); // Get timestamp of the file
$css_ver_stamp = filemtime( plugin_dir_path( __FILE__ ) . 'css/styles.js' ); // Get timestamp of the file
wp_enqueue_script('my_scripts', plugins_url( 'js/scripts.js', __FILE__ ), array(), $js_ver_stamp); // Append timestamp of the file
wp_register_style('my_styles', plugins_url( 'css/styles.css', __FILE__ ), false, $css_ver_stamp); // Append timestamp of the file
wp_enqueue_style ('my_styles');
@stefanmm
stefanmm / functions.php
Created July 24, 2022 22:51
Get Gravity Forms entries by field value
<?php
/* https://docs.gravityforms.com/searching-and-getting-entries-with-the-gfapi/
* $form_id: ID of the GF form. Default is 0 which means "All" (int)
* $field_id: ID of the field inside the GF $form_id (string)
* $field_val: value you want to search (string)
* $operator: =, IS, CONTAINS, IS NOT, ISNOT, <>, LIKE, NOT IN, NOTIN, IN (string)
* $limit: number of results to return (int)
*/
function gf_get_entries_by_field( $form_id = 0, $field_id = "", $field_val = "", $operator = "=", $limit = 10 ){
if ( !class_exists('GFAPI') ) { return; } // bail early if GF class doesn't exist
@stefanmm
stefanmm / woo-get-list-of-countries.php
Created June 6, 2022 15:08
Get list of Woo countries using Woo API
<?php
// Ref: https://woocommerce.github.io/code-reference/classes/WC-Countries.html#method___get
global $woocommerce;
$countries = new WC_Countries();
$countries = $countries->__get('countries'); // obj
// echo "<pre>".var_export($countries,true)."</pre>";
?>
@stefanmm
stefanmm / functions.php
Created May 20, 2022 20:12
Simple honeypot for WP comments
<?php
// We will add a fake input field to the WP comment form and make it invisible for humans
// The "firstname" field name is not used by WP (by default) so we can use it
function simple_honey_pot_44587513(){
ob_start();
?>
<style>
.rpodum {
top: 0;
left: 0;
@stefanmm
stefanmm / wp-config.php
Created May 17, 2022 21:32
Set WP URL wp-config
// Other code...
define( 'WP_HOME', 'http://yoursiteurl.com' );
define( 'WP_SITEURL', 'http://yoursiteurl.com' );
@stefanmm
stefanmm / functions.php
Created May 17, 2022 12:49
ACFBS - search by ACF fields only
function stfn_acfbs_post_fields( $fields ) {
// default lista: ['post_title', 'post_content', 'post_excerpt']
$fields = [];
return $fields;
}
add_filter( 'acfbs_search_post_object_fields', 'stfn_acfbs_post_fields', 10, 1 );