Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
register_custom_post_types
<?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 {
private static $custom_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'
)
)
),
);
private static $post_rule = '([^/]+.*?)/%post_type%/([^/]+)/?$';
private static $taxonomy_rule = '([^/]+.*?)/(%post_type%)/?$';
private static $wp_rewrite = false;
private static $taxonomy_slug = 'category';
private static $post_type = false;
/**
* 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 __construct() {
global $wp_rewrite;
self::$wp_rewrite = &$wp_rewrite;
self::create_custom_posttypes();
self::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);
}
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 = true ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
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] ) ){
self::$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);
self::$taxonomy_slug = substr( $taxonomy_slug, 0, strlen( $taxonomy_slug )-1 );
}
}
return str_replace('%'. $post->post_type . '_slug%', self::$taxonomy_slug, $permalink);
}
public function init_general() {
foreach( self::$custom_post_types as $post_type => $post_types_array){
if( $post_types_array['rewrite'] ){
self::$post_rule = $post_types_array['rewrite'];
self::$taxonomy_rule = $post_types_array['rewrite'];
}
$wp_rewrite_rules = self::$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, self::$post_rule );
$taxonomy_rule = str_replace( '%post_type%', $post_type, self::$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');
self::$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( self::$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' => 'Duchrsuchen',
'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 ),
)
);
}
self::$post_type = $post_type;
}
}
}
new register_custom_post_types();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.