If you’re in the business of creating WordPress plugins for yourself or for others, then it’s likely that you’ve done work with custom post types.
On top of that, there’s a chance that you’ve needed to introduce some functionality into a custom post type based on what’s selected in the plugin settings (which I discussed all of that fun stuff in a previous post).
And I’ve seen a number of different ways in which this is done: Some of them involve complexity such as reading the select post types into an array, iterating through the list, comparing the current post type, setting a boolean, and so on.
But there is a cleaner way that this can be done. I’m not claiming it’s the best way (perhaps you could share some insight on your opinions on this in the comments), but it’s way that I’ve found useful and that I’ve been using a few times over throughout several projects.
Find the Post Type in the Theme Post Types
For the sake of completeness, let’s define the following example:
- There is a settings page that lists all of the current theme’s post types
- Each post type is associated with a checkbox
- When the user saved the plugin settings, the checkboxes store the name of the custom post type in the array.
In this case, code probably speaks better than anyway else, so take a look at several functions (which can easily be added to your current theme’s
functions.php file for demo purposes) to see how this all fits together.
Note that the functions that you’re going to see below assume that you’re familiar with the Settings API. Also note that the DocBlocks aren’t up to par with the WordPress Coding Standards as the primary purpose of all of this is to share the functionality.
If you’re good with that, then you should be good to go.
1. Add the Settings Menu
Though this is optional – that is, you could just add the settings to an existing menu – I think this helps to make the demo a bit clearer as it segments the rest of the functionality from WordPress.
And that’s how we’re going to define our custom menu item (that will appear under the Settings menu):
2. Introduce the Settings
Adding the settings consists of two steps:
- Defining the sections, settings, and options using the Settings API
- Setup the function for rendering the post types
- Rendering the options page
Luckily, the amount of code to do this is relatively short especially if you’re comfortable with, as mentioned, the settings API.
So first, define the section, settings, and options:
Then display the options on the screen:
Then add the pages that renders the actual options:
3. Use the Code on the Front End
At this point, the code can actually be used on the front end. In order to keep this trivial, I’m using
the_content filter and this is where the crux of everything comes together: This is how to determine if the current post type is in a list of supported post types.
If so, a message will be appended to the content; otherwise, it will not.
Instead, the idea behind this is to provide a clean and easy way to determine if the current post type exists is a list of select options used elsewhere in the WordPress options.
Obviously, I’ve placed more of an emphasis on what the code does throughout this article rather than pushing comments, documentation, and so on. I’ve done that in plenty of other articles. This is primarily meant to be a quick way to determine if you can find the current post type in the theme post types.
Anyway, I’m sure the code can be modified to be even more efficient and cleaner (though I’m not looking for something so clever that it’s a single line that takes me a number of mental cycles to parse :) but I’m open to whatever other suggests you may have.
Note that, though, the core of the purpose of this – despite all of the code necessary to setup it up – really reduces to this:
in_array( ucwords( get_post_type() ), get_option( 'acme-post-types' ) );
Anyway, I’m all ears as this is something that I’m encountering more and more.