Gccoe

Total WordPress theme post types unlimited single post settings

The way to Add Customized Choices to the PTU WordPress Plugin


The Publish Varieties Limitless plugin, often known as “PTU”, means that you can simply add {custom} put up varieties and taxonomies to your WordPress website. I created this plugin as a result of different plugins, equivalent to the favored Customized Publish Kind UI plugin, are bloated & stuffed with upsells. It’s a nice add-on for any website that requires {custom} varieties and/or taxonomies.

On this information, I’ll present you learn how to add {custom} choices to the put up sort and taxonomy registration screens. This manner, you’ll be able to present built-in integration to your theme so anybody utilizing it might have extra management over their {custom} put up varieties and taxonomies.

This information is primarily centered round traditional themes, since block themes have entry to the location editor the place the top person can totally customise their put up varieties and taxonomies.

Why Add Customized Choices to the Publish Varieties Limitless Plugin

As a theme developer it’s possible you’ll be questioning why must you add {custom} choices to the PTU plugin. We’ll, by including choices to the plugin you may make it simpler for the top person to manage the design of their {custom} put up varieties and taxonomies. My Whole Theme is a good instance.

Here’s a screenshot displaying the “Single Publish” settings added by the Whole theme:

This supplies the person with the next helpful fields:

Use Clean Template: Allow to usee a clean template for the only put up sort posts (aka no header/footer)

Dynamic Template: Choose a template for the only put up show.

Title: Modifies the default single put up web page header title textual content.

Title Fashion: Choose the title model (can be utilized to cover the web page header title).

Title Tag: Choose the web page header title HTML tag (h1,h2,h3,h4,h5,h6,div).

Format: Choose the format for the only put up sort posts (no sidebar, left sidebar, proper sidebar, full display screen, and so forth).

Subsequent/Earlier: Permits the subsequent and former hyperlinks on the backside of the put up earlier than the footer (often disabled when utilizing a dynamic template as you’ll be able to add the subsequent/prev immediately within the dynamic template).

There are literally much more settings if a dynamic template isn’t set, however more often than not customers will probably be creating dynamic templates for his or her put up varieties.

As you’ll be able to see, when creating {custom} put up varieties with Whole, you should have lots of management over how the only posts look. Usually, themes are coded in order that {custom} put up sort archives and single posts all the time look the identical. Whole additionally consists of settings for {custom} taxonomies.

This manner, customers don’t have to make use of hooks, filters or template components to switch their {custom} put up varieties and taxonomies. This makes it doable for non-developers to create extra superior web sites.

How Publish Varieties Limitless Works

The Publish Varieties Limitless plugin works utilizing core WordPress performance. Customized put up varieties and taxonomies are literally registered as put up varieties themselves. And the settings when registering/modifying a put up sort or taxonomy are achieved through a {custom} metabox.

Registered {custom} put up varieties are saved in a {custom} put up sort named ptu and {custom} taxonomies are saved in a {custom} put up sort named ptu_tax.

When the web page masses the plugin hooks into the init hook to question the ptu and ptu_tax put up varieties, loop by way of and register them. The {custom} put up varieties and taxonomies are saved in a category variable to allow them to be retrieved shortly later.

Retrieving a Checklist of Publish Varieties & Taxonomies

These are the 2 capabilities you should use to retrieve the {custom} put up varieties and taxonomies:

PTUPostTypes::get_registered_items() – returns an array of put up varieties, the place the secret’s the put up sort title and the worth is the ID of the ptu put up sort put up.

PTUTaxonomies::get_registered_items() – returns an array of registered taxonomies, the place the secret’s the taxonomy title and the worth is the ID of the ptu_tax put up sort put up.

If you’ll want to verify or loop by way of person created {custom} put up varieties or taxonomies you should use these to public strategies.

Retrieving a Publish Kind or Taxonomy Setting Worth

I discussed beforehand that the put up sort and taxonomy settings are achieved through a metabox, which implies they’re saved as {custom} fields. To get the worth of any setting you’d use the core get_post_meta() operate like such:

// Get put up sort setting worth.
get_post_meta( ‘post_type_name’, ‘_ptu_{option_id}’, true );

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

The Publish Varieties Limitless plugin mechanically provides a _ptu_ prefix to the {custom} fields when they’re saved. Ensure that to incorporate this prefix when getting the worth of your personal choices. I’ll clarify in higher element with some examples in a while.

The way to Add Customized Choices to the Publish Kind & Taxonomy Add New/Edit Screens

Including choices is tremendous easy and it’s achieved by creating new tabs to the default metabox. You’ll be able to add new tabs to each the put up sort and the taxonomy new/edit screens. That is achieved, by hooking into the ptu/posttypes/meta_box_tabs and ptu/taxonomies/meta_box_tab filters.

Right here is an instance of including a brand new tab named “Cool Theme Settings” with a couple of {custom} choices. This code will add the identical settings to each the put up sort and taxonomy setting pages.

/**
* Add {custom} choices to the Publish Varieties Limitless Plugin.
*
* @hyperlink https://www.wpexplorer.com/post-types-unlimited-custom-options/
*/
operate 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 brand new tab that appears as follows:

Choice Area Varieties

The pattern snippet must be fairly straight ahead nevertheless it solely reveals learn how to add a couple of subject varieties. Beneath is a desk of all subject varieties with an outline and the listing of additional arguments that may be added for the kind.

Area TypeDescriptionExtra ArgstextBasic textual content subject.(bool) required(int) maxlengthnumberNumber sort subject.(int) step(int) min(int) maxtextareaTextarea.(int) rowscheckboxCheckbox.–selectCustom choose dropdown.(array) choicesmulti_selectMultiple checkboxes grouped collectively.(array) choicespageSelect choice that returns all pages.–image_sizeSelect choice that returns all outlined picture sizes.–taxonomySelect choice that returns all registered public taxonomy names.–dashiconDashicon icon choose.–

Conditional (present/disguise) Fields

The metabox class has the flexibility to indicate/disguise tabs and fields through a situation key. You’ll be able to add a brand new situation key to the tab array to manage the tab show or to particular person choices to manage the visibility of the choice.

The conditional argument works through an array utilizing the next format:

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

setting_to_check: (string) setting ID to verify the worth of.

operator: (string) may be “=” or “!=”.

value_to_check: (string) the anticipated worth you need to be equal or not equal to.

For instance, let’s say you wished so as to add a tab that solely shows if the put up sort “public” setting is enabled you are able to do so like such:

operate 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 let’s imagine you’re including 2 choices however the second choice ought to solely show if the primary choice is enabled. You are able to do so like such:

operate 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 Option’, ‘themename’ ),
‘id’ => ‘text_option’,
‘type’ => ‘text’,
‘condition’ => [ ‘check_option’, ‘=’, ‘true’ ], // !!! CONDITIONAL CHECK !!!
],
]
];
return $tabs;
}
add_filter( ‘ptu/posttypes/meta_box_tabs’, ‘wpexplorer_add_ptu_type_tabs’ );

Through the use of conditional checks you’ll be able to present a greater person expertise if sure choices will not be wanted relying on the worth of different choices. Simply observe that the conditional performance could be very minimal and can solely permit you to verify a single choice at a time towards a single worth.

Retrieving Your Customized Choice Values

Now that you know the way so as to add {custom} choices I’ll clarify learn how to get the worth of these choices. Earlier I defined that you’ll use the core get_post_meta() operate to seize the worth of your fields. However with a view to seize the {custom} subject worth you’ll want to go the right ID primarily based on the put up sort or taxonomy. I’ll present you the way to try this.

Getting the Worth of a Publish Kind Choice

Let’s say you’ve got an choice to decide on the format for the {custom} put up sort’s single posts and the choice ID is “post_layout”. So, in no matter code you’re utilizing to get the present put up format, you’ll want to add an additional verify to your {custom} PTU choice.

Right here is an instance of learn how to get a put up sort choice worth:

if ( is_callable( ‘PTUPostTypes::get_registered_items’ ) ) {
// Get the put up sort.
$post_type = get_post_type();

// Get registered PTU put up varieties.
$ptu_types = PTUPostTypes::get_registered_items();

// Test if the present put up sort is a part of the PTU varieties.
if ( isset( $ptu_types[ $post_type ] ) ) {
$ptu_post_layout = get_post_meta( $ptu_types[ $post_type ], ‘_ptu_themename_post_layout’, true );
}
}

The code works by checking to see if the present put up is a PTU {custom} put up sort and if that’s the case, it makes use of get_post_meta to seize the {custom} subject worth. Make sure to change themename_post_layout to the ID of the choice you’re retrieving.

Getting the Worth of a Taxonomy Choice

Getting the worth for a taxonomy choice is similar to that of a {custom} put up sort. The distinction is you’ll want to run a verify towards the registered taxonomies.

Right here is an instance of learn how to get a taxonomy choice worth:

if ( is_callable( ‘PTUTaxonomies::get_registered_items’ ) ) {
// Get the present taxonomy.
$taxonomy = get_query_var( ‘taxonomy’ );

// Get registered PTU taxonomies.
$ptu_taxonomies = PTUTaxonomies::get_registered_items();

// Test if the taxonomy is a part of the PTU taxonomies.
if ( isset( $ptu_taxonomies[ $taxonomy ] ) ) {
$ptu_archive_layout = get_post_meta( $ptu_taxonomies[ $taxonomy ], ‘_ptu_themename_archive_layout’, true );
}
}

The code works by checking to see if the present taxonomy is a PTU {custom} taxonomy and if that’s the case, it makes use of get_post_meta to seize the {custom} subject worth. Make sure to change themename_archive_layout to the ID of the choice you’re retrieving.

Making a Helper Operate to Retrieve Choices

To observe DRY rules I might suggest including a helper operate to your theme that you should use to get the worth of a PTU put up sort or taxonomy. Publish Varieties Limitless doesn’t embrace a helper operate as a result of it’s not wanted.

Get Publish Kind Choice Worth Helper Operate

Here’s a helper operate for returning a put up sort choice worth:

/**
* Get the worth of a PTU put up sort setting.
*/
operate themename_get_ptu_meta( string $post_type, string $setting_id, $default=”” ) {
if ( is_callable( ‘PTUPostTypes::get_registered_items’ ) ) {
$ptu_types = PTUPostTypes::get_registered_items();
if ( isset( $ptu_types[ $post_type ] ) ) {
if ( $default && ! metadata_exists( ‘put up’, $ptu_types[ $post_type ], $setting_id ) ) {
return $default;
}
return get_post_meta( $ptu_types[ $post_type ], $setting_id, true );
}
}
}

And right here is how you’d use the operate:

$post_layout = themename_get_ptu_meta( get_post_type(), ‘post_layout’ );

Get Taxonomy Choice Worth Helper Operate

Here’s a helper operate for returning a taxonomy choice worth:

/**
* Get the worth of a PTU taxonomy setting.
*/
operate themename_get_ptu_tax_meta( string $taxonomy, string $setting_id, $default=”” ) {
if ( is_callable( ‘PTUTaxonomies::get_registered_items’ ) ) {
$ptu_taxonomies = PTUTaxonomies::get_registered_items();
if ( isset( $ptu_taxonomies[ $taxonomy ] ) ) {
if ( $default && ! metadata_exists( ‘put up’, $ptu_taxonomies[ $taxonomy ], $setting_id ) ) {
return $default;
}
return get_post_meta( $ptu_taxonomies[ $taxonomy ], $setting_id, true );
}
}
}

And right here is how you’d use the operate:

$archive_layout = themename_get_ptu_tax_meta( get_query_var( ‘taxonomy’ ), ‘archive_layout’ );

In each helper capabilities you’ll be able to see I’ve additionally added a $default parameter you should use. This may permit you to go a default worth in case {custom} subject doesn’t exist.

Conclusion

Including choices to your theme for the Publish Varieties Limitless Plugin is tremendous simple and you must do it! Particularly in case you are promoting a premium theme, it will be very good to your customers to have the ability to add {custom} put up varieties and taxonomies to their website and have extra management over how they appear with none coding information.

You would additionally simply cease promoting or sustaining your personal {custom} themes and as a substitute turn into an affiliate of ThemeForest and suggest/use my Whole theme.

Let me know within the feedback if in case you have any points or questions!

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다