Skip to content

Instantly share code, notes, and snippets.

View andrejcremoznik's full-sized avatar

Andrej Cremoznik andrejcremoznik

View GitHub Profile
@andrejcremoznik
andrejcremoznik / fragments.md
Created April 14, 2021 13:08
What are WooCommerce add to cart fragments? (to the point tl;dr Woo ajax tutorial)

WooCommerce has an option to run various UI actions via ajax.

If you are developing a theme and want to update your own elements during Woo ajax calls you will want to add the following hook:

add_filter('woocommerce_add_to_cart_fragments', 'my_woocommerce_add_to_cart_fragments');

function my_woocommerce_add_to_cart_fragments($fragments) {
  $fragments['#my-dom-element'] = '<div id="my-dom-element">Some new markup with some computed value.</div>';
 return $fragments;
@andrejcremoznik
andrejcremoznik / diffdir.sh
Last active June 29, 2020 20:09
Diff files between 2 directories, copy new files from src to dest, then check if existing files in dest have bigger files in src and copy those over too.
#!/bin/sh
#
# Copy files from one directory to another;
# - if the same file doesn't already exist in destination directory
# - if the file already exists, compare size
# - - if over 1 GB bigger, override existing file
# - - if bigger but less than 1 GB, write a log for manual review
#
# I need this to copy video files from multiple different folders to one large archive.
@andrejcremoznik
andrejcremoznik / intro.md
Last active December 22, 2018 23:11
Run WordPress cron in a real cronjob

Disable WordPress built-in cron because it's unreliable and suboptimal.

  1. In wp-config.php set: define('DISABLE_WP_CRON', true);
  2. On server in $HOME create the following:
    ~/backups/         # directory to store DB backups in
    ~/bin/wp           # [WP-CLI](https://wp-cli.org/), make it executable
    ~/bin/wpbackup     # see wpbackup.sh below, make it executable
    ~/bin/wpcron10min  # see wpcron10min.sh below, make it executable
    
@andrejcremoznik
andrejcremoznik / Instructions (self-signed SSL certificates).md
Last active March 10, 2019 18:30
Move your local development to HTTPS easily with self signed SSL certificates.

You need to create a personal Certificate Authority and install this CA into your system and browsers. Then you can create any number of self signed certificates for any domain you want.

  1. Create your CA (write down the passphrase)

    openssl genrsa -des3 -out myCA.key 2048
    openssl req -x509 -new -nodes -key myCA.key -sha256 -days 1825 -out myCA.pem
    
  2. Install your CA system-wide (this is for Arch linux)

@andrejcremoznik
andrejcremoznik / form.html
Last active October 9, 2018 10:23
Create a MailChimp subscribe form in WordPress
<!--
NOTE:
- this is minimal markup
- you can add elements and classes however you want
- if you change existing classes, match them in the config in the javascript
-->
<form class="mailchimp"><!-- NOTE: error, success and loading classes will be added here -->
<input class="mailchimp__input" type="email" placeholder="Your e-mail" required>
<button class="mailchimp__btn" type="submit">Subscribe</button>
<div class="mailchimp__feedback"></div>
@andrejcremoznik
andrejcremoznik / file-perms.sh
Created December 12, 2017 14:12
Blog post snippets - WordPress Security
# Assuming that:
# - the normal user on the server is `john` and he's the only member of the group `john`
# - the webroot is in `/srv/http/mywebsite.com`
# Recursively set user and group ownership of everything to john
chown john:john /srv/http/mywebsite.com -R
# Just in case, recursively remove write permission for group and others from everything
chmod go-w /srv/http/mywebsite.com -R
# Recursively set ownership of uploads to user john and group www-data
chown john:www-data /srv/http/mywebsite.com/wp-content/uploads -R
@andrejcremoznik
andrejcremoznik / domain.tld.conf
Last active January 16, 2024 13:59
Production Nginx config (HTTPS + rate limiting + PHP)
# Server directives (place in /etc/nginx/sites-enabled/domain.tld.conf)
# http://domain.tld --> https://domain.tld
# http://www.domain.tld --> https://domain.tld
server {
listen 80;
listen [::]:80;
server_name domain.tld www.domain.tld;
return 301 https://domain.tld$request_uri;
}

Keybase proof

I hereby claim:

  • I am andrejcremoznik on github.
  • I am andrejcremoznik (https://keybase.io/andrejcremoznik) on keybase.
  • I have a public key whose fingerprint is 8A85 9833 62B8 ECC7 0E0B B9CF A443 664E 5978 0117

To claim this, I am signing this object:

@andrejcremoznik
andrejcremoznik / k780.md
Last active June 13, 2024 14:04
Logitech K780 switch media and function keys on linux

Media/function keys on K780

K780 doesn't have a hard switch to lock the function keys. Logitech provides a utility to do this on Windows and iOS but not on Linux. You need to manually remap the keys.

Below works for Arch Linux, other systemd based distros should be about the same.

There's a problem with the F1-F3 keys as they're hardware specific and don't emit an event if pressed on their own and therefore can't be remapped. I might be wrong as I haven't spend any time on researching that.

Edit: about htat, see @tangruize comments below https://gist.github.com/andrejcremoznik/e56234138305226abd41fe4d1d2561a3#gistcomment-3390489

@andrejcremoznik
andrejcremoznik / add-scripts-to-PATH.md
Last active July 12, 2018 10:38
Instructions on how to set up various webdev tools in a bash shell

Make custom executables available as global bash commands

If you want custom Bash, NodeJS, PHP or any other script available as global bash commands you need to add them to the $PATH environment variable.

Global access to composer and WP-CLI

  1. Create a new directory in your user's home directory that will contain global apps
  2. Install those apps to that directory and make them executable by the current user
  3. Add the directory to system's $PATH