Skip to content

Instantly share code, notes, and snippets.

@sang4lv
Created August 13, 2013 03:24
Show Gist options
  • Save sang4lv/6217625 to your computer and use it in GitHub Desktop.
Save sang4lv/6217625 to your computer and use it in GitHub Desktop.
<?php
class Idu_Taxonomy {
function __construct($taxonomy) {
$this->taxonomy = $taxonomy;
$this->taxonomy["singular"] = $this->prepare_name($taxonomy["singular"]);
$this->taxonomy["plural"] = $this->prepare_name($taxonomy["plural"]);
if("tax" == $taxonomy["type"]) {
$this->generate_taxonomy();
} else if("cpt" == $taxonomy["type"]) {
$this->generate_posttype();
}
}
function prepare_name($string) {
if( preg_match("/-/", $string) ) {
return ucwords( str_replace("-", " ", $string) );
} else {
return $string;
}
}
function generate_posttype() {
register_post_type( $this->taxonomy["singular"], array(
'public' => true,
'menu_position' => $this->taxonomy["menu_pos"],
'capability_type' => 'page',
'rewrite' => array( 'slug' => $this->taxonomy["singular"] ),
'supports' => isset($this->taxonomy["supports"]) ? $this->taxonomy["supports"] : array( 'title', 'thumbnail', 'editor' ),
'labels' => array(
'name' => $this->taxonomy["plural"],
'menu_name' => $this->taxonomy["menu_label"],
'singular_name' => $this->taxonomy["singular"],
'add_new' => __("Add New"),
'add_new_item' => __("Add ") . $this->taxonomy["singular"],
'edit_item' => __("Edit ") . $this->taxonomy["singular"],
'new_item' => __("New ") . $this->taxonomy["singular"],
'all_items' => __("All ") . $this->taxonomy["plural"],
'view_item' => __("View ") . $this->taxonomy["singular"],
'search_items' => __("Search ") . $this->taxonomy["plural"],
'not_found' => __("Can not find ") . $this->taxonomy["singular"],
'not_found_in_trash' => __( sprintf("Can not find %s in trash", $this->taxonomy["singular"]) )
)
) );
}
function generate_taxonomy() {
register_taxonomy( $this->taxonomy["singular"], $this->taxonomy["post_type"], array(
'show_ui' => true,
'rewrite' => array( 'slug' => $this->taxonomy["singular"] ),
'query_var' => true,
'hierarchical' => true,
'show_admin_column' => true,
'labels' => array(
'name' => $this->taxonomy["plural"],
'singular_name' => $this->taxonomy["singular"],
'search_items' => __("Search ") . $this->taxonomy["plural"],
'all_items' => __("All ") . $this->taxonomy["plural"],
'parent_item' => __("Parent ") . $this->taxonomy["singular"],
'parent_item_colon' => __("Parent ") . $this->taxonomy["singular"] . ':',
'edit_item' => __("Edit ") . $this->taxonomy["singular"],
'update_item' => __("Update ") . $this->taxonomy["singular"],
'add_new_item' => __("Add New ") . $this->taxonomy["singular"],
'new_item_name' => __("New ") . $this->taxonomy["singular"],
'menu_name' => $this->taxonomy["menu_label"]
),
) );
if( exists($this->taxonomy["post_type"]) ) {
foreach ($this->taxonomy["post_type"] as $item) {
register_taxonomy_for_object_type( $this->taxonomy["singular"], $item );
}
}
}
}
$cpt = new Idu_Taxonomy( array(
"singular" => "book",
"plural" => "books",
"type" => "cpt"
) );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment