#development

WordPress ShortCode API – How to make a function echo and not break

According to my previous understanding and WordPress’s documentation:

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results

So, we’re limtied, shortcode functions must return, not echo. So:

https://gist.github.com/davidsword/394108430a76c36aa9cefb6c809dc187

Has been my understanding for years. Unless…

https://gist.github.com/davidsword/1a590c2a11bb55531ddfc98eb83afee3

Yeah. Yeah. That’s right! You can use output buffering to collect your echo’ed HTML from printing, and return it instead.

This is good to use when a shortcode has dozens of lines some multi-language script. For example I’m working on a custom Google Map with Javascript, it’s 200 lines, called to via [map] and has lots of PHP, HTML, some CSS, and WordPress conditional statements weaved into it. It’s a nightmare putting that much Javascript and multi-language markup into a PHP string, escaping quotes alone is a pile of trouble. What’s worse is the syntax highlighting from the IDE is lost, so any bug fixes or changes, you loose a great assistant. I know there’s other solutions, I could build a class, or I could do a file_get_contents() and reduce my PHP to being at the top of the script, or similar, but when the scripts not too long, it’s just so nice to have raw HTML inside the function, instead of dealing with everything else.


Update: Another great use for this is if you want to include() a local file’s echo‘ed results into a variable. If you use get_file_contents() you’ll receive the unexecuted code, storing executed code in an object allows you to return it into a variable. For the following example /file.php is just a simple file with echo "Rock the boat";

https://gist.github.com/davidsword/29aa612b73266c72b79e41bddbe01174

With the above, $files_output will be Rock the boat. Neat eh!

Fix for Finder

It may have to do with having Duet installed, but macOS Sierra’s Finder, Expose, and Dock – along with custom mac OS keyboard shortcuts, have been failing a bit more regularily than I’m willing to tolerate.

The fix is simple enough, just restarting them, but I have to dig through my notes or Google and find the commands I’m looking for, which interupts my workflow even more. So I made a single word Terminal alias that fixes prettymuch any blow up any of those services experience:

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

I added that too my bash profile via vi ~/.bash_profile and reload it with source ~/.bash_profile So now, any problem I have, just pop to Terminal (which is open all day anyways) and > fix the problem.

Simple RSA Passwordless SSH Into Unix Server via OSX

Passwordless SSH is something I use daily, but it was one of those tasks I never invested time into understanding. A one-off task: follow tutorials and forget. However, it seems every few years something changes and I have to redo my RSA keys on my Mac andor my servers.

This year, after an upgrade to Mac OS X Sierra, marks the fourth time I’ve had to temporarily learn how to set RSA Keys up again, and I’ve decided to actually invest in it this time in learning so the firth time, I can do it without reference and tons of reading.

There are many better written articles on how to do this, however, I personally found the majority of them difficult to understand or follow – so I’ve tried to make this as simple as possible.

For this tutorials “Remote” is Bash via Ubuntu 16 on EC2 AWS service, and “Local” is  Terminal on OS X Sierra.


Remote

  1. Create a key pair, where keyname is your servers name

https://gist.github.com/davidsword/62a7cffaf0b2b8574e17a58d379a90a1

  1. Copy the contents of newly created file ~/.ssh/keyname for next step

Local

  1. Create and paste contents from the above into this new .pem file (close and save file). Then add the .pem to OSX

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

That’s it. Really.


You can now login passwordless:

https://gist.github.com/davidsword/74edf98468bc4b7b0fcf21ac58326c10

I have three optional extra’s that I personally do that make it a litter better. First, name your keypair appropriately, most tutorials all have idrsa or idusername and if you’ve got or will get multiple servers, your local ~/.ssh folder will be a pile of unknown randomness.

I also suggest setting AWS security to allow SSH only on a random port, like 42111 or something like that, prevent unwanted SSH attempts by limiting where your door is.

Lastly, adding an alias to your local bash for the SSH connection command with a single word is a great second saver. Heres how to do it via Terminal on your Local:

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

Random Image From Select Flickr Groups via PHP

Yesterday I wrote about Cache’ing CURL requests, today I had to write a small script that uses it for a web app I’m working on. It uses serialized PHP data instead of JSON but everything’s pretty much the same.

The app I’m creating needed a dynamic background image, something professional, new, and interesting. I wanted a pool of images (so each request has a new image) but I didn’t want the pool to be too big. I also wanted the pool of images (Flickr Groups) to change on once in a while, so it wasn’t all too similar (landscapes, or all portraits, etc.).

The script below outputs a random image from a stream of 20 images from a Flickr Group (though it can be set to output just one image per $refresh). The Flickr Group is random too and comes from a predefined array of Groups I selected.

https://gist.github.com/davidsword/7c32c7d2d77928eeac9ecf629167b7fd

The above will output

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

Where {group_name} will change every $refresh seconds, and {image_url} will be 1 of 20 images on rotation every request and change with the $refresh group change.

To get the Groups ID’s I used idgettr.com and I got the Flickr API Key $flickrapi from Flickr API.

Really Simple PHP CURL and Cache of JSON Result

When using PHP CURL with a third party API, I’ve found keeping the JSON results in a local cache is easier than constantly querying the service – especially when the API is slow or has query restrictions.

Here’s a great snippet I save in my tool belt and use quite often:

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

If the API is really slow (I’ve encountered two myself) I suggest splitting the snippets CURL+Cache section and putting it into it’s own file, from there create a CRON command to execute the independent CURL+Cache file. That’ll separate the slow API from your PHP app, and your JSON data will still be up to date in the background.

Uses

I love reading these. I’ll make my own.

Apps

  • Panic’s Coda 2 is my code editor of choice, I think the people there in Oregon are making great software and have made a great ecosystem with Panic Sync. It’s a great IDE for quickly working remote, easily tap into SVN, a MySQL GUI, reference books, and preview browser.
  • Slack for work related discussions and assignments, almost no email now
  • Notes.app & Reminders.app are surprisingly my choice for personal management. I really tried so many different apps, and apreshiated them, but after Apple removed the skeuomorphic designs and added fonts to notes, with Siri and the entire Apple ecosystem, it seemed the easiest choice for workflow
  • Adobe Creative Cloud (Photoshop + Lightroom) for designing
  • Google Drive for client files and shared resources

Desktop

  • Docked Retina MacBook Pro 15″ (Early 2013 2.7 / 16 / 256)
  • 22” LG IPS Monitor (22mp67hq)
  • iPad 3 Retina mounted to monitor with a modified Twevlve South HoverBar a propriatry dashboard all day long, similar to Status Board (but better). Use Duet Display a few times a season when working on somthing large or complex and need the reference realestate
  • Kindle Paperwhite (3rd generation | Mid 2015) one of my most favourite peices of hardware. E-ink blows my mind every time I pick it up
  • HeadPhones Monster Inspiration Overear Noise Cancellation
  • Desktop Speaker Sony SRS-X33 I never intended for these to be my speakers, but it works and works well.

Website

  • Powered by WordPress
  • Plugins Jetpack, Yoast SEO, Monster Insights, AMP, Broken Link Checker, Login LockDown, oEmbed Gist
  • Theme is a custom one I made for myself for my exact needs
  • GoodReads is a custom shortcode – I will release it as a plugin eventually

Emoji short name’s to HTML HEX Code via PHP

I’ve been working on a Slack App that shows recent Slack posts and activities. The majority of recent posts include emojis, and problematically Slack’s API doesn’t store and send emojis as characters, they’re down-converted to their respective shortnames, then wrapped in colons (presumably for universal support – it appears to be a standard practice of some sort, as many use it).

Basically I was just looking for converting

:thumbsup: to convert to: 👍 which’d show: 👍

Simple enough task, but I couldn’t find any resources to do it (and if you’ve discovered this blog post, you’ve probably discovered that out too). So, starting at the top, Slack in their API docs refers to using github.com/iamcal/emoji-data as their emoji shortcode source. This repository does have a PHP script, but it doesn’t convert as simply as I wanted it to, and it’s huge.

Taking a step back, iamcal/emoji-data has a enormous master .json file, basically a database of each emoji and all variations therein. Taking this file, and running through it with some basic PHP, we can build a simple array of shortcode => hexcode. Doing this was almost too easy:

https://gist.github.com/davidsword/74ed32b5e6bf475ce847b5da88d5d89b

We then take the output of that file, and create our own emoji_unicode.php file, an array housing the values output above. Which looks like:

https://gist.github.com/davidsword/40de8686bb693ad0e4b742d1df32aad2

Much smaller and easier to work with. 97% smaller actually coming in at: 35KB instead of 928KB. I’ve hosted the file on github here.

So with a simple array to call back to, we can just include it, search the string for the colon’ized shortcode emojis and replace with hex code, like so:

https://gist.github.com/davidsword/657de8877eb3bff3f5fc31e2a439c134

Which’d output

> hello 🌎 . it’s 🔨 ⏱.

This works with :skin-tone-x which surprised me- I guess the skin-tone is a “invisible” character of it’s own, that basically gets compounded with the emoji character before it, and changes the emojis that way. Pretty cool, so no extra work for that. (And no, this method doesn’t work on devices that don’t support emojis).

Kindle Currently Reading on Panic Status Board or Website

Kindle Currently Reading on Panic Status Board or Website

Here’s a neat little widget that’ll fetch your currently-reading and read books from your Goodreads shelf, and show them as a widget for either Panic Status Board (DIY Panel), or your website. Code is inspired and built upon from a great little 2013 script from flapane.com

First, some styling:

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

Setup, query and cache:

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

Read and output:

https://gist.github.com/davidsword/6920a66855e522da8f3af3d00a671406

You’ll need to create empty file goodreads.cache manually, in the dir of the script.

I’m not entirely sure what happens when there’s more than 1 currently-reading. I never bothered checking.

$image_fix is nasty, I don’t understand why the Goodreads user API would not return a cover when the cover is present on the Goodreads site itself. I had to do a fix for one of my books, manually tracking down the cover’s URL on Goodreads.com.

<!--?= (98/147)*54 ?--> is my ..interesting.. way of doing proportional scaling on the fly. Old width/height values kept prior to overwrites for reference; makes sizing your widgets books covers to much easier.

filter: grayscale(100%); Keepin’ it real with Kindle, grayscale the covers.