Custom Post Type Permalinks - Version 0.7.5

Version Description

Download this release

Release Info

Developer Toro_Unit
Plugin Icon 128x128 Custom Post Type Permalinks
Version 0.7.5
Comparing to
See all releases

Code changes from version 0.7.4.1 to 0.7.5

Files changed (4) hide show
  1. cptp-ja.mo +0 -0
  2. cptp-ja.po +7 -1
  3. custom-post-type-permalinks.php +163 -146
  4. readme.txt +6 -1
cptp-ja.mo CHANGED
Binary file
cptp-ja.po CHANGED
@@ -8,7 +8,7 @@ msgstr ""
8
  "Project-Id-Version: Custom Post Type Permalinks 0.7.3 Ja\n"
9
  "Report-Msgid-Bugs-To: \n"
10
  "POT-Creation-Date: 2011-09-21 11:00+0900\n"
11
- "PO-Revision-Date: 2012-01-06 23:08+0900\n"
12
  "Last-Translator: Toro_Unit <mail@torounit.com>\n"
13
  "Language-Team: Toro_Unit <mail@torounit.com>\n"
14
  "MIME-Version: 1.0\n"
@@ -42,3 +42,9 @@ msgstr "末尾の '/' の有無は、標準のパーマリンク構造の設定
42
  msgid "If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/."
43
  msgstr "未入力のときは、/%year%/%monthnum%/%day%/%post_id%/ に設定されます。"
44
 
 
 
 
 
 
 
8
  "Project-Id-Version: Custom Post Type Permalinks 0.7.3 Ja\n"
9
  "Report-Msgid-Bugs-To: \n"
10
  "POT-Creation-Date: 2011-09-21 11:00+0900\n"
11
+ "PO-Revision-Date: 2012-01-18 15:37+0900\n"
12
  "Last-Translator: Toro_Unit <mail@torounit.com>\n"
13
  "Language-Team: Toro_Unit <mail@torounit.com>\n"
14
  "MIME-Version: 1.0\n"
42
  msgid "If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/."
43
  msgstr "未入力のときは、/%year%/%monthnum%/%day%/%post_id%/ に設定されます。"
44
 
45
+ msgid "Use custom permalink of custom taxonomy archive."
46
+ msgstr "カスタム分類のアーカイブを変更する。"
47
+
48
+ msgid "If you check,The custom taxonomy's permalinks is example.com/post_type/taxonomy/term."
49
+ msgstr "チェックした場合、カスタム分類のアーカイブのパーマリンクは example.com/post_type/taxonomy/term. に変更されます。"
50
+
custom-post-type-permalinks.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://www.torounit.com
5
  Description: Add post archives of custom post type and customizable permalinks.
6
  Author: Toro-Unit
7
  Author URI: http://www.torounit.com/plugins/custom-post-type-permalinks/
8
- Version: 0.7.4.1
9
  Text Domain: cptp
10
  Domain Path: /
11
  */
@@ -29,31 +29,25 @@ Domain Path: /
29
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30
  */
31
 
32
-
33
-
34
-
35
  /* This plugin don't support Multisite yet.*/
36
 
37
 
38
-
39
  class Custom_Post_Type_Permalinks {
40
 
41
- function add_hooks() {
 
 
42
  add_action('wp_loaded',array(&$this,'set_archive_rewrite'),99);
43
  add_action('wp_loaded', array(&$this,'set_rewrite'),100);
44
  add_filter('post_type_link', array(&$this,'set_permalink'),10,3);
 
45
  add_filter('getarchives_where', array(&$this,'get_archives_where'), 10, 2);
46
  add_filter('get_archives_link', array(&$this,'get_archives_link'));
 
47
  add_action('wp_loaded', array(&$this,'add_tax_rewrite'));
48
  add_filter('term_link', array(&$this,'set_term_link'),10,3);
49
  }
50
 
51
- static function uninstall_hook() {
52
- if ( function_exists('register_uninstall_hook') ) {
53
- register_uninstall_hook(__FILE__, array(&$this,'uninstall_hook_custom_permalink'));
54
- }
55
- }
56
-
57
  function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
58
  $chain = '';
59
  $parent = &get_term( $id, $taxonomy, OBJECT, 'raw');
@@ -77,8 +71,6 @@ class Custom_Post_Type_Permalinks {
77
  return $chain;
78
  }
79
 
80
-
81
-
82
  function set_archive_rewrite() {
83
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
84
 
@@ -88,74 +80,71 @@ class Custom_Post_Type_Permalinks {
88
  $slug = get_post_type_object($post_type)->rewrite['slug'];
89
 
90
  if( $slug ){
91
- add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type,'top');
92
- add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type,'top');
93
- add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type='.$post_type,'top');
94
- add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type='.$post_type,'top');
95
- add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type,'top');
96
- add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type,'top');
97
- add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type='.$post_type,'top');
98
- add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$post_type,'top');
99
- add_rewrite_rule($slug.'/date/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type,'top');
100
- add_rewrite_rule($slug.'/date/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type,'top');
101
- add_rewrite_rule($slug.'/date/([0-9]{4})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&paged=$matches[2]&post_type='.$post_type,'top');
102
- add_rewrite_rule($slug.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'top');
103
- add_rewrite_rule($slug.'/author/([^/]+)/?$','index.php?author=$matches[1]&post_type='.$post_type,'top');
104
- add_rewrite_rule($slug.'/?$','index.php?post_type='.$post_type,'top');
105
  }
106
 
107
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type,'top');
108
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type,'top');
109
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type='.$post_type,'top');
110
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type='.$post_type,'top');
111
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type,'top');
112
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type,'top');
113
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type='.$post_type,'top');
114
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$post_type,'top');
115
- add_rewrite_rule($post_type.'/date/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type,'top');
116
- add_rewrite_rule($post_type.'/date/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type,'top');
117
- add_rewrite_rule($post_type.'/date/([0-9]{4})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&paged=$matches[2]&post_type='.$post_type,'top');
118
- add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'top');
119
- add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?author=$matches[1]&post_type='.$post_type,'top');
120
- add_rewrite_rule($post_type.'/?$','index.php?post_type='.$post_type,'top');
121
  endforeach;
122
  }
123
- //rewrite_tagの追加
124
  function set_rewrite() {
125
  global $wp_rewrite;
126
 
127
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
128
-
129
  foreach ( $post_types as $post_type ):
 
130
 
131
- $permalink = get_option($post_type.'_structure');
 
132
 
133
- if( !$permalink ){
134
- $permalink = '/%year%/%monthnum%/%day%/%post_id%/';
135
- }
136
-
137
- $permalink = str_replace('%postname%','%'.$post_type.'%',$permalink);
138
- $permalink = str_replace('%post_id%','%'.$post_type.'_id%',$permalink);
139
 
140
  $slug = get_post_type_object($post_type)->rewrite['slug'];
141
- if( !$slug ) {
 
142
  $slug = $post_type;
143
- }
144
 
145
  $permalink = '/'.$slug.'/'.$permalink;
146
  $permalink = $permalink.'/%'.$post_type.'_page%';
147
- $permalink = str_replace('//','/',$permalink);
148
 
149
- $wp_rewrite->add_rewrite_tag('%post_type%', '([^/]+)','post_type=');
150
- $wp_rewrite->add_rewrite_tag('%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=');
151
- $wp_rewrite->add_rewrite_tag('%'.$post_type.'_page%', '/?([0-9]{1,}?)/?',"page=");
152
- $wp_rewrite->add_permastruct($post_type,$permalink, false);
153
 
154
  endforeach;
155
 
156
- $taxonomies = get_taxonomies(array("show_ui" => true, "_builtin" => false),'objects');
157
  foreach ( $taxonomies as $taxonomy => $objects ):
158
- $wp_rewrite->add_rewrite_tag("%$taxonomy%", '(.+?)',"$taxonomy=");
159
  endforeach;
160
 
161
  $wp_rewrite->use_verbose_page_rules = true;
@@ -168,75 +157,73 @@ class Custom_Post_Type_Permalinks {
168
  return $post_link;
169
 
170
  $post_type = $post->post_type;
171
- $permalink = $wp_rewrite->get_extra_permastruct($post_type);
172
 
173
- $permalink = str_replace('%post_type%', $post->post_type, $permalink);
174
- $permalink = str_replace('%'.$post_type.'_id%', $post->ID, $permalink);
175
- $permalink = str_replace('%'.$post_type.'_page%', "", $permalink);
176
 
177
- if( !$leavename ){
178
- $permalink = str_replace('%'.$post_type.'%', $post->post_name, $permalink);
179
- }
 
180
 
181
- $taxonomies = get_taxonomies( array('show_ui' => true),'objects');
182
  foreach ( $taxonomies as $taxonomy => $objects ) {
 
183
  if ( strpos($permalink, "%$taxonomy%") !== false ) {
184
- $terms = get_the_terms($post->ID,$taxonomy);
185
 
186
  if ( $terms ) {
187
  usort($terms, '_usort_terms_by_ID'); // order by ID
188
  $term = $terms[0]->slug;
189
 
190
- if ( $parent = $terms[0]->parent ) {
191
- $term = $this->get_taxonomy_parents($parent,$taxonomy, false, '/', true) . $term;
192
- }
193
  }
194
- $permalink = str_replace("%$taxonomy%", $term, $permalink);
 
195
  }
196
  }
197
 
198
- $user = get_userdata($post->post_author);
199
- $permalink = str_replace("%author%", $user->user_nicename, $permalink);
 
 
 
 
 
 
 
 
200
 
201
- $post_date = strtotime($post->post_date);
202
- $permalink = str_replace("%year%",date("Y",$post_date), $permalink);
203
- $permalink = str_replace("%monthnum%",date("m",$post_date), $permalink);
204
- $permalink = str_replace("%day%",date("d",$post_date), $permalink);
205
- $permalink = str_replace("%hour%",date("H",$post_date), $permalink);
206
- $permalink = str_replace("%minute%",date("i",$post_date), $permalink);
207
- $permalink = str_replace("%second%",date("s",$post_date), $permalink);
208
 
209
- $permalink = str_replace('//',"/",$permalink);
210
- $permalink = home_url(user_trailingslashit($permalink));
211
  return $permalink;
212
  }
213
 
214
  /**
215
- *
216
  *wp_get_archives fix for custom post
217
- *
218
- *How To Use:
219
- *
220
- * $arg = 'type=monthly';
221
- * if(is_post_type_archive()) {
222
- * $arg .= '&post_type='.get_query_var( 'post_type' );
223
- * }
224
- * wp_get_archives($arg);
225
- *
226
- *
227
- *
228
  */
229
 
230
  public $get_archives_where_r;
 
231
  function get_archives_where( $where, $r ) {
232
  $this->get_archives_where_r = $r;
233
- if ( isset($r['post_type']) ) {
 
234
  $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
235
- }
236
  return $where;
237
  }
238
 
239
  function get_archives_link( $link_html ) {
 
240
  if (isset($this->get_archives_where_r['post_type']) and $this->get_archives_where_r['type'] != 'postbypost'){
241
  $blog_url = get_bloginfo("url");
242
  $blog_url = rtrim($blog_url,"/");
@@ -247,61 +234,68 @@ class Custom_Post_Type_Permalinks {
247
  }
248
 
249
  /**
250
- *
251
- *fix permalink custom taxonomy
252
- *
253
- *Ex:
254
- * example.org/posttype/taxonomy/term/
255
- *
256
  */
257
-
258
  function add_tax_rewrite() {
 
 
 
 
259
  global $wp_rewrite;
260
  $taxonomies = get_taxonomies(array( '_builtin' => false));
261
- if(empty($taxonomies)){
262
- return ;
263
- }
264
 
265
  foreach ($taxonomies as $taxonomy) :
266
  $post_types = get_taxonomy($taxonomy)->object_type;
267
- foreach ($post_types as $post_type){
 
268
  $slug = get_post_type_object($post_type)->rewrite["slug"];
269
  //add taxonomy slug
270
- add_rewrite_rule($slug.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]','top');
271
- add_rewrite_rule($post_type.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]','top');
272
- }
 
 
 
 
 
 
 
 
 
273
  endforeach;
274
  }
275
 
276
  function set_term_link( $termlink, $term, $taxonomy ) {
 
 
 
 
277
  $taxonomy = get_taxonomy($taxonomy);
278
- if( $taxonomy->_builtin ) {
279
  return $termlink;
280
- }
281
 
282
- if(empty($taxonomy)){
283
  return $termlink;
284
- }
285
- $wp_home = get_option('home');
286
- $wp_home = rtrim($wp_home,'/');
287
 
288
  $post_type = $taxonomy->object_type[0];
289
  $slug = get_post_type_object($post_type)->rewrite['slug'];
290
- return str_replace($wp_home,$wp_home.'/'.$slug,$termlink);
291
- }
292
 
293
- static function uninstall_hook_custom_permalink () {
294
- $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
295
- foreach ( $post_types as $post_type ):
296
- delete_option( $post_type.'_structure' );
297
- endforeach;
298
- }
299
 
 
 
 
 
 
 
300
  }
301
 
302
  class Custom_Post_Type_Permalinks_Admin {
303
 
304
- function add_hooks() {
305
  add_action('init', array(&$this,'load_textdomain'));
306
  add_action('admin_init', array(&$this,'settings_api_init'),10);
307
  add_action('admin_init', array(&$this,'set_rules'),50);
@@ -312,10 +306,11 @@ class Custom_Post_Type_Permalinks_Admin {
312
  }
313
 
314
  function settings_api_init() {
315
- add_settings_section('setting_section',
316
  __("Permalink Setting for custom post type",'cptp'),
317
  array(&$this,'setting_section_callback_function'),
318
- 'permalink');
 
319
 
320
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
321
  foreach ($post_types as $post_type):
@@ -325,17 +320,17 @@ class Custom_Post_Type_Permalinks_Admin {
325
  $structure = trim(esc_attr($_POST[$post_type.'_structure']));#get setting
326
 
327
  #default permalink structure
328
- if( !$structure ) {
329
- $structure = '/%year%/%monthnum%/%day%/%post_id%/';
330
- }
331
  $structure = str_replace('//','/','/'.$structure);# first "/"
332
 
333
  #last "/"
334
  $lastString = substr(trim(esc_attr($_POST['permalink_structure'])),-1);
335
  $structure = rtrim($structure,'/');
336
- if ( $lastString == '/') {
337
- $structure = $structure.'/';
338
- }
339
 
340
  update_option($post_type.'_structure', $structure );
341
  }
@@ -344,13 +339,32 @@ class Custom_Post_Type_Permalinks_Admin {
344
 
345
  add_settings_field($post_type.'_structure',
346
  $post_type,
347
- array(&$this,'setting_callback_function'),
348
  'permalink',
349
- 'setting_section',
350
- $post_type.'_structure');
 
351
 
352
  register_setting('permalink',$post_type.'_structure');
353
  endforeach;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
  }
355
 
356
  function set_rules() {
@@ -370,20 +384,23 @@ class Custom_Post_Type_Permalinks_Admin {
370
  <?php
371
  }
372
 
373
- function setting_callback_function( $option ) {
374
  $post_type = str_replace('_structure',"" ,$option);
375
  $slug = get_post_type_object($post_type)->rewrite['slug'];
376
- if( !$slug ) {
377
  $slug = $post_type;
378
- }
379
  echo '/'.$slug.' <input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />';
380
  }
 
 
 
 
 
381
  }
382
 
383
 
384
- $custom_post_type_permalinks = new Custom_Post_Type_Permalinks;
385
- if(get_option("permalink_structure") != ""){ $custom_post_type_permalinks->add_hooks(); }
386
- $custom_post_type_permalinks->uninstall_hook();
387
- $custom_post_type_permalinks_admin = new Custom_Post_Type_Permalinks_Admin;
388
- $custom_post_type_permalinks_admin->add_hooks();
389
 
 
5
  Description: Add post archives of custom post type and customizable permalinks.
6
  Author: Toro-Unit
7
  Author URI: http://www.torounit.com/plugins/custom-post-type-permalinks/
8
+ Version: 0.7.5
9
  Text Domain: cptp
10
  Domain Path: /
11
  */
29
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30
  */
31
 
 
 
 
32
  /* This plugin don't support Multisite yet.*/
33
 
34
 
 
35
  class Custom_Post_Type_Permalinks {
36
 
37
+ static public $default_structure = '/%year%/%monthnum%/%day%/%post_id%/';
38
+
39
+ function __construct () {
40
  add_action('wp_loaded',array(&$this,'set_archive_rewrite'),99);
41
  add_action('wp_loaded', array(&$this,'set_rewrite'),100);
42
  add_filter('post_type_link', array(&$this,'set_permalink'),10,3);
43
+
44
  add_filter('getarchives_where', array(&$this,'get_archives_where'), 10, 2);
45
  add_filter('get_archives_link', array(&$this,'get_archives_link'));
46
+
47
  add_action('wp_loaded', array(&$this,'add_tax_rewrite'));
48
  add_filter('term_link', array(&$this,'set_term_link'),10,3);
49
  }
50
 
 
 
 
 
 
 
51
  function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
52
  $chain = '';
53
  $parent = &get_term( $id, $taxonomy, OBJECT, 'raw');
71
  return $chain;
72
  }
73
 
 
 
74
  function set_archive_rewrite() {
75
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
76
 
80
  $slug = get_post_type_object($post_type)->rewrite['slug'];
81
 
82
  if( $slug ){
83
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type, 'top' );
84
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type, 'top' );
85
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type='.$post_type, 'top' );
86
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type='.$post_type, 'top' );
87
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type, 'top' );
88
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type, 'top' );
89
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type='.$post_type, 'top' );
90
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$post_type, 'top' );
91
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type, 'top' );
92
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type, 'top' );
93
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&paged=$matches[2]&post_type='.$post_type, 'top' );
94
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' );
95
+ add_rewrite_rule( $slug.'/author/([^/]+)/?$', 'index.php?author=$matches[1]&post_type='.$post_type, 'top' );
96
+ add_rewrite_rule( $slug.'/?$', 'index.php?post_type='.$post_type, 'top' );
97
  }
98
 
99
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type, 'top' );
100
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type, 'top' );
101
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type='.$post_type, 'top' );
102
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type='.$post_type, 'top' );
103
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type, 'top' );
104
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type, 'top' );
105
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type='.$post_type, 'top' );
106
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$post_type, 'top' );
107
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type, 'top' );
108
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type, 'top' );
109
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&paged=$matches[2]&post_type='.$post_type, 'top' );
110
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' );
111
+ add_rewrite_rule( $post_type.'/date/([0-9]{4})/?$', 'index.php?author=$matches[1]&post_type='.$post_type, 'top' );
112
+ add_rewrite_rule( $post_type.'/?$', 'index.php?post_type='.$post_type, 'top' );
113
  endforeach;
114
  }
115
+
116
  function set_rewrite() {
117
  global $wp_rewrite;
118
 
119
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
 
120
  foreach ( $post_types as $post_type ):
121
+ $permalink = get_option( $post_type.'_structure' );
122
 
123
+ if( !$permalink )
124
+ $permalink = self::$default_structure;
125
 
126
+ $permalink = str_replace( '%postname%', '%'.$post_type.'%', $permalink );
127
+ $permalink = str_replace( '%post_id%', '%'.$post_type.'_id%', $permalink );
 
 
 
 
128
 
129
  $slug = get_post_type_object($post_type)->rewrite['slug'];
130
+
131
+ if( !$slug )
132
  $slug = $post_type;
 
133
 
134
  $permalink = '/'.$slug.'/'.$permalink;
135
  $permalink = $permalink.'/%'.$post_type.'_page%';
136
+ $permalink = str_replace( '//', '/', $permalink );
137
 
138
+ $wp_rewrite->add_rewrite_tag( '%post_type%', '([^/]+)', 'post_type=' );
139
+ $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=' );
140
+ $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_page%', '/?([0-9]{1,}?)/?',"page=" );
141
+ $wp_rewrite->add_permastruct( $post_type, $permalink, false );
142
 
143
  endforeach;
144
 
145
+ $taxonomies = get_taxonomies( array("show_ui" => true, "_builtin" => false), 'objects' );
146
  foreach ( $taxonomies as $taxonomy => $objects ):
147
+ $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" );
148
  endforeach;
149
 
150
  $wp_rewrite->use_verbose_page_rules = true;
157
  return $post_link;
158
 
159
  $post_type = $post->post_type;
160
+ $permalink = $wp_rewrite->get_extra_permastruct( $post_type );
161
 
162
+ $permalink = str_replace( '%post_type%', $post->post_type, $permalink );
163
+ $permalink = str_replace( '%'.$post_type.'_id%', $post->ID, $permalink );
164
+ $permalink = str_replace( '%'.$post_type.'_page%', "", $permalink );
165
 
166
+ if( !$leavename )
167
+ $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink );
168
+
169
+ $taxonomies = get_taxonomies( array('show_ui' => true),'objects' );
170
 
 
171
  foreach ( $taxonomies as $taxonomy => $objects ) {
172
+
173
  if ( strpos($permalink, "%$taxonomy%") !== false ) {
174
+ $terms = get_the_terms( $post->ID, $taxonomy );
175
 
176
  if ( $terms ) {
177
  usort($terms, '_usort_terms_by_ID'); // order by ID
178
  $term = $terms[0]->slug;
179
 
180
+ if ( $parent = $terms[0]->parent )
181
+ $term = $this->get_taxonomy_parents( $parent,$taxonomy, false, '/', true ) . $term;
 
182
  }
183
+
184
+ $permalink = str_replace( "%$taxonomy%", $term, $permalink );
185
  }
186
  }
187
 
188
+ $user = get_userdata( $post->post_author );
189
+ $permalink = str_replace( "%author%", $user->user_nicename, $permalink );
190
+
191
+ $post_date = strtotime( $post->post_date);
192
+ $permalink = str_replace( "%year%", date("Y",$post_date), $permalink );
193
+ $permalink = str_replace( "%monthnum%", date("m",$post_date), $permalink );
194
+ $permalink = str_replace( "%day%", date("d",$post_date), $permalink );
195
+ $permalink = str_replace( "%hour%", date("H",$post_date), $permalink );
196
+ $permalink = str_replace( "%minute%", date("i",$post_date), $permalink );
197
+ $permalink = str_replace( "%second%", date("s",$post_date), $permalink );
198
 
199
+ $permalink = str_replace('//', "/", $permalink );
200
+
201
+
202
+ $permalink = home_url( user_trailingslashit( $permalink ) );
203
+ $str = rtrim( preg_replace("/%[a-z]*%/","",get_option("permalink_structure")) ,'/');
204
+ $permalink = str_replace($str, "", $permalink );
 
205
 
 
 
206
  return $permalink;
207
  }
208
 
209
  /**
 
210
  *wp_get_archives fix for custom post
211
+ *Ex:wp_get_archives('&post_type='.get_query_var( 'post_type' ));
 
 
 
 
 
 
 
 
 
 
212
  */
213
 
214
  public $get_archives_where_r;
215
+
216
  function get_archives_where( $where, $r ) {
217
  $this->get_archives_where_r = $r;
218
+
219
+ if ( isset($r['post_type']) )
220
  $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
221
+
222
  return $where;
223
  }
224
 
225
  function get_archives_link( $link_html ) {
226
+
227
  if (isset($this->get_archives_where_r['post_type']) and $this->get_archives_where_r['type'] != 'postbypost'){
228
  $blog_url = get_bloginfo("url");
229
  $blog_url = rtrim($blog_url,"/");
234
  }
235
 
236
  /**
237
+ * fix permalink custom taxonomy
 
 
 
 
 
238
  */
 
239
  function add_tax_rewrite() {
240
+
241
+ if(get_option('no_taxonomy_structure'))
242
+ return false;
243
+
244
  global $wp_rewrite;
245
  $taxonomies = get_taxonomies(array( '_builtin' => false));
246
+ if(empty($taxonomies))
247
+ return false;
 
248
 
249
  foreach ($taxonomies as $taxonomy) :
250
  $post_types = get_taxonomy($taxonomy)->object_type;
251
+
252
+ foreach ($post_types as $post_type):
253
  $slug = get_post_type_object($post_type)->rewrite["slug"];
254
  //add taxonomy slug
255
+ add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/?$', 'index.php?'.$taxonomy.'=$matches[1]', 'top' );
256
+ add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' );
257
+ add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' );
258
+ add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/page/?([0-9]{1,})/?$', 'index.php?'.$taxonomy.'=$matches[1]&paged=$matches[2]', 'top' );
259
+
260
+ add_rewrite_rule( $post_type.'/'.$taxonomy.'/(.+?)/?$', 'index.php?'.$taxonomy.'=$matches[1]', 'top' );
261
+ add_rewrite_rule( $post_type.'/'.$taxonomy.'/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' );
262
+ add_rewrite_rule( $post_type.'/'.$taxonomy.'/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' );
263
+ add_rewrite_rule( $post_type.'/'.$taxonomy.'/(.+?)/page/?([0-9]{1,})/?$', 'index.php?'.$taxonomy.'=$matches[1]&paged=$matches[2]', 'top' );
264
+
265
+ endforeach;
266
+
267
  endforeach;
268
  }
269
 
270
  function set_term_link( $termlink, $term, $taxonomy ) {
271
+
272
+ if( get_option('no_taxonomy_structure') )
273
+ return $termlink;
274
+
275
  $taxonomy = get_taxonomy($taxonomy);
276
+ if( $taxonomy->_builtin )
277
  return $termlink;
 
278
 
279
+ if( empty($taxonomy) )
280
  return $termlink;
281
+
282
+ $wp_home = rtrim( get_option('home'), '/' );
 
283
 
284
  $post_type = $taxonomy->object_type[0];
285
  $slug = get_post_type_object($post_type)->rewrite['slug'];
 
 
286
 
 
 
 
 
 
 
287
 
288
+ //$termlink = str_replace( $term->slug.'/', $this->get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink );
289
+ $termlink = str_replace( $wp_home, $wp_home.'/'.$slug, $termlink );
290
+ $str = rtrim( preg_replace("/%[a-z]*%/","",get_option("permalink_structure")) ,'/');
291
+ return str_replace($str, "", $termlink );
292
+
293
+ }
294
  }
295
 
296
  class Custom_Post_Type_Permalinks_Admin {
297
 
298
+ function __construct () {
299
  add_action('init', array(&$this,'load_textdomain'));
300
  add_action('admin_init', array(&$this,'settings_api_init'),10);
301
  add_action('admin_init', array(&$this,'set_rules'),50);
306
  }
307
 
308
  function settings_api_init() {
309
+ add_settings_section('cptp_setting_section',
310
  __("Permalink Setting for custom post type",'cptp'),
311
  array(&$this,'setting_section_callback_function'),
312
+ 'permalink'
313
+ );
314
 
315
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
316
  foreach ($post_types as $post_type):
320
  $structure = trim(esc_attr($_POST[$post_type.'_structure']));#get setting
321
 
322
  #default permalink structure
323
+ if( !$structure )
324
+ $structure = Custom_Post_Type_Permalinks::$default_structure;
325
+
326
  $structure = str_replace('//','/','/'.$structure);# first "/"
327
 
328
  #last "/"
329
  $lastString = substr(trim(esc_attr($_POST['permalink_structure'])),-1);
330
  $structure = rtrim($structure,'/');
331
+
332
+ if ( $lastString == '/')
333
+ $structure = $structure.'/';
334
 
335
  update_option($post_type.'_structure', $structure );
336
  }
339
 
340
  add_settings_field($post_type.'_structure',
341
  $post_type,
342
+ array(&$this,'setting_structure_callback_function'),
343
  'permalink',
344
+ 'cptp_setting_section',
345
+ $post_type.'_structure'
346
+ );
347
 
348
  register_setting('permalink',$post_type.'_structure');
349
  endforeach;
350
+
351
+ add_settings_field(
352
+ 'no_taxonomy_structure',
353
+ __("Use custom permalink of custom taxonomy archive.",'cptp'),
354
+ array(&$this,'setting_no_tax_structure_callback_function'),
355
+ 'permalink',
356
+ 'cptp_setting_section'
357
+ );
358
+
359
+ register_setting('permalink','no_taxonomy_structure');
360
+ if(isset($_POST['submit']) && strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) {
361
+ if(!isset($_POST['no_taxonomy_structure'])){
362
+ $set = true;
363
+ }else {
364
+ $set = false;
365
+ }
366
+ update_option('no_taxonomy_structure', $set);
367
+ }
368
  }
369
 
370
  function set_rules() {
384
  <?php
385
  }
386
 
387
+ function setting_structure_callback_function( $option ) {
388
  $post_type = str_replace('_structure',"" ,$option);
389
  $slug = get_post_type_object($post_type)->rewrite['slug'];
390
+ if( !$slug )
391
  $slug = $post_type;
392
+
393
  echo '/'.$slug.' <input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />';
394
  }
395
+
396
+ function setting_no_tax_structure_callback_function(){
397
+ echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked( false, get_option('no_taxonomy_structure'),false) . ' /> ';
398
+ _e("If you check,The custom taxonomy's permalinks is example.com/post_type/taxonomy/term.","cptp");
399
+ }
400
  }
401
 
402
 
403
+ if(get_option("permalink_structure") != "")
404
+ new Custom_Post_Type_Permalinks;
 
 
 
405
 
406
+ new Custom_Post_Type_Permalinks_Admin;
readme.txt CHANGED
@@ -3,13 +3,15 @@ Contributors: Toro_Unit
3
  Tags: custom post type,permalink
4
  Requires at least: 3.2
5
  Tested up to: 3.3
6
- Stable tag: 0.7.4.1
7
 
8
  Lets you edit the permalink of custom post type.
9
 
10
  == Description ==
11
  Custom Post Type Permalinks lets you edit the permalink structure of custom post type.
12
 
 
 
13
 
14
 
15
 
@@ -30,6 +32,9 @@ That's it. You can access the permalinks setting by going to *Settings -> Permal
30
 
31
  == Changelog ==
32
 
 
 
 
33
 
34
  =0.7.4=
35
  * Bug fix taxonomy rewrite.
3
  Tags: custom post type,permalink
4
  Requires at least: 3.2
5
  Tested up to: 3.3
6
+ Stable tag: 0.7.5
7
 
8
  Lets you edit the permalink of custom post type.
9
 
10
  == Description ==
11
  Custom Post Type Permalinks lets you edit the permalink structure of custom post type.
12
 
13
+ Change custom taxonomy archive's permalink to "example.org/post_type/taxonomy_name/term_slug". Can disable this fix.
14
+
15
 
16
 
17
 
32
 
33
  == Changelog ==
34
 
35
+ =0.7.5=
36
+ * Add ability to disable to change custom taxonomy archive's permalink.
37
+
38
 
39
  =0.7.4=
40
  * Bug fix taxonomy rewrite.