Use LESSCSS Client Side While Developing WordPress Theme

I write in LESSCSS because I find it’s nesting so much eaiser to read. While creating a theme, I never compile during, it’s too annoying, instead I have a on/off toggle for using .LESS file or .CSS file.

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

In the snippet I made the toggle WP_DEBUG, which should be on while you develop, but if you’re developing in a network it’s eaiser to use a global veriable from functions.php

Furthermore if you are using LESSCSS and are developing/maintaining a lot of WordPress themes, I highly suggest making a shortcut for LESSCSS in bash:

https://gist.github.com/davidsword/71e290a3afd296e841bca3f33099d5e2

So all you have to do is type less when you’re in the theme directory. If you’re not comfortable with terminal, I’ve written about a PHP LESSCSS Compiler.

PreLoad Images With jQuery

Another incredible snippet if you’ve got a JS app that changes images and want said images to be ready and loaded, but not overwhelm the normal page load.

https://gist.github.com/davidsword/765532bd4b0bf81b74c9c81060a01fa5

Remove Select POST FORMATS from Child Theme

The documentation left this out, but if you want to remove some post formats from a WordPress child theme, the remove_theme_support( 'post-formats') does not allow you to pass an array of formats, it’s just a hard switch. However you can over-write the original add_theme_support('post-formats') with only formats you desire.

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

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.

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