markjaquith / feedback

Ask @markjaquith anything!
42 stars 4 forks source link

Best method to filter the_content for custom post types using a plugin #31

Closed MickeyKay closed 9 years ago

MickeyKay commented 9 years ago

I'm working on a plugin that registers a custom post type and needs to filter the_content for single displays of the post type. Any suggestions on best practices for doing this? I'd like to include the output as a separate template filter, like single-custom_post_type.php to be included, but I'm not sure if this is the best way. Is filtering on the_content the advised practice (filtering single_template doesn't seem to be a good idea for plugin use since I have no way to reliably mimic the theme's template HTML structure)? Another method? What suggestions do you have for creating custom content/output for custom post types so that it works as best as possible across all themes? Thanks!

markjaquith commented 9 years ago

I would just filter the_content and in the callback just bail immediately if it’s not is_single() && 'your-post-type' === get_post_type(). For performance reasons, I would hook in to the_content twice... once really early, to blank out the content. And then once really late to write the content you want. This is so that filters that work on "normal" hook levels don't have a lot of needless work to do (like, curling quotes, and stuff).