Skip to content

Instantly share code, notes, and snippets.

@dobbyloo
Last active September 28, 2021 13:53
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dobbyloo/6188322 to your computer and use it in GitHub Desktop.
Save dobbyloo/6188322 to your computer and use it in GitHub Desktop.
Wordpress: custom post wrapper class for "register_post_type" function.
<?php namespace PaintedCloud\WP\Classes;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class CustomPost {
protected $textdomain;
protected $posts;
public function __construct( $textdomain )
{
// Initialize text domain
$this->textdomain = $textdomain;
// Initialize the posts array
$this->posts = array();
// Add the action hook calling the register_custom_post method
add_action('init', array(&$this, 'register_custom_post'));
}
public function make( $type, $singular_label, $plural_label, $settings = array() )
{
// Define the default settings
$default_settings = array(
'labels' => array(
'name' => __($plural_label, $this->textdomain),
'singular_name' => __($singular_label, $this->textdomain),
'add_new_item' => __('Add New '.$singular_label, $this->textdomain),
'edit_item'=> __('Edit '.$singular_label, $this->textdomain),
'new_item'=>__('New '.$singular_label, $this->textdomain),
'view_item'=>__('View '.$singular_label, $this->textdomain),
'search_items'=>__('Search '.$plural_label, $this->textdomain),
'not_found'=>__('No '.$plural_label.' found', $this->textdomain),
'not_found_in_trash'=>__('No '.$plural_label.' found in trash', $this->textdomain),
'parent_item_colon'=>__('Parent '.$singular_label, $this->textdomain),
),
'public'=>true,
'has_archive' => true,
'menu_position'=>20,
'supports'=>array(
'title',
'editor',
'thumbnail'
),
'rewrite' => array(
'slug' => sanitize_title_with_dashes($plural_label)
)
);
// Override any settings provided by user
// and store the settings with the posts array
$this->posts[$type] = array_merge($default_settings, $settings);
}
public function register_custom_post()
{
// Loop through the registered posts
// and register all posts stored in the array
foreach($this->posts as $key=>$value) {
register_post_type($key, $value);
}
}
}
@peterhebert
Copy link

You should use sprintf function for the internationalization of the labels, like so:

'add_new_item' => sprintf( __( 'Add New %s', $this->textdomain ), $singular_label ),
'edit_item' => sprintf( __( 'Edit %s', $this->textdomain ), $singular_label ),
'new_item' => sprintf( __( 'New %s', $this->textdomain ), $singular_label ),
'view_item' => sprintf( __( 'View %s', $this->textdomain ), $singular_label ),
'search_items' => sprintf( __( 'Search %s', $this->textdomain ), $plural_label ),
'not_found' => sprintf( __( 'No %s found', $this->textdomain ), $plural_label ),
'not_found_in_trash' => sprintf( __( 'No %s found in trash', $this->textdomain ), $plural_label ),
'parent_item_colon' => sprintf( __( 'Parent %s', $this->textdomain ), $singular_label ),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment