Adding Custom Options to PTU WordPress Plugin

- Advertisement -

The Post Types Unlimited plugin, also known as “PTU”, allows you to easily add custom post types and taxonomies to your WordPress site. I created this plugin because other plugins, such as the popular Custom Post Type UI plugin, are bloated & full of upsells. It is a great add-on for any site that requires custom types and/or taxonomies.

In this guide, I will show you how to add custom options to the post type and taxonomy registration screens. This way, you can provide built-in integration for your theme so anyone using it can have more control over their custom post types and taxonomies.

- Advertisement -

Why Add Custom Options to the Post Types Unlimited Plugin

As a theme developer you may be wondering why should you add custom options to the PTU plugin. Well, by adding options to the plugin you can make it easier for the end user to control the design of their custom post types and taxonomies. My Total Theme is a great example.

- Advertisement -

Here is a screenshot showing the “Single Post” settings added by the Total theme:

This provides the user with the following useful fields:

- Advertisement -

Use Blank Template: Enable to use a blank template for the single post type posts (aka no header/footer)
Dynamic Template: Select a template for the single post display.
Title: Modifies the default single post page header title text.
Title Style: Select the title style (can be used to hide the page header title).
Title Tag: Select the page header title HTML tag (h1,h2,h3,h4,h5,h6,div).
Layout: Select the layout for the single post type posts (no sidebar, left sidebar, right sidebar, full screen, etc).
Next/Previous: Enables the next and previous links at the bottom of the post before the footer (usually disabled when using a dynamic template as you can add the next/prev directly in the dynamic template).

There are actually a lot more settings if a dynamic template isn’t set, but most of the time users will be creating dynamic templates for their post types.

As you can see, when creating custom post types with Total, you will have a lot of control over how the single posts look. Generally, themes are coded so that custom post type archives and single posts always look the same. Total also includes settings for custom taxonomies.

This way, users don’t have to use hooks, filters or template parts to modify their custom post types and taxonomies. This makes it possible for non-developers to create more advanced websites.

How Post Types Unlimited Works

The Post Types Unlimited plugin works using core WordPress functionality. Custom post types and taxonomies are actually registered as post types themselves. And the settings when registering/editing a post type or taxonomy are done via a custom metabox.

Registered custom post types are saved in a custom post type named ptu and custom taxonomies are saved in a custom post type named ptu_tax.

When the page loads the plugin hooks into the init hook to query the ptu and ptu_tax post types, loop through and register them. The custom post types and taxonomies are stored in a class variable so they can be retrieved quickly later.

Retrieving a List of Post Types & Taxonomies

These are the two functions you can use to retrieve the custom post types and taxonomies:

PTUPostTypes::get_registered_items() – returns an array of post types, where the key is the post type name and the value is the ID of the ptu post type post.
PTUTaxonomies::get_registered_items() – returns an array of registered taxonomies, where the key is the taxonomy name and the value is the ID of the ptu_tax post type post.

If you need to check or loop through user created custom post types or taxonomies you can use those to public methods.

Retrieving a Post Type or Taxonomy Setting Value

I mentioned previously that the post type and taxonomy settings are done via a metabox, which means they are stored as custom fields. To get the value of any setting you would use the core get_post_meta() function like such:

// Get post type setting value.
get_post_meta( ‘post_type_name’, ‘_ptu_{option_id}’, true );

// Get taxonomy setting value.
get_post_meta( ‘taxonomy_name’, ‘_ptu_{option_id}’, true );

The Post Types Unlimited plugin automatically adds a _ptu_ prefix to the custom fields when they are saved. Make sure to include this prefix when getting the value of your own options. I will explain in greater detail with some examples later on.

How to Add Custom Options to the Post Type & Taxonomy Add New/Edit Screens

Adding options is super simple and it’s done by creating new tabs to the default metabox. You can add new tabs to both the post type and the taxonomy new/edit screens. This is done, by hooking into the ptu/posttypes/meta_box_tabs and ptu/taxonomies/meta_box_tab filters.

Here is an example of adding a new tab named “Cool Theme Settings” with a few custom options. This code will add the same settings to both the post type and taxonomy setting pages.

/**
* Add custom options to the Post Types Unlimited Plugin.
*
* @link https://www.wpexplorer.com/post-types-unlimited-custom-options/
*/
function wpexplorer_add_ptu_type_tabs( array $tabs ): array {
$tabs[] = [
‘id’ => ‘themename_settings’,
‘title’ => esc_html__( ‘Cool Theme Settings’, ‘themename’ ),
‘fields’ => [
[
‘name’ => esc_html__( ‘Check Option’, ‘themename’ ),
‘id’ => ‘check_option’,
‘type’ => ‘checkbox’,
],
[
‘name’ => esc_html__( ‘Text Option’, ‘themename’ ),
‘id’ => ‘text_option’,
‘type’ => ‘text’,
],
[
‘name’ => esc_html__( ‘Custom Select Option’, ‘themename’ ),
‘id’ => ‘select_option’,
‘type’ => ‘select’,
‘choices’ => [
” => esc_html__( ‘- Select -‘, ‘themename’ ),
‘option_1’ => esc_html__( ‘Option 1’, ‘themename’ ),
‘option_2’ => esc_html__( ‘Option 2’, ‘themename’ ),
‘option_3’ => esc_html__( ‘Option 3’, ‘themename’ ),
],
],
[
‘name’ => esc_html__( ‘Image Select Option’, ‘themename’ ),
‘id’ => ‘image_size_option’,
‘type’ => ‘image_size’,
],
],
];
return $tabs;
}
add_filter( ‘ptu/posttypes/meta_box_tabs’, ‘wpexplorer_add_ptu_type_tabs’ );
add_filter( ‘ptu/taxonomies/meta_box_tabs’, ‘wpexplorer_add_ptu_type_tabs’ );

This code would add a new tab that looks as follows:

Option Field Types

The sample snippet should be pretty straight forward but it only shows how to add a few field types. Below is a table of all field types with a description and the list of extra arguments that can be added for the type.

Field Type Description Extra Args
text Basic text field. (bool) required
(int) maxlength
number Number type field. (int) step
(int) min
(int) max
textarea Textarea. (int) rows
checkbox Checkbox. –
select Custom select dropdown. (array) choices
multi_select Multiple checkboxes grouped together. (array) choices
page Select option that returns all pages. –
image_size Select option that returns all defined image sizes. –
taxonomy Select option that returns all registered public taxonomy names. –
dashicon Dashicon icon select. –

Conditional (show/hide) Fields

The metabox class has the ability to show/hide tabs and fields via a condition key. You can add a new condition key to the tab array to control the tab display or to individual options to control the visibility of the option.

The conditional argument works via an array using the following format:

‘condition’ => [ ‘setting_to_check’, ‘operator’, ‘value_to_check’ ]

setting_to_check: (string) setting ID to check the value of.
operator: (string) can be “=” or “!=”.
value_to_check: (string) the expected value you want to be equal or not equal to.

For example, let’s say you wanted to add a tab that only displays if the post type “public” setting is enabled you can do so like such:

function wpexplorer_add_ptu_type_tabs( array $tabs ): array {
$tabs[] = [
‘id’ => ‘themename_public_settings’,
‘title’ => esc_html__( ‘Public Settings’, ‘themename’ ),
‘condition’ => [ ‘public’, ‘=’, ‘true’ ], // !!! CONDITIONAL CHECK !!!
‘fields’ => [ ]
];
return $tabs;
}
add_filter( ‘ptu/posttypes/meta_box_tabs’, ‘wpexplorer_add_ptu_type_tabs’ );

Or lets say you are adding 2 options but the second option should only display if the first option is enabled. You can do so like such:

function wpexplorer_add_ptu_type_tabs( array $tabs ): array {
$tabs[] = [
‘id’ => ‘themename_public_settings’,
‘title’ => esc_html__( ‘Public Settings’, ‘themename’ ),
‘fields’ => [
[
‘name’ => esc_html__( ‘Check Option’, ‘themename’ ),
‘id’ => ‘check_option’,
‘type’ => ‘checkbox’,
],
[
‘name’ => esc_html__( ‘Conditional Text

- Advertisement -

Related articles

Googlebot: Understanding Google’s Web Crawler

What Is Googlebot? How Google's Web Crawler Works

Googlebot is the website crawler Google uses to find content on the internet. Learn how it works in this guide.

21 Tips for Effective Google Searches

21 Google Search Tips to Find Exactly What You Want

Get answers faster with these 21 Google search tips: 1) Filter your results, 2) Search within sites, and more.

13 Digital Marketing Types for Brand Growth in 2024

13 Types of Digital Marketing to Use for Brand Growth in 2024

Discover 13 types of digital marketing your brand needs to stay ahead of the competition and drive growth.

Product Marketing Strategy: A Step-by-Step Guide

How to Create a Product Marketing Strategy: A Step-by-Step Guide

Learn to craft a product marketing strategy with our detailed guide and effectively reach your target audience.

Measuring Influencer Marketing Success & ROI: A Guide

How to Measure Influencer Marketing Success & Return on Investment

What’s the best way to measure your next influencer marketing campaign? Find out with this article.