Blog

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.

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:

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.

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.

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:

Has been my understanding for years. Unless…

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.

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:

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
  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

That’s it. Really.


You can now login passwordless:

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:

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.

The above will output

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:

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.