View difference between Paste ID: G8YUGex9 and ZQrNCEaP
SHOW: | | - or go back to the newest paste.
1
add_action( 'init', 'add_property_and_location' );
2
3
function add_property_and_location() {
4
5
// register custom post type property
6
7
$labels = array(
8
    'name' => _x('Properties', 'post type general name'),
9
    'singular_name' => _x('Property', 'post type singular name'),
10
    'add_new' => _x('Add New', 'book'),
11
    'add_new_item' => __('Add New Property'),
12
    'edit_item' => __('Edit Property'),
13
    'new_item' => __('New Property'),
14
    'all_items' => __('All Properties'),
15
    'view_item' => __('View Property'),
16
    'search_items' => __('Search Properties'),
17
    'not_found' =>  __('No properties found'),
18
    'not_found_in_trash' => __('No properties found in Trash'), 
19
    'parent_item_colon' => '',
20
    'menu_name' => 'Properties'
21
22
  );
23
  $args = array(
24
    'labels' => $labels,
25
    'public' => true,
26
    'publicly_queryable' => true,
27
    'show_ui' => true, 
28
    'show_in_menu' => true, 
29
    'query_var' => true,
30
    'rewrite' => true,
31
    'has_archive' => true, 
32
    'hierarchical' => true,
33
    'menu_position' => null,
34
    'supports' => array( 'custom-fields', 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','page-attributes'  ),
35
  ); 
36
37
 register_post_type('property',$args);
38
 
39
 // register custom taxonomy location
40
 
41
 $labels = array(
42
    'name' => _x( 'Locations', 'taxonomy general name' ),
43
    'singular_name' => _x( 'Location', 'taxonomy singular name' ),
44
    'search_items' =>  __( 'Search location' ),
45
    'all_items' => __( 'All locations' ),
46
    'parent_item' => __( 'Parent location' ),
47
    'parent_item_colon' => __( 'Parent Location:' ),
48
    'edit_item' => __( 'Edit Location' ), 
49
    'update_item' => __( 'Update location' ),
50
    'add_new_item' => __( 'Add New location' ),
51
    'new_item_name' => __( 'New location Name' ),
52
    'menu_name' => __( 'location' ),
53
  ); 
54
  
55
  register_taxonomy('location',array('property'), array(
56
    'public' => true,
57
    'labels' => $labels,
58
    'show_ui' => true,
59
    'query_var' => true,
60
    'hierarchical' => true,
61
    'rewrite' => array('hierarchical' => true, 'slug' => 'properties' )
62
  ));
63
}
64
65
if(!class_exists('Refactord_add_rewrite_rules')):
66
    class Refactord_add_rewrite_rules {
67
68
        var $query_vars = array();
69
        var $rules = array();
70
        var $show_rules = false; //used for debugging rewrite rules
71
        var $show_query_vars = false; //used for debugging query vars
72
73
        function __construct($options = NULL){
74
            if(!is_null($options)){
75
                $this->init($options);
76
            }
77
        }
78
79
        function init($options){
80
            foreach($options as $key => $value){
81
                $this->$key = $value;
82
            }
83
84
            if(!empty($this->rules)){
85
                add_action('wp_head', array(&$this, 'flush_rules'));
86
                add_action('generate_rewrite_rules', array(&$this, 'add_rules'));
87
            }
88
89
            if(!empty($this->query_vars)){
90
                add_filter('query_vars', array(&$this, 'add_query_vars'));
91
            }
92
93
            if($this->show_rules){
94
                add_action('wp_footer', array(&$this, 'show_rules'), 1);
95
            }
96
97
            if($this->show_query_vars){
98
                add_action('wp_footer', array(&$this, 'show_query_vars'), 1);
99
            }
100
        }
101
102
        function add_query_vars($query_vars){
103
            foreach($this->query_vars as $var){
104
                $query_vars[] = $var;
105
            }
106
            return $query_vars;
107
        }
108
109
        function add_rules(){
110
            global $wp_rewrite;
111
            $wp_rewrite->rules = $this->rules + $wp_rewrite->rules;
112
        }
113
114
        function rules_exist(){
115
            global $wp_rewrite;
116
117
            foreach($this->rules as $key => $rule){
118
                if(!in_array($rule, $wp_rewrite->rules) || !key_exists($key, $wp_rewrite->rules)){
119-
                echo 'dooooooooooooont exist';
119+
120
                }
121
            }
122
            return TRUE;
123
        }
124
125
        function flush_rules(){
126
            global $wp_rewrite;
127
            if(!$this->rules_exist()){
128
                $wp_rewrite->flush_rules();
129
            }
130
        }
131
132
        function show_rules(){
133
            global $wp_rewrite;
134
135
            echo "<pre>";
136
            print_r($wp_rewrite->rules);
137
            echo "</pre>";
138
        }
139
140
        function show_query_vars(){
141
            global $wp_query;
142
143
            echo "<pre>";
144
            print_r($wp_query->query_vars);
145
            echo "</pre>";
146
        }
147
    }
148
endif;
149
150
151
152
  $options = array(  
153
    'rules' => array(
154
      'properties/(.+?)/(for-sale)/page/?([0-9]{1,})/?$' => 'index.php?location=$matches[1]&type=$matches[2]&paged=$matches[3]',
155
      'properties/(.+?)/(for-sale)/?$' => 'index.php?location=$matches[1]&type=$matches[2]',
156
      'properties/(.+?)/(for-rent)/page/?([0-9]{1,})/?$' => 'index.php?location=$matches[1]&type=$matches[2]&paged=$matches[3]',
157
      'properties/(.+?)/(for-rent)/?$' => 'index.php?location=$matches[1]&type=$matches[2]'
158
      ), 
159
      'query_vars' => array('type') 
160
  );  
161
  $add_rewrite_rules = new Refactord_add_rewrite_rules($options);