If you’ve ever had to dig down into specific columns of a spreadsheet with > Z columns you probably know how annoying it is to try to convert something like ‘AQ’ to the appropriate index (e.g. row[42]). Here’s a quick one-liner to convert a spreadsheet letter-based column to the equivalent zero-based index.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Over a year ago I was checking out WordPress’ new JavaScript media uploader interface and I had the idea to try to get it working on the front-end. After playing around with it I managed to get something working and thought, “I should write a blog post about that,” and promptly forgot all about it. Since I’m at Loopconf at the moment and feeling inspired I guess it’s finally time 🙂
You need to decide how you want to handle how it’ll work for users that aren’t logged in.
You’ll also have to decide what to do about users without ‘upload_files’ permissions.
That said, let’s take a look!
The Plugin
The media interface is all JavaScript so the plugin mostly serves as a way to bootstrap the js libraries we’ll need. I also included a simple shortcode that spits out a button that summons the interface and replaces the button with the selected image. Otherwise, there are three important things happening:
I’m calling wp_enqueue_media() to load the media interface libraries.
I’m filtering ajax_query_attachments_args in filter_media() to retrieve only the images belonging to the user. If you don’t care about restricting the images just remove the filter.
I’m checking current_user_can( 'upload_files' ) in the shortcode to restrict access to the interface, you’ll need a similar check somewhere in your own code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This is the meat of what you’ll need to use the media interface on the front-end. It’s actually pretty simple to summon the interface and then do something with the file that was retrieved. It’s all handled in these three easy steps:
Create the file_frame object using wp.media().
Attach a 'select' listener to the file_frame object to handle what you want to do when a file is selected. The attachment variable will have an object containing all the metadata for the selected item. Just do a console.log() to figure out what you’re getting.
Tell the file_frame to pop open the interface based on a click (or whatever) on an element somewhere.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
To get this new blog started I’m poaching an old post I made about scrambling words while retaining legibility. In the time since I originally posted this I found the source.
I was recently reading the Lucky Basartd page on the Stone Brewery website and I remembered an (unverified) study claiming “scrambled words are legible as long as first and last letters are in place.” For grins, I whipped up a Python script to scramble a word/sentence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters