I've been working on extending the WP Dispensary plugin and wanted to share some of the ways I've found to help boost the power of the Custom Post Types you create. Custom Post Types are a great building block for turning WordPress into more than a piece of blogging software. Going beyond the original Posts and Pages, you can now segment your content as much as you need, with relative ease. I personally feel that WordPress wouldn't be where it is today without the inclusion of Post Type's in version 3.0. This is my attempt to show how a few simple code snippets can push the boundaries of what's possible with WordPress and Custom Post Types. Plus, it's a good way for me to keep the codes together in one place to reference in the future ?
If you're new to CPT's and/or haven't built a Custom Post Type yet, you can check out this article from Torque that breaks CPT's down and gives you a variety of ways to create your own.
Read it? Got your own already built? Great! Let's look at what we can do now that we have our CPT built. Note: This article assumes you have a basic understanding of developing for WordPress. Please leave a comment or reach out onTwitter if you need further clarification on any of the tips below.
1. Custom API endpoints
Since this plugin seems like a likely candidate for future app integration, I want to add in some custom API endpoints to allow for content handling in something like AngularJS or vue.js.
Giving your featured image an API endpoint**
You can add a function like the one below, adding a filter to rest_prepare_$CPTNAME which will grab the url for the Custom Post Type's post featured image.
In the example below, I want to add the featured image endpoint to my Flowers CPT, so it's rest_prepare_flowers.
[snippet slug=custom-api-endpoint-featured-images lang=php]
Adding API endpoints for custompost_tag taxonomies**
With WPD I have a variety of custom taxonomies for the CPT's I created, so I wanted to make sure that data is available via API endpoints too.
The code I use to accomplish this is below.
[snippet slug=custom-api-endpoint-tag-taxonomies lang=php]
Adding API endpoints for customcategory taxonomies**
The same set up can work for your custom category taxonomies too.
[snippet slug=custom-api-endpoints-category-taxonomies lang=php]
**Adding custom API endpoints for your metabox data
When you create custom post types, a lot of times you find yourself adding in custom metaboxes to suppor the data you need users to control through your Custom Post Type.
This data can have a custom API endpoint created by using the following code snippet.
The $productsizes array are the metabox information I needed for pricing in the WPD plugin so your set up may vary based on your metaboxes.
[snippet slug=custom-api-endpoints-metaboxes lang=php]
Once you have your custom post types, taxonomies and metaboxes API endpoints added, you can now allow developers to consume your API data and have more control over how the data gets displayed.
Custom API endpoints in action
You can look at an example of each of these endpoints being added into a live API by checking out the CannaBiz demo here.
2. oEmbed improvements
With WP Dispensary I wanted to customize the output of information through the Embeds so that the custom data I display within WPD's single item view gets embedded as well.
The filter below will filter out the_excerpt and replace it with the_content, making sure any content you added into the output of the_content get's displayed properly.
[snippet slug=oembed-customization-custom-post-types lang=php]
Note that on line 15 there is the wpd-oembed-wrap ID, which you can change to anything you'd like and then add custom CSS to the public output of your plugin.
[snippet slug=enqueue-custom-stylesheet-oembed lang=php]
See how I handle adding this in with WP Dispensary here.
Below is an example of how the WP Dispensary items get displayed through oEmbed now.
https://www.wpdispensary.com/demo/flowers/chemdawg/
3. Flush rewrite rules during your plugin's activation
One thing that I looked over when building the earlier versions of the WPD plugin was flush_rewrite_rules.
If I'm being completely honest, at the time I didn't even know it existed, so I was telling people in a FAQ page how to manually go in and resave their permalinks settings.
We live and learn, right? ?
Thankfully, WordPress does have a page in the codex on flush rewrite rules to look through.
With the code below, I am able to take the Custom Post Type function and add it to the activation hook within the WPD plugin.
I also do the same for the custom tag and category taxonomies for each menu type.
[snippet slug=flush-rewrite-rules-plugin-activation lang=php]
Now any time someone activates your plugin, your custom post types and taxonomies will flush and work with your current permalink settings.
Additional resources
Now that we've gone over a few ways to enhance your CPT's, it's time to dig even further and see what else you can do to make your plugins better. Here's some links to other content that you can check out, written by people much smarter than I am ?