Created
June 12, 2015 09:31
-
-
Save pankamilr/461a7eda6d46b4a13770 to your computer and use it in GitHub Desktop.
Rapid create custom post types and custom taxonomies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Rejestracja niestandardowych rodzajów zawartości | |
*/ | |
class RegisterCPTS { | |
/** | |
* Rodzaje zawartości do stworzenia | |
* @var type | |
*/ | |
var $types = array('new_type'); | |
/** | |
* Dodanie akcji rejestrującej rodzaje zawartości | |
*/ | |
function __construct() { | |
add_action("init", array($this, "register_types")); | |
} | |
/** | |
* Wywołanie funkcji register_post_type w pętli | |
* z zadeklarowanymi nazwami rodzajów zawartości | |
*/ | |
public function register_types() { | |
foreach ($this->types as $cpt) { | |
register_post_type($cpt, call_user_func_array(array($this, "types_args"), array($cpt))); | |
} | |
} | |
/** | |
* Ustawienia argumentów rodzajów zawartości | |
* | |
* @param string $type | |
* @return array | |
*/ | |
protected function types_args($type) { | |
/** | |
* Zmienna przetrzymująca dedykowane ustawienia dla typu | |
* Zerowanie zmiennej | |
*/ | |
$type_args = NULL; | |
/** | |
* Standardowe ustawienia | |
*/ | |
$args = array( | |
'labels' => call_user_func_array(array($this, "types_labels"), array($type)), | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => false, | |
'capability_type' => 'post', | |
'has_archive' => false, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array('title', 'editor', 'excerpt') | |
); | |
/** | |
* Dedykowane ustawienia dla danego typu $type | |
* @param $type string | |
*/ | |
switch ($type) { | |
case 'new_type': | |
$type_args = array( | |
'has_archive' => 'types', | |
'rewrite' => array( | |
'slug' => 'type' | |
), | |
'menu_icon' => 'dashicons-tablet', | |
'supports' => array('title', 'thumbnail', 'editor') | |
); | |
break; | |
} | |
/** | |
* Scalanie standardowych ustawień z dedykowanymi | |
* Dedykowane klucze tablicy nadpisuja standardowe ustawienia | |
* Klucze nie wypisane w dedykowanej tablicy zostają bez zmian | |
*/ | |
if (!is_null($type_args) && is_array($type_args)) { | |
$args = array_merge($args, $type_args); | |
} | |
return $args; | |
} | |
/** | |
* Ustawienia labelów, opisów dla rodzajów zawartości | |
* | |
* @param string $type | |
* @return array | |
*/ | |
protected function types_labels($type) { | |
/** | |
* Zmienna przetrzymująca dedykowane ustawienia dla typu | |
* Zerowanie zmiennej | |
*/ | |
$type_labels = NULL; | |
/** | |
* Standardowe ustawienia | |
* Nazwa wyświetlana generowana na podstawie nazwy bazowej rodzaju zawartości | |
*/ | |
$default_name = str_replace(array("_", "-"), array(" ", " "), $type); | |
$default_name = ucwords($default_name); | |
$labels = array( | |
'name' => $default_name, | |
'singular_name' => $default_name, | |
'menu_name' => $default_name, | |
'name_admin_bar' => $default_name | |
); | |
/** | |
* Dedykowane ustawienia dla danego typu $type | |
* @param $type string | |
*/ | |
switch ($type) { | |
} | |
/** | |
* Scalanie standardowych ustawień z dedykowanymi | |
* Dedykowane klucze tablicy nadpisuja standardowe ustawienia | |
* Klucze nie wypisane w dedykowanej tablicy zostają bez zmian | |
*/ | |
if (!is_null($type_labels) && is_array($type_labels)) { | |
$labels = array_merge($labels, $type_labels); | |
} | |
return $labels; | |
} | |
} | |
$cpts = new RegisterCPTS(); | |
class RegisterTax { | |
/** | |
* array(term => array(post_type)) | |
*/ | |
var $taxes = array( | |
'type_term' => array('new_type'), | |
); | |
public function __construct() { | |
add_action('init', array($this, 'register_tax')); | |
} | |
/** | |
* Wywołanie funkcji register_post_type w pętli | |
* z zadeklarowanymi nazwami rodzajów zawartości | |
*/ | |
public function register_tax() { | |
foreach ($this->taxes as $tax => $cpt) { | |
register_taxonomy($tax, $cpt, call_user_func_array(array($this, "tax_args"), array($tax))); | |
} | |
} | |
/** | |
* Ustawienia argumentów rodzajów zawartości | |
* | |
* @param string $type | |
* @return array | |
*/ | |
protected function tax_args($type) { | |
/** | |
* Zmienna przetrzymująca dedykowane ustawienia dla typu | |
* Zerowanie zmiennej | |
*/ | |
$type_args = NULL; | |
/** | |
* Standardowe ustawienia | |
*/ | |
$args = array( | |
'hierarchical' => true, | |
'labels' => call_user_func_array(array($this, "tax_labels"), array($type)), | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'update_count_callback' => '_update_post_term_count', | |
'query_var' => true, | |
); | |
/** | |
* Dedykowane ustawienia dla danego typu $type | |
* @param $type string | |
* @use $type_args | |
*/ | |
switch ($type) { | |
case 'type_term': | |
$type_args = array( | |
'hierarchical' => false, | |
'public' => false, | |
'show_admin_column' => false | |
); | |
break; | |
} | |
/** | |
* Scalanie standardowych ustawień z dedykowanymi | |
* Dedykowane klucze tablicy nadpisuja standardowe ustawienia | |
* Klucze nie wypisane w dedykowanej tablicy zostają bez zmian | |
*/ | |
if (!is_null($type_args) && is_array($type_args)) { | |
$args = array_merge($args, $type_args); | |
} | |
return $args; | |
} | |
/** | |
* Ustawienia labelów, opisów dla rodzajów zawartości | |
* | |
* @param string $type | |
* @return array | |
*/ | |
protected function tax_labels($type) { | |
/** | |
* Zmienna przetrzymująca dedykowane ustawienia dla typu | |
* Zerowanie zmiennej | |
*/ | |
$type_labels = NULL; | |
/** | |
* Standardowe ustawienia | |
* Nazwa wyświetlana generowana na podstawie nazwy bazowej rodzaju zawartości | |
*/ | |
$default_name = str_replace(array("_", "-"), array(" ", " "), $type); | |
$default_name = ucwords($default_name); | |
$labels = array( | |
'name' => $default_name, | |
'singular_name' => $default_name, | |
'menu_name' => $default_name, | |
'name_admin_bar' => $default_name | |
); | |
/** | |
* Dedykowane ustawienia dla danego typu $type | |
* @param $type string | |
*/ | |
switch ($type) { | |
} | |
/** | |
* Scalanie standardowych ustawień z dedykowanymi | |
* Dedykowane klucze tablicy nadpisuja standardowe ustawienia | |
* Klucze nie wypisane w dedykowanej tablicy zostają bez zmian | |
*/ | |
if (!is_null($type_labels) && is_array($type_labels)) { | |
$labels = array_merge($labels, $type_labels); | |
} | |
return $labels; | |
} | |
} | |
$taxes = new RegisterTax(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment