Convert Alfred snippets (.json) to Espanso matches (.yaml)

Moving to Linux this month (Pop_OS to be specific), the biggest change was leaving Alfred behind. While almost every other tool or app had a nearly exact replacement, I could not find any launchers on Linux that had the huge number of power user features Alfred has. However, I found lots of amazing small apps that do just one feature and do it well.

One of those apps was Espanso, a light weight CLI focused text expander. Using Alfred on macOS, the global snippets where a big part of my workflow. After a bit of reading, on Linux I stumbled on Espanso which seems like one of the best text expanders available. A quick browse of the well written and maintained documentation and it’s clear why.

The UX for the actual text expanding was the exact same, however the setup was a bit different between the two apps. There’s no simple way to just import Alfred snippets into Espanso, so a bit of data massaging was required.

To do this I first exported Alfred snippets (either by browsing Alfreds preferences folder, or Exporting the snippets manually in Alfred > Features > Snippets > Export), I then jotted up and ran this script to convert Alfreds method of storing the snippets in .json to Espanso’s .yaml format, & tweaking some minor differences with cursor and clipboard variables:

A pretty simple difference and straight forward result. Then it was just copy & paste of the output to the bottom of ~/.config/espanso/default.yaml

If this doesn’t work and Espanso complains of errors, try opening default.yaml in VSCode or another editor and add YAML Syntax extension or similar. This should flag any syntax errors for manual resolving. In my case one or two regex patterns I have saved were just a bit to regex’y for my simple str_replace()’s.

All and all it wasn’t a heavy lift, & I managed to wrangle all 388 some snippets of mine over.

WordPress Pass PHP Variable and Values to JS Theme or Plugin

The beautiful function wp_localize_script() was built for l11n, however its found as a method for carrying over server-side values into client-side JS.

Your values are now available in your .js file:

console.dir(my_theme_data);

Currently using this in a React theme I’m building for WordPress.

This can also be done with something like:

Every Image URL Filter For WordPress, Front & Back End

I had a project where every single media library image URL needed to be filtered, backend and front end. As far as I’ve found these were the filters for every area:

The myplugin_filter_html_image_urls() function peals out images with regex from areas where it’s not just the URL being sent. This function may require verification of the current domain incase bad practices of using external images is being done.

WordPress Pass Variable Into wp_footer, wp_head, & all Other Actions/ Hooks/Filters

Passing variables into WordPress hooks using an anonymous function I thought was impossible. I always worked around this and rewrote the logic. Today I found out anonymous functions support a use keyword, allowing passing:

So simple and easy. Shaves hours of troubleshooting.

Switching PHP Versions Localhost, Keep phpMyAdmin Working

When switching localhost php versions for testing, nothing interrupts the workflow more than phpMyAdmin failing because you’re on php 5.4.

The single line of code, to be your index.php in /phpmyadmin/ simply routes you to either the phpmyadmin/new/ (v4.7 – supports php 5.5-7.2) and phpmyadmin/old/ (4.0 – supporting php 5.2 – 5.4):

Simple.

On Img Upload, Create New Post, Set as Featured Img

I used to manually do everything for the Images section of my site. Adding an image would involve: save photo after editing, renaming it, uploading, creating new post, setting the title and date, assigning the featured image as the upload, then setting the category to Images so it’s added to the page. So, 8 steps, usually done over-and-over again to a dozen or so images in a batch. We’re talking over 96 steps to get a batch of images online – it took a bit of time, and was slightly demotivating, becoming a chore instead of a hobby.

Able to scratch my own itch – I came up with a way to automate everything. Instead of 96 steps, I now publish an entire batch of images in just 2 simple steps. Yes Two. Too. To. You read correctly, just 2! How’d I do it?

After editing the photos in Lightroom, I use Lightroom’s file-rename feature during bulk export that automatically sets the filename to the images meta timestamp, so YYYYMMDD.jpg.

I then use the following script tapping into the add_attachment hook during bulk upload that does the other 7 steps for each image. Everything in just 1 step, for the entire bulk.

https://gist.github.com/davidsword.ca4a6ec6cde878a8a47fde35feecab69

So, all I have to do after exporting from Lightroom, is drag-and-drop upload to the site.

WordPress Dynamically Write Number of Years Since With Shortcode

As the lengthy title says, the follow simple script allows you to use a shortcode to dynamically output the number of years since a specified date. I wrote this for my own bio, which I used to have to update the number of years since the start of my employment once a year. The simple’r solution would be to write, “blah blah blah since 2010” instead of “blah blah blah for 7 years“. But hey, why not.

Simple usage is []years since="2010-07-01"[] would output something like 7.

WordPress Display MarkDown ReadMe File With Shortcode

For demo/home sites of a theme/plugin that wish to include the projects main readme files as part of the website, this quick snippet loads the folders README.md file, and parses the MarkDown (to githubs flavour) with a awesome jQuery plugin, all with the simple call of a [readme] shortcode.

Add All Super Admins To All Sites On WordPress

One of the most annoying features of WordPress Multisites (besides domain mapping) is adding a Super Admin a few years after your network is established and grown. Sure, being a Super Admin allows said user to access any site on the network, but the user is technically not added to the site – so the Super Admins users My Sites tab will only have sites that s/he has been manually added to. The user is then forced to jump between sites with a headache of manually typing.

The follow script solves the problem, it runs through all super admins and adds them to all sites in the network they’re not yet added to.

The function needs to be run once to get everyone up to date. For the future it can be wrapped in a $_GET conditional to fire manually, a cronjob to run in background, or like I did, a transient, so that you can run it as often as you need. Mind you this isn’t a network plugin or intended to run on all sites, it just needs to run once on a single site. All the multisite data and blog_switching annoyance happens within the functions themselves.

Quickly And Easily Send To Slack via PHP Function

Building a PHP app that communicates with Slack a lot, I found it was much cleaner to tuck everything away in a function and call it in a single function that resembles the attributes of mail(). This makes for changing your apps icon andor color easy, as well as staying consistent with the Title and PreText.

Slack Incoming Hook required.