View difference between Paste ID: 2WRTVzFu and
SHOW: | | - or go back to the newest paste.
1-
1+
<?php
2
/*
3
4
Plugin Name: Add rewrite bases for custom post_types
5
Plugin URI:  http://aahacreative.com
6
Description: Shows only recent posts in category. (And allows HTML tags in widget title.)
7
Version: 1.5
8
Author: Aaron Harun.
9
Author URI: http://aahacreative.com
10
11
12
*/
13
14
$post_type_base = new post_type_base();
15
16
add_filter('rewrite_rules_array',array($post_type_base,'wp_insertMyRewriteRules'));
17
add_filter('init',array($post_type_base,'flushRules'));
18
add_filter('redirect_canonical',array($post_type_base,'redirect_canonical'),10,2);
19
20
register_activation_hook( __FILE__, 'flushRules' );
21
22
class post_type_base{
23
24
	private $bases = array('client','work','project');
25
26
	function flushRules(){
27
28
	$project_labels = array(
29
	'name' => _x('Ourl Projects', 'post type general name'),
30
	'singular_name' => _x('Project', 'post type singular name'),
31
	'add_new' => _x('Add New', 'project'),
32
	'add_new_item' => __('Add New site'),
33
	'edit_item' => __('Edit Project'),
34
	'new_item' => __('New Project'),
35
	'view_item' => __('View Client'),
36
	'search_items' => __('Search Projects'),
37
	'not_found' =>  __('No projects found'),
38
	'not_found_in_trash' => __('No projects found in Trash'), 
39
	'parent_item_colon' => ''
40
	);
41
42
	$project_args = array(
43
	'labels' => $project_labels,
44
	'public' => true,
45
	'publicly_queryable' => true,
46
	'show_ui' => true, 
47
	'query_var' => true,
48
	'_builtin' =>  false,
49
	'capability_type' => 'post',
50
	'hierarchical' => false,
51
	'menu_position' => null,
52
	'taxonomies' => array('features'),
53
	'rewrite' => array("slug" => "our-projects"), // Permalinks format
54
	'supports' => array('title','editor','custom-fields','revisions','thumbnail')
55
	); 
56
57
	register_post_type('project',$project_args);
58
59
		global $wp_rewrite;
60
		$wp_rewrite->flush_rules();
61
	}
62
63
64
	// Adding a new rule
65
	function wp_insertMyRewriteRules($rules){
66
		$newrules = array();
67
68
		foreach($this->bases as $base){
69
			$newrules['^'.$base.'/page/?([0-9]{1,})/?$'] = 'index.php?post_type='.$base.'&paged=$matches[1]';
70
			$newrules['^'.$base.'/?$'] = 'index.php?post_type='.$base;
71
		}
72
73
	return $newrules + $rules;
74
	}
75
76
	function redirect_canonical($redirect,$request){
77
78
		foreach($this->bases as $base)
79
			if(strpos($request,get_option('siteurl').'/'.$base) !== false)
80
				return false;
81
82
83
	return $redirect;
84
	}
85
86
}