Media Categories 1.6 Beta

I am happy to see that so many people have found this plugin useful. I’ve had many feature requests, a couple code contributions, and plenty of bug reports. I appreciate it all.

It’s been over a year since there was an update to the Media Categories plugin, so I’ve been hard at work this past week adding a ton of new features. I’ve taken to heart as many feature requests and bug reports as I could handle.

Heres a detailed list of what you can expect in this new and improved version of Media Categories:

Improved Category Metabox: Filter terms as you type

Edit_Media_‹_Local_Community_Dev_—_WordPressBack in WordPress 3.4 and earlier, there were no metaboxes for taxonomy on attachments, so I had to roll my own fake metaboxes (“metafauxes”, I’ve started calling them). With the big push in WordPress 3.5 to improve the media modal, the need for that metafaux on the main editor page went away – but with it went one of everyone’s favorite features of this plugin. The “Searchable Categories” field, the input box that let users filter the categories that were showing, making the metabox easier to use.

In Media Categories 1.6, I’ve created a new set of custom metaboxes that bring back this feature! You can now type the name of the term your interested in and the list of terms in the metabox will filter as you type.

Drop-downs Filters in the Media Library & Media Modal

One of the most frequent feature requests made for this plugin has been the ability to filter items in the Media Library by taxonomy, just like you can on the Posts admin list. We’ll I’ve done that, so now you’ll have a separate drop-down for each taxonomy assigned to work with Media Categories.

Somewhat less frequent of a feature request, but still a great idea, has been to have the same ability to filter by taxonomy, but from within the Media Modal. This would be useful for obvious reasons to anyone with a large collection of images – especially for folks who’s website might happen to focus around imagery.

Media Categories 1.6 will include both of these new drop-down filters.

Sortable Columns in Media Library

Media_Library_‹_Local_Community_Dev_—_WordPressSimple enhancement, but a frequent request. Click the header of the taxonomy columns to sort them by taxonomy. Not much to say about it except that getting it to work alongside the drop-down filters was pretty challenging.

 

 

Improved Media Modal “Metafauxes”

As I mentioned above, a metafaux is what I call a fake metabox –Edit_Post_‹_Local_Community_Dev_—_WordPress just a simplified version of the metabox meant to be used in places where the WordPress metabox framework doesn’t normally work. When WordPress 3.5 was released, it introduced the Media Modal/Media Manager. As a matter of chance, a bug was introduced. There was no way to distinguish between the media editor in sidebar of the Media Modal, and the main media editor in the Media Library. So my metafauxes were showing up in the modal, but they weren’t working.

I reported the bug, and they got it patched up quickly, but instead of taking out the metafauxes, I made them work – in the modal. However, they were kinda buggy.

I’ve fixed them up now, they look a little nicer, and they work a lot better – and just as before, they collapse to save space on the small modal sidebar, and you can filter as you type!

Gallery Shortcode Compatibility

To be honest, I should have known better. It was back in Media Categories 1.2 that I added the ability to select a taxonomy when using the “gallery” shortcode. This was very cool, and I think it became another big draw for people to use the plugin.

There was a terrible secret here though (thunder!), to make this work, I had to override the default WordPress shortcode (shreak!). This was bound to cause problems, and it did. I’ve received multiple reports of other plugins also interacting with the “gallery” shortcode, and when those plugins are on at the same time as this one – they don’t get along.

I figure most people dont have the conflicts (i think?) – so this plugin by default continues to modify the default “gallery” shortcode for the sake of not breaking people’s websites who already use it this way. However, it also creates a secondary shortcode called [media_gallery]

To stop the conflicts with other plugins, paste two line snippet of PHP into function.php file. This sets a property in the Media Categories object to false which stops my plugin from messing with the default gallery.

See the documentation for exact details on how this will work. I know its annoying that it requires code – but just you wait. An options page is coming.

Eventually.

Whats Next?

Bulk Editing – its the one feature request that I really wanted to get into this release, but it turned out to me more work than I had time for. I’ll try to make this feature a top priority on the next major release.

Options/Settings Page – I want to eventually eliminate the need for people to drop lines of code into functions.php to control how this plugin works. I want this settings page to simply let users pick from a list of all available taxonomies, and select which ones should be made to work with attachments.

Code Cleanup/Refactor – Given that the scope of this plugin has changed, I also want to reevaluate how it is written. It is currently a lot clunkier than I’d like it to be.

This plugin was originally meant to only be a tool for developers, to let them easily turn on metaboxes and associate taxonomies with attachments. It’s become more than that, and the users aren’t all developers anymore. That said, I’ll keep intact the control developers already have. I’ll probably even make it so the settings page can be completely disabled by defining some property in functions.php

Can’t wait? Wanna help?

If you’d like to beta test the plugin, please do so. Its available in the master branch of its GitHub repository here: Media Categories 1.6 Beta

If you have a bug to report, I always prefer that you do so on GitHub Issues, since its so much better than the WordPress.org forums.

 

 

 

 


Git Smarter: Retrieve deleted files from history [ git unremove ]

In the course of development, there are times you want to look at or retrieve a file deleted sometime in the past – and you probably don’t know when it was deleted. I’ve written a simple git alias that makes it super easy.


### Git Unremove ##
# Adds an alias called "unrm" to retrieve a pile or path deleted at any point in the repositories history.
# Usage:
# git unrm [path]
# git unrm path/to/file
git config –global alias.unrm '!COMMIT=$(git log -1 –pretty=%h — "$1"); git checkout $COMMIT^ — "$1"; echo "File: $1, was restored from commit $COMMIT"; git show -s $COMMIT'

view raw

git-unrm.sh

hosted with ❤ by GitHub

So how does this work? If you don’t care, then just copy and paste the config line above – if you want to know what’s going on here, read on.

It uses some simple features of the “git log” and “git checkout” commands that everyone is already familiar with. Each can have a file path passed to them after a couple of dashes “–“. First, you need to find out when in the git history the file was removed, for that you can just use “git log” with a “–” and a path.


# Show specifically only the last commit to affect the path/to/file –
# this will be the commit where the file as deleted.
git log -1 — path/to/file

This works just like normal git-log, but narrows the log to show only commits that affect the given path – keep in mind that the path can be a specific file or an entire directory.

What if you want to actually retrieve that file? Well, you can do that with the “git checkout” command by doing basically the same thing. First copy the SHA1 hash of the last commit affecting the file you want to retrieve – this is important because you it will tell git-checkout where to look for the file.


# Recreate the file at path/to/file in the state in the state it was at
# just prior to having been deleted (made up commit hash for example)
git checkout 4efa6b0^ — path/to/file

The commit hash you copied from the “git log” output doesn’t actually contain the file, its just when the file was deleted because that commit is by definition the last commit to affect the file. To get the last state of the file before its removal you need to look to the commit immediately before it, thats what the “^” does, it looks one commit back.

So that a bunch of stuff to remember – although its fairly simple compared to some other more complex tasks in git – but I made the git alias to make it even easier. I think I originally came up with this alias an an answer on StackOverflow, but its been sitting around as a gist on my github account for a while. I’ll probably be sharing more stuff from gist in the future.

 


VIM: Saving with sudo, without opening with sudo

It happens all the time. You open a file in VIM, you make three and a quarter trillion changes, and then realize you needed sudo but didn’t use it when you opened the file. What do you do?

Well, if you might save the file under a new name, then quit, delete the original and rename the new file with the original name. It works, but its really annoying.

Luckily there is a cool trick in VIM that lets you save with sudo, even after having opened a file without it. Here it is.

:w !sudo tee %

Hit enter after typing that in, you will be prompted for a password if necessary, and then you will then get the following message.

Press ENTER or type command to continue

Go ahead and hit enter, and assuming you actually made some changes, you will then get the following message.

Press ENTER or type command to continue
W12: Warning: File "sites-available/webgrind.conf" has changed and the buffer was changed in Vim as well
See ":help W12" for more info.
[O]K, (L)oad File:

This is just telling you there are two versions of the file, the old one and the new one and gives you an option to simply acknowledge, or to load the new version. You are typically going to want to enter “L”.

Thats it, your file should be saved now. Cool trick, saved me some frustration many time.

Note: I first heard of this trick at WordCamp, but I can’t remember who it was that mentioned it.


Wooden 3D Printed WordPress Logos #wcchi

It’s day 3 of WordCamp Chicago 2013. On day 1, the Foundation Friday, my hobby of 3D Printing came up in conversation and someone suggested that I print a WordPress logo. On day 2 I brought two 3D Printed logos, printed with Cherry Laywoo-d3, a material used for 3D printing made from repurposed sawdust mixed with a plolymer (pro tip: saying polymer in an meaningful context make you sound smart!).

BN-PN5jCAAAsG4K-1

During the after party I was showing the logo’s off. Matt Boynes (@senyob) tweeted a picture. A few people retweeted it and asked if they could have one. Today I come with a total of 6 logos to give away to anyone who is interested (although 3 are already committed to specific individuals).

20130630_090303

I wish I had thought of printing the logo sooner, I would have printed 30 of them and just handed them out. Maybe next year.

If you want one and live in the Chicago area, or even work in the loop – let me know and we can work something out.


WP_Query: Get Posts by Relative Date

Someone asked me today how to use WP_Query to get posts by a relative date. The codex provides an example on how to achieve this. Heres what is shown on the codex.


<?php /** NOTE: This is the example from the Codex, which think is BAD. **/
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days (hardcoded)
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
//Requires immediate additiona and removal of the filter for intended use.
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

I feel that its sloppy and not very flexible. Having to add the filter and remove it each time you want to do this is ugly. I solved this some time ago and had to dig through some old code to find it.


<?php
/**
* Allows WP_Query to use a 'since' argument to query for
* relative date queries.
*
* Usage: Query posts from last 30 days
* $query = new WP_Query('since' => '-30 days');
*
* @uses strtotime()
* @author Eddie Moya
**/
function filter_where_add_since( $where = '', $query) {
if( isset($query->query_vars['since']) ){
$where .= " AND post_date > '" . date('Y-m-d', strtotime($query->query_vars['since'])) . "'";
}
return $where;
}
add_filter( 'posts_where', 'filter_where_add_since', 10, 2 );

Ultimately what I have done is the same solution, using the same filter, and utilizing strtotime() to get the relative date. However  i’ve wrapped it into a simple “since” parameter that can be used on any query. Just a little nicer and simpler to use.

This technique can easily be adapted to other custom parameters which result in modifications to the where or join filters.


Better Git: Interact With a Branch, Without Checking it Out.

Tricks for interacting with a branch, without checking it out.

Browse a directory (like ls):
Syntax: git show [ref]:[path]

> git show master:your/path/

See contents of a file (command as above):
Syntax: git show [ref]:[filepath]

> git show master:your/path/file.php

Checkout a specific file or directory from a different branch:
Syntax: git checkout [ref] -- [path]

> git checkout master -- your/path/file.php

Note: There are other ways to do similar tasks such as `git ls-tree` and they may have more options. However I find these to be more accessible and easy to remember.


Better Git: git fetch not getting tags

I just ran into a little gotcha with regard how fetch handles tags. When it pulls down commits, you will usually see that it also pulls down tags. I was a little confused today when fetch was refusing to pull a tag that was clearly in the repository (“git ls-remote --tags“) lets you see tags available on the remote).

I kept running the fetch command but the tag wouldn’t get pulled down. The reason has to do with the way fetch works, it only fetches tags that are direct references to a commit that is in a branch being fetched. To get all tags regardless of what commits they reference use the fetch in the following way.


# Passing the –tags argument makes fetch retreive tags explicitly.
git fetch –tags

From the git manual (“git help fetch”):

-t, –tags Most of the tags are fetched automatically as branch heads are downloaded, but tags that do not point at objects reachable from the branch heads that are being tracked will not be fetched by this mechanism. This flag lets all tags and their associated objects be downloaded. The default behavior for a remote may be specified with the remote.<name>.tagopt setting. See git-config(1).

This will effectively do the inverse of normal fetch. It will fetch all tags, and bring with is any necessary commits.

Hope this helps some poor confused folks out there a little bit of greif.


Better Git: How to find out what submodules there are in a different branch

Generally speaking Git is fantastic and easy to use – one of the few pain points is where submodules butt heads with branches.

Far from solving all the problems in related to this – I’ve found an easy way to find out what submodules are in a branch, commit, or tag without having to check that reference out. This method also shows you the commits that each submodule is currently pointing to.


#Where <ref> should be replaced with a branch name, tag name, or commit hash.
git ls-tree -rt <reference> | grep 'commit [0-9A-z]'

The ls-tree command is fairly simple, its like running the ls unix command but includes git information – like the type of object each item is and the hash reference for that object. That command can also be used on a specific directory also by using <reference>:<path>. The -r flag makes it recursive so that is searches through all the subdirectories in the branch.

The second part of this is a simple grep command, that filters the results by those of type “commit”, which is how ls-tree represents submodules. I’ve included a pattern to match the word commit followed by the SHA1 hash – to avoid getting items that have the word commit as part of a folder or filename.

Hope this helps someone out there – it took me a while to figure out how to do this.


Blending Custom Post Types and Custom Taxonomies

In the past I’ve mentioned a very complex plugin named “WidgetPress”, which I built and is the underlying foundation of the Sears Community and Kmart Community sites. What WidgetPress does is complex, and the short time I had did not allow me to really clean it up the way I would have liked to. This is why I have not released it publicly.

However, the lessons learned from WidgetPress are many, and some of the ideas and solutions that led to its completion can be feathered out of the bulky plugin into their own stand-alone plugins. Thats exactly what I am trying to do now. The specific functionality I’m working on right now is the idea of using custom post types and taxonomies in such a way that they are no longer independent. It will take some explaining to show how this is useful  so I will start by explaining how it is used in WidgetPress.

WidgetPress, at the end of the day just lets admin’s create “Section Fronts” based on category or a Page – define their layout, and drop widgets into them to determine the entire contents of that layout. The widgets themselves make up as much of the page as the theme developer likes – for our purposes, everything between the top navigation and the button navigation is entirely composed of widgets on almost every single page on the site.

For WidgetPress to work, I actually had to completely deconstruct and reengineer the way WordPress handles widgets internally. I completely bypass WP_Widget_Factory, I ignore all those horrible global arrays, and hijack the whole widget registration process. I built my own entire structure for handling widgets, while still allowing (most) normal WordPress widgets to function without a hitch. I could write a whole book about the reasons I had to rebuilt all this but I’ll skip that. In my implementation Widgets are actually a post type, and sidebars are constructed using a custom post type AND a custom taxonomy working together – not in the typical way posts and categories work together – instead the post type and taxonomy function as a single entity. When an admin creates a sidebar, in the background it creates a new post of that post type, and a new term for that taxonomy.

The flexibility this allows is not immediately obvious. Widgets being a post type mean that they exist independent of their actual sidebar. Sidebars being a taxonomy means widgets can be placed into any number of sidebars, and sidebars can be associated with any number of “Pages”, “Posts” or other post types registered for that taxonomy. Sidebars, being a taxonomy and a post type, means that although they are used primarily as a taxonomy – each term can actually store meta data! This is the functionality that today I am building into a separate component.

The project I’m doing this is completely different from WidgetPress, its actually for an in-house products plugin – the intention being that any bit of data about each product can easily be made to be meta data and/or a taxonomy term.

The working name for this is WP_Node. I realize “node” has all sorts of existing meanings and connotations. However, given the way this merges content and relationships, blending them into a single idea – I find it difficult to think of a term that fits it better while remaining as abstract as it should be.

Hopefully in the coming months I’ll be able to release WP_Node (or whatever I end up calling it), as an independent library or plugin. There are also several other very interesting things I’d like to pull out of WidgetPress – maybe at the end of all this I can rebuild WidgetPress using the separate components (each of which will be better thought out and better written).


Incorrect Datetime Bug Fix Updated – Version 1.1 Released

A user of the Incorrect Datetime Bug Fix plugin submitted a bug report last week. While the plugin was turned on the user was getting an error about improper usage of wpdb::prepare() in WordPress 3.5.

Warning: Missing argument 2 for wpdb::prepare()

This threw me for a loop, since its such a simple plugin that I’ve never had to change, I assumed that something changed in the new version of WordPress 3.5. I was all ready to submit a new trac ticket. As it turns out, it was my usage of wpdb::prepare() that had always been incorrect, if you are a developer and are interested in knowing more about the problem, read Andrew Nacin’s article on the subject. In the past WordPress simply wasn’t throwing out an error and that’s what was changed in WordPress 3.5. I found this by using git’s “bisect” feature, which is really great and I might write a blog post about it in the future.

As always I greatly appreciate but reports, idea submissions for my plugins, and any other feedback regarding the plugins I maintain. It is whats great about Open Source and it helps me validate its usage in the corporate world. The new version is now available for download. Please update to avoid these errors from being reported.