<?php
/*
Plugin Name: Add rewrite bases for custom post_types
Plugin URI: http://aahacreative.com
Description: Shows only recent posts in category. (And allows HTML tags in widget title.)
Version: 1.5
Author: Aaron Harun.
Author URI: http://aahacreative.com
*/
$post_type_base = new post_type_base();
add_filter('rewrite_rules_array',array($post_type_base,'wp_insertMyRewriteRules'));
add_filter('init',array($post_type_base,'flushRules'));
add_filter('redirect_canonical',array($post_type_base,'redirect_canonical'),10,2);
register_activation_hook( __FILE__, 'flushRules' );
class post_type_base{
private $bases = array('client','work','project');
function flushRules(){
$project_labels = array(
'name' => _x('Ourl Projects', 'post type general name'),
'singular_name' => _x('Project', 'post type singular name'),
'add_new' => _x('Add New', 'project'),
'add_new_item' => __('Add New site'),
'edit_item' => __('Edit Project'),
'new_item' => __('New Project'),
'view_item' => __('View Client'),
'search_items' => __('Search Projects'),
'not_found' => __('No projects found'),
'not_found_in_trash' => __('No projects found in Trash'),
'parent_item_colon' => ''
);
$project_args = array(
'labels' => $project_labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'_builtin' => false,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'taxonomies' => array('features'),
'rewrite' => array("slug" => "our-projects"), // Permalinks format
'supports' => array('title','editor','custom-fields','revisions','thumbnail')
);
register_post_type('project',$project_args);
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// Adding a new rule
function wp_insertMyRewriteRules($rules){
$newrules = array();
foreach($this->bases as $base){
$newrules['^'.$base.'/page/?([0-9]{1,})/?$'] = 'index.php?post_type='.$base.'&paged=$matches[1]';
$newrules['^'.$base.'/?$'] = 'index.php?post_type='.$base;
}
return $newrules + $rules;
}
function redirect_canonical($redirect,$request){
foreach($this->bases as $base)
if(strpos($request,get_option('siteurl').'/'.$base) !== false)
return false;
return $redirect;
}
}