Forked from derpixler/register_custom_post_types.php
Created
January 28, 2012 18:04
-
-
Save RalfAlbert/1695290 to your computer and use it in GitHub Desktop.
register_custom_post_types
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 | |
/** | |
* Meine Dokumentation zu der Funktion xyz die dafuer sorgt das XYZ etwas macht | |
* | |
* @autor Rene Reimann | |
* @package Theme XYZ | |
*/ | |
if ( ! class_exists( 'register_custom_post_types' ) ) { | |
class register_custom_post_types { | |
public $wp_rewrite = NULL; | |
public $post_type = NULL; | |
// predefine vars | |
public $custom_post_types = array(); | |
public $post_rule = '([^/]+.*?)/%post_type%/([^/]+)/?$'; | |
public $taxonomy_rule = '([^/]+.*?)/(%post_type%)/?$'; | |
public $taxonomy_slug = 'category'; | |
/** | |
* Meine Dokumentation zu der Funktion xyz die dafuer sorgt das XYZ etwas macht | |
* | |
* @autor Rene Reimann | |
* @package Theme XYZ | |
* @param bool $var | |
* @retun mixed | |
*/ | |
public function my_custom_post_types() { | |
global $wp_rewrite; | |
$this->wp_rewrite = &$wp_rewrite; | |
$this->create_custom_posttypes(); | |
$this->init_general(); | |
#self::print_rewrite_urls(); | |
add_action( 'template_redirect', array( &$this, 'load_template' ) ); | |
add_filter('query_vars', array( &$this, 'add_query_vars' ) ); | |
add_filter('post_link', array( &$this, 'filter_permalink' ), 10, 3); | |
add_filter('post_type_link', array( &$this, 'filter_permalink' ), 10, 3); | |
} | |
public function add_query_vars($public_query_vars) { | |
$public_query_vars[] = 'custom_taxonomy_name'; | |
$public_query_vars[] = 'post_type'; | |
return $public_query_vars; | |
} | |
public function load_template( $hard = true ) { | |
global $wp_query; | |
$post_type = $wp_query->query_vars['post_type']; | |
$custom_taxonomy_name = $wp_query->query_vars['custom_taxonomy_name']; | |
$template = $wp_query->query_vars['post_type'] . '.php'; | |
if( isset( $wp_query->query_vars['custom_taxonomy_name'] ) ){ | |
if( file_exists( TEMPLATEPATH. '/' . $template ) ) { | |
include( TEMPLATEPATH. '/' . $template ); | |
}else if( file_exists( TEMPLATEPATH. '/category.php' ) ) { | |
include( TEMPLATEPATH. '/category.php' ); | |
}else{ | |
include( TEMPLATEPATH. '/archive.php' ); | |
} | |
exit; | |
} | |
} | |
public function refresh_rewrite_rules( $hard = FALSE ) { | |
global $wp_rewrite; | |
$wp_rewrite->flush_rules( $hard ); | |
} | |
public function filter_permalink($permalink, $post, $leavename) { | |
if ( strpos( $permalink, '/%'. $post->post_type . '_slug%' ) === FALSE ) | |
return $permalink; | |
$terms = wp_get_object_terms( $post->ID, $post->post_type . '_kategorie' ); | |
if ( !is_wp_error( $terms ) && !empty( $terms ) && is_object( $terms[0] ) ){ | |
$this->taxonomy_slug = $terms[0]->slug; | |
if( count( $terms ) > 1 ){ | |
foreach( $terms as $term ){ | |
if( $term->parent != 0 ) | |
$terms_array[] = $term->slug; | |
} | |
$taxonomy_slug = implode( '/', $terms_array); | |
$this->taxonomy_slug = substr( $taxonomy_slug, 0, strlen( $taxonomy_slug )-1 ); | |
} | |
} | |
return str_replace('%'. $post->post_type . '_slug%', $this->taxonomy_slug, $permalink); | |
} | |
public function init_general() { | |
foreach( $this->custom_post_types as $post_type => $post_types_array){ | |
if( $post_types_array['rewrite'] ){ | |
$this->post_rule = $post_types_array['rewrite']; | |
$this->taxonomy_rule = $post_types_array['rewrite']; | |
} | |
$wp_rewrite_rules = $this->wp_rewrite->wp_rewrite_rules(); | |
if( ! $wp_rewrite_rules[$post_rule] ){ | |
add_action( 'init', array( &$this, 'refresh_rewrite_rules' ) ); | |
} | |
$post_rule = str_replace( '%post_type%', $post_type, $this->post_rule ); | |
$taxonomy_rule = str_replace( '%post_type%', $post_type, $this->taxonomy_rule ); | |
add_rewrite_rule($post_rule, 'index.php?' . $post_type . '=$matches[2]', 'top'); | |
add_rewrite_rule($taxonomy_rule, 'index.php?custom_taxonomy_name=$matches[1]&post_type=$matches[2]', 'top'); | |
$this->wp_rewrite->add_permastruct($post_type . '_category', '%pagename%/%' . $post_type . '_slug%'); | |
} | |
} | |
public function print_rewrite_urls(){ | |
print_r( self::$wp_rewrite->wp_rewrite_rules() ); | |
} | |
public function create_custom_posttypes(){ | |
foreach( $this->custom_post_types as $post_type => $post_types_array){ | |
register_post_type( $post_type, | |
array( | |
'hierarchical' => true, | |
'labels' => array( | |
'name' => $post_types_array['plural'], | |
'singular_name' => $post_types_array['singular'], | |
'search_items' => 'Durchsuchen', | |
'all_items' => 'Alle ' . $post_types_array['singular'], | |
'edit_item' => $post_types_array['singular'] . ' berabeiten', | |
'update_item' => $post_types_array['singular'] . ' aktualisieren', | |
'add_new_item' => 'Verfasse eine neue ' . $post_types_array['singular'], | |
'new_item_name' => 'Neue ' . $post_types_array['singular'], | |
'menu_name' => $post_types_array['plural'], | |
), | |
'label' => $post_types_array['plural'], | |
'public' => true, | |
'show_ui' => true, | |
'description' => true, | |
'supports' => array( | |
'title', | |
'excerpt', | |
'editor', | |
'thumbnail', | |
'revisions' | |
), | |
'capability_type' => 'post', | |
'query_var' => true, | |
'rewrite' => array('slug' => '%'. $post_type . '_slug%/' . $post_type), | |
'can_export' => true, | |
'taxonomys' => array($post_type . '_' . $post_types_array['taxonomy']['slug']), | |
'menu_position' => 5, | |
) | |
); | |
foreach( $post_types_array['taxonomy'] as $taxonomy_key => $taxonomy ){ | |
register_taxonomy( $post_type . '_' . $taxonomy['slug'], | |
array( $post_type, 'nav_menu'), | |
array( 'hierarchical' => true, | |
'labels' => array( | |
'name' => _x( $taxonomy['name'], 'taxonomy general name' ), | |
'singular_name' => _x( $taxonomy['name'], 'taxonomy singular name' ), | |
'search_items' => __( 'Search ' . $taxonomy['name'] ), | |
'all_items' => __( 'Alle ' . $taxonomy['name'] ), | |
'parent_item' => __( 'Eltern ' . $taxonomy['name'] ), | |
'edit_item' => __( $taxonomy['name'] . ' berabeiten' ), | |
'update_item' => __( 'Aktualisere ' . $taxonomy['name'] ), | |
'add_new_item' => __( 'Neue ' . $taxonomy['name'] ), | |
'new_item_name' => __( 'Neuer ' . $taxonomy['name'] ), | |
'menu_name' => __( $taxonomy['name'] ), | |
), | |
'show_ui' => true, | |
'query_var' => true, | |
#'rewrite' => array( 'slug' => $post_type ), | |
) | |
); | |
} | |
$this->post_type = $post_type; | |
} | |
} | |
} | |
} | |
$post_types = array( | |
'descriptions' => array( | |
'plural' => 'Beschreibungen', | |
'singular' => 'Beschreibung', | |
'taxonomy' => array( | |
array( | |
'slug' => 'kategorie', | |
'name' => 'Kategorie' | |
) | |
) | |
), | |
'faq' => array( | |
'plural' => 'FAQ', | |
'singular' => 'FAQ', | |
'taxonomy' => array( | |
array( | |
'slug' => 'kategorie', | |
'name' => 'Kategorie' | |
) | |
), | |
#'rewrite' => '([^/]+)/%post_type%/([^/]+)/?$' | |
), | |
'lexikon' => array( | |
'plural' => 'Lexika', | |
'singular' => 'Lexikon', | |
'taxonomy' => array( | |
array( | |
'slug' => 'kategorie', | |
'name' => 'Kategorie' | |
) | |
) | |
), | |
'ratgeber' => array( | |
'plural' => 'Ratgeber', | |
'singular' => 'Ratgeber', | |
'taxonomy' => array( | |
array( | |
'slug' => 'kategorie', | |
'name' => 'Kategorie' | |
) | |
) | |
), | |
); | |
$post_rule = '([^/]+.*?)/%post_type%/([^/]+)/?$'; | |
$taxonomy_rule = '([^/]+.*?)/(%post_type%)/?$'; | |
$taxonomy_slug = 'category'; | |
$rcpt = new register_custom_post_types(); | |
$rcpt->post_types = $post_types; | |
$rcpt->post_rule = $post_rule; | |
$rcpt->taxonomy_rule = $taxonomy_rule; | |
$rcpt->taxonomy_slug = $taxonomy_slug; | |
$rcpt->my_custom_post_types(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment