Mark Jaquith makes plugins. He also makes a plugin about making plugins, called I Make Plugins, which auto-formats your ‘local’ plugin pages, so you don’t have to write anything, and just pull in the WordPress repository readme for your plugin. It’s almost like the Victor/Victoria of plugins. Anyway, my issue was I don’t use pages for my plugin listing, I use CPTs. And in order to use Mark’s plugin, I had to hack it.
So I did the smart thing, and emailed him with an “I love your plugin! Here’s what I had to change to make it work with a CPT, though.” A couple days later, Mark replied with “Use these filters instead. Untested!” He also bailed me out when I screwed it up, so he gets an Internet today.
Even though I’ve never actually messed with filters in this way (actions yes, filters I’m still learning), I sat down with my coffee and started reading. Yes, I actually read things before I jump in, contrary to what my friends think.
This turned out to be pretty simple, when you got around to it. Since Mark called apply_filters(NAME,PARAMS)
, all I had to do was add_filter(NAME,MYFUNCTION)
and then make a function, passing the PARAMS and parsing as I needed. Mark fixed my original code (which was hellishly not optimized) and fixed my weird preview issue by returning an option I forgot.
Post Type
First I had to set the post type. In this case, Mark defaults to pages, I default to plugins. Yes, I named a post type ‘plugins.’ It works.
add_filter( 'i-make-plugins__post_type', 'halfelf_imp_posttype' ); function halfelf_imp_posttype() { return 'plugins'; }
This takes in the arguments as $args
, resets it to plugins, and returns the new value.
Get Plugins
Mark also has a ‘post parent’ so where I just use the CPT’s archive page for https://halfelf.org/plugins, he has an actual page with sub-pages. I don’t need post parent, so per Mark’s suggestion, I need to remove it from ‘get plugins.’
add_filter( 'i-make-plugins__get_plugins', 'halfelf_imp_getplugins' ); function halfelf_imp_getplugins( $options ) { unset( $options['post_parent'] ); return $options; }
Since the parameters I’m pulling in are an array, I have to use unset instead of making it a null value.
Is Plugin
The last check is to verify this is a plugin, and we can return the content. The normal string for this checks if the parent of the page is the ‘page parent’ (set earlier normally, unset by me). I just swapped it for a ‘is this a plugin?’ There are two parameters in this one, and the second is the post ID, which I need to check post type.
add_filter ('i-make-plugins__is_plugin', 'halfelf_imp_isplugin', 10, 2); function halfelf_imp_isplugin( $is_plugin, $post ) { $post = get_post( $post ); return $post->post_type === 'plugins'; }
Originally I had a call to is_preview()
because, for some reason, it was overwriting all my post previews. While that only annoys me, it really annoys me! Thankfully once Mark fixed my ‘Get Plugins’ call, it all started working.