Custom Post Type
The Custom_Post_Type
class provides a convenient and reusable way to register custom post types in WordPress. By passing a configuration object during instantiation, you can dynamically set up various aspects of the custom post type, including menu icon, URL rewrite rules, and optional custom redirection.
Key Features
- Dynamic Registration: Configure multiple aspects of custom post types through a single instantiation.
- SEO and UI Control: Options to hide permalink, preview, and SEO columns in the WordPress editor.
- Custom Column Management: Utilizes the Manage_Columns class for managing admin columns.
- Custom Redirection: Supports both static and custom redirect functions for handling URL redirection.
You can find the full class implementation here.
Example with standard Redirection Usage
<?php// Instantiate Custom_Post_Type with a redirect to a static URLnew Custom_Post_Type((object) array( 'post_type' => 'media-and-press', 'singular_name' => 'Media and Press', // Singular name for the post type 'plural_name' => 'Media and Press', // Plural name for the post type 'args' => (object) array( 'menu_icon' => 'dashicons-portfolio', // Menu icon for the post type 'rewrite' => array('slug' => 'media-and-press', 'with_front' => false), // Rewrite rules 'terra_hide_permalink' => true, // Hides permalink in editor 'terra_hide_preview_button' => true, // Hides preview button 'terra_hide_seo_columns' => true, // Hides SEO columns in editor 'terra_redirect' => '/some-url', // Redirect to '/some-url'
// Use Manage_Columns class to configure admin columns 'terra_manage_columns' => array( 'featured_image' => array( 'label' => 'Featured Image', // Label for the column 'reference' => 'native' // Column source type ('native' or 'acf') ), 'highlighted' => array( 'label' => 'High', // Label for ACF column 'reference' => 'acf' // Indicates this column is from ACF ) ) )));?>
Configuration Details
- post_type (string): Defines the post type slug.
- singular_name (string): Singular display name for the post type.
- plural_name (string): Plural display name for the post type.
- args (object): Contains additional arguments for the post type setup:
- menu_icon (string): Sets a custom icon for the post type in the admin menu.
- rewrite (array): URL rewrite rules for the post type.
- slug: Custom slug for the URL structure.
- with_front: Controls URL prefixing.
- terra_hide_permalink (bool): Hides the permalink in the editor.
- terra_hide_preview_button (bool): Hides the preview button in the editor.
- terra_hide_seo_columns (bool): Hides SEO-related columns in the editor.
- terra_redirect (string|callable): Defines a redirect URL or a function for custom redirection.
- terra_manage_columns (array): Passes column settings to the Manage_Columns class, including:
- label (string): Column label.
- reference (string): Source type (native or acf).
Example with custom Redirection Usage
<?php
// Instantiate Custom_Post_Type with custom redirection logicnew Custom_Post_Type((object) array( 'post_type' => 'media-and-press', 'singular_name' => 'Media and Press', 'plural_name' => 'Media and Press', 'args' => (object) array( 'menu_icon' => 'dashicons-portfolio', 'rewrite' => array('slug' => 'media-and-press', 'with_front' => false), 'terra_hide_permalink' => true, 'terra_hide_preview_button' => true, 'terra_hide_seo_columns' => true, 'terra_redirect' => 'custom_redirect_media', // Redirects using custom function
// Use Manage_Columns class to configure admin columns 'terra_manage_columns' => array( 'featured_image' => array( 'label' => 'Featured Image', 'reference' => 'native' ), 'highlighted' => array( 'label' => 'High', 'reference' => 'acf' ) ) )));
// Custom redirection functionfunction custom_redirect_media() { $pageTitle = get_the_title(); // Gets the title of the current page if ($pageTitle == 'Test') { // Checks if title matches 'Test' $principalUrl = esc_url(home_url('/')); // Gets and escapes the home URL wp_redirect($principalUrl, 301); // Redirects to home with a 301 status exit; // Ends execution to complete redirection }}?>