WP Custom Post Types UI and “Insert Into Post”

On a WordPress site I was working on recently I ran into an issue that had me stumped. Namely on some, but not all, of the Custom Post Types I was using, when using the Add Media dialog to upload and insert media to an entry, the “Insert Into Post” button was missing!

I googled, as one does, and similar reports led me to realise that the problem occurs when you generate a custom Edit screen for a particular post type. Specifically, if you don’t allow support for the Editor in your post type setup and then place it manually in a custom meta box. This project was a new build in WP3.3+, and so the Editor is rendered with the new wp_editor() function, with the ID set to ‘content’ so it automatically hooks into the regular save routine and you can display the content using the_content() in your template. In this situation, the media buttons will display perfectly happily, if called, and the Media Uploader works as expected. It’s just that the “Insert Into Post” button is gone. If you add Editor support in the arguments when registering the post type, it appears as expected.

(You may be asking why I want to render the Editor manually, which is a topic for another post. In brief, it made more sense for the content I’m collecting to have some custom fields rendered before the Editor, and to be able to label and provide some instructions for what should be entered in the Editor. More about that another time…)

Eventually, I ran across a helpful post on the WordPress Support Forums with a method for forcing the button to appear:

add_filter( 'get_media_item_args', 'force_send' );

function force_send($args){
$args['send'] = true;
return $args;
}

That works as described and I thank ‘misternifty’ for it. However, it forces the button to appear in such a way that it also appears when viewing/editing an attachment through the Media Library. That’s not ideal, mainly because it simply won’t work, but also because it has the potential to be confusing for the client for whom the site has been built. Fortunately, I hit upon a solution fairly quickly, which only targets the Post Types for which it’s a problem. This is what I came up with:


add_filter( 'get_media_item_args', 'force_send' );

function force_send($args){
$pid = $_GET['post_id'];
if(get_post_type($pid) == '{post type}') {
$args['send'] = true;
}
return $args;
}

I realised that when the Add Media dialog is called from within a post (or other post type), there is a query string sent to media-upload.php in the iframe to attach the uploaded file to the current post object. It takes the form: {your path to WP installation}/wp-admin/media-upload.php?post_id=xxx.

Therefore, what this code does is GET the parent post id from the iframe URL. Then you check if that post has a post type that is missing Editor support and force the button, if so. Et voila, the “Insert Into Post” is back in place where it’s needed.

A longwinded explanation, perhaps, but it will hopefully make it easier to find a solution that stumped both Google and I for too long this afternoon! If you have questions or comments about this approach, comments are open. I look forward to reading what you have to say!