Custom Post Type Permalinks - Version 0.7.2

Version Description

Download this release

Release Info

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

Code changes from version 0.7.1 to 0.7.2

Files changed (3) hide show
  1. custom-post-type-permalinks.php +111 -111
  2. readme.txt +10 -3
  3. screenshot-1.png +0 -0
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.1
9
  */
10
 
11
 
@@ -83,51 +83,48 @@ class Custom_Post_Type_Permalinks {
83
 
84
 
85
 
86
-
87
-
88
- //カスタム投稿タイプのアーカイブのリライトルールの追加
89
  function set_archive_rewrite() {
90
- $post_types = get_post_types(array("_builtin"=>false,"publicly_queryable"=>true));
91
 
92
- foreach ($post_types as $post_type):
93
- if(!$post_type) continue;
94
- $permalink = get_option($post_type."_structure");
95
- $slug = get_post_type_object($post_type)->rewrite["slug"];
96
 
97
  if($slug){
98
 
99
- 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");
100
- 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");
101
- 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");
102
- 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");
103
- 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");
104
-
105
- 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");
106
- 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");
107
- 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");
108
- 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");
109
- 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");
110
- 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");
111
- add_rewrite_rule($slug.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,"top");
112
- add_rewrite_rule($slug.'/([0-9]{1,})/?$','index.php?p=$matches[1]&post_type='.$post_type,"top");
113
- add_rewrite_rule($slug.'/?$','index.php?post_type='.$post_type,"top");
114
  }
115
 
116
 
117
- 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");
118
- 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");
119
- 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");
120
- 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");
121
- 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");
122
- 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");
123
- 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");
124
- 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");
125
- 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");
126
- 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");
127
- 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");
128
- add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,"top");
129
- add_rewrite_rule($post_type.'/([0-9]{1,})/?$','index.php?p=$matches[1]&post_type='.$post_type,"top");
130
- add_rewrite_rule($post_type.'/?$','index.php?post_type='.$post_type,"top");
131
  endforeach;
132
  }
133
 
@@ -137,79 +134,81 @@ class Custom_Post_Type_Permalinks {
137
  //rewrite_tagの追加
138
  function set_rewrite() {
139
  global $wp_rewrite;
140
- $post_types = get_post_types(array("_builtin"=>false,"publicly_queryable"=>true));
141
 
142
- foreach ($post_types as $post_type):
 
 
143
 
144
- $permalink = get_option($post_type."_structure");
145
- $slug = get_post_type_object($post_type)->rewrite["slug"];
146
 
147
- if(!$permalink){
148
  $permalink = '/%year%/%monthnum%/%day%/%post_id%/';
149
  }
150
 
151
- $permalink = str_replace('%postname%',"%$post_type%",$permalink);
 
 
 
 
 
 
152
 
153
- $permalink = '/%post_type%/'.$permalink;
154
  $permalink = str_replace('//','/',$permalink);
155
 
156
-
157
  $wp_rewrite->add_rewrite_tag('%post_type%', '([^/]+)','post_type=');
 
 
 
158
  $wp_rewrite->add_permastruct($post_type,$permalink, false);
159
 
160
-
161
-
162
  endforeach;
163
 
164
- //カスタム分類の対応
165
  $taxonomies = get_taxonomies(array("show_ui" => true),'objects');
166
  foreach ( $taxonomies as $taxonomy => $objects ):
 
167
  $wp_rewrite->add_rewrite_tag("%$taxonomy%", '(.+?)',"$taxonomy=");
 
168
  endforeach;
169
 
170
  $wp_rewrite->use_verbose_page_rules = true;
171
  }
172
 
173
- function term_id_asc($a, $b){
174
-
175
- if($a->term_id < $b->term_id){
176
- return -1;
177
- }else if($a->term_id > $b->term_id){
178
- return 1;
179
- }else{
180
- return 0;
181
- }
182
- }
183
 
184
-
185
- //パーマリンクの出力の変更
186
- function set_permalink($post_link, $post,$leavename) {
187
  global $wp_rewrite;
 
 
 
 
 
188
 
189
- $newlink = $wp_rewrite->get_extra_permastruct($post->post_type);
190
-
191
- $newlink = str_replace("%post_type%", $post->post_type, $newlink);
192
- $newlink = str_replace("%post_id%", $post->ID, $newlink);
193
- if(!$leavename){
194
- $newlink = str_replace("%$post->post_type%", $post->post_name, $newlink);
195
- }
196
 
197
- $newlink = str_replace("%postname%", $post->post_name, $newlink);
 
 
 
 
 
 
198
 
199
 
200
- //カスタム分類の対応
201
- $taxonomies = get_taxonomies(array("show_ui" => true),'objects');
202
  foreach ( $taxonomies as $taxonomy => $objects ) {
203
- $term = '';
204
- if ( strpos($newlink, "%$taxonomy%") !== false ) {
205
- $terms = get_the_terms($post->ID,$taxonomy);
206
  if ( $terms ) {
207
  usort($terms, '_usort_terms_by_ID'); // order by ID
208
  $term = $terms[0]->slug;
209
- if ( $parent = $terms[0]->parent )
 
210
  $term = get_taxonomy_parents($parent,$taxonomy, false, '/', true) . $term;
 
211
  }
212
- $newlink = str_replace("%$taxonomy%", $term, $newlink);
 
213
  }
214
 
215
  }
@@ -218,25 +217,25 @@ class Custom_Post_Type_Permalinks {
218
 
219
 
220
  $user = get_userdata($post->post_author);
221
- $newlink = str_replace("%author%", $user->user_login, $newlink);
222
 
223
 
224
 
225
  $post_date = strtotime($post->post_date);
226
 
227
- $newlink = str_replace("%year%",date("Y",$post_date), $newlink);
228
- $newlink = str_replace("%monthnum%",date("m",$post_date), $newlink);
229
- $newlink = str_replace("%day%",date("d",$post_date), $newlink);
230
- $newlink = str_replace("%hour%",date("H",$post_date), $newlink);
231
- $newlink = str_replace("%minute%",date("i",$post_date), $newlink);
232
- $newlink = str_replace("%second%",date("s",$post_date), $newlink);
233
 
234
- $newlink = str_replace('//',"/",$newlink);
235
 
236
- $newlink = home_url(user_trailingslashit($newlink));
237
- return $newlink;
238
  }
239
-
240
 
241
 
242
  /**
@@ -298,7 +297,6 @@ class Custom_Post_Type_Permalinks {
298
  foreach ($post_types as $post_type){
299
  $slug = get_post_type_object($post_type)->rewrite["slug"];
300
  //add taxonomy slug
301
-
302
  add_rewrite_rule($slug.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]');
303
  add_rewrite_rule($post_type.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]');
304
  }
@@ -310,21 +308,20 @@ class Custom_Post_Type_Permalinks {
310
  if(empty($taxonomy)){
311
  return $termlink;
312
  }
313
- $wp_home = get_option("home");
314
- $wp_home = $wp_home."/";
315
- $wp_home = str_replace("//","/",$wp_home);
316
  $post_type = $taxonomy->object_type[0];
317
- $slug = get_post_type_object($post_type)->rewrite["slug"];
318
  return str_replace($wp_home,$wp_home.$slug,$termlink);
319
  }
320
 
321
 
322
 
323
- //アンインストール時
324
  static function uninstall_hook_custom_permalink () {
325
- $post_types = get_post_types(array("_builtin"=>false,"publicly_queryable"=>true));
326
- foreach ($post_types as $post_type):
327
- delete_option($post_type."_structure");
328
  endforeach;
329
  }
330
 
@@ -350,27 +347,27 @@ class Custom_Post_Type_Permalinks_Admin {
350
 
351
  function settings_api_init(){
352
  add_settings_section('setting_section',
353
- __("Permalink Setting for custom post type","cptp"),
354
  array(&$this,'setting_section_callback_function'),
355
  'permalink');
356
 
357
 
358
 
359
- $post_types = get_post_types(array("_builtin"=>false,"publicly_queryable"=>true));
360
  foreach ($post_types as $post_type):
361
- if(isset($_POST["submit"])){
362
- if( strpos($_POST["_wp_http_referer"],"options-permalink.php") !== FALSE )
363
- update_option($post_type."_structure",esc_attr($_POST[$post_type."_structure"]));
364
  }
365
 
366
- add_settings_field($post_type."_structure",
367
  $post_type,
368
  array(&$this,'setting_callback_function'),
369
  'permalink',
370
  'setting_section',
371
- $post_type."_structure");
372
 
373
- register_setting('permalink',$post_type."_structure");
374
  endforeach;
375
 
376
  }
@@ -383,11 +380,10 @@ class Custom_Post_Type_Permalinks_Admin {
383
 
384
 
385
  function setting_section_callback_function() {
386
- //セクションの上の説明文
387
  ?>
388
- <p><?php _e("Setting permalinks of custom post type.","cptp");//カスタム投稿タイプごとのパーマリンク構造を設定できます。?><br />
389
- <?php _e("The tags you can use is '%year%','%monthnum%','%day%','%hour%','%minute%','%second%','%postname%','%post_id%','%author%','%category%','%tag%' and '%{custom_taxonomy_slug}%(Replace the taxomomy term)'.","cptp");?><br />
390
- <?php _e("If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/.","cptp");?>
391
  </p>
392
  <?php
393
  }
@@ -395,8 +391,12 @@ class Custom_Post_Type_Permalinks_Admin {
395
 
396
  function setting_callback_function( $option ) {
397
 
398
- $post_type = str_replace("_structure","" ,$option);
399
- echo '/'.$post_type.'<input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />';
 
 
 
 
400
 
401
  }
402
 
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.2
9
  */
10
 
11
 
83
 
84
 
85
 
 
 
 
86
  function set_archive_rewrite() {
87
+ $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
88
 
89
+ foreach ( $post_types as $post_type ):
90
+ if( !$post_type ) continue;
91
+ $permalink = get_option( $post_type.'_structure' );
92
+ $slug = get_post_type_object($post_type)->rewrite['slug'];
93
 
94
  if($slug){
95
 
96
+ 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');
97
+ 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');
98
+ 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');
99
+ 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');
100
+ 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');
101
+
102
+ 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');
103
+ 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');
104
+ 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');
105
+ 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');
106
+ 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');
107
+ 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');
108
+ add_rewrite_rule($slug.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'top');
109
+ add_rewrite_rule($slug.'/([0-9]{1,})/?$','index.php?p=$matches[1]&post_type='.$post_type,'top');
110
+ add_rewrite_rule($slug.'/?$','index.php?post_type='.$post_type,'top');
111
  }
112
 
113
 
114
+ 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');
115
+ 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');
116
+ 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');
117
+ 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');
118
+ 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');
119
+ 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');
120
+ 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');
121
+ 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');
122
+ 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');
123
+ 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');
124
+ 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');
125
+ add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'top');
126
+ add_rewrite_rule($post_type.'/([0-9]{1,})/?$','index.php?p=$matches[1]&post_type='.$post_type,'top');
127
+ add_rewrite_rule($post_type.'/?$','index.php?post_type='.$post_type,'top');
128
  endforeach;
129
  }
130
 
134
  //rewrite_tagの追加
135
  function set_rewrite() {
136
  global $wp_rewrite;
 
137
 
138
+ $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
139
+
140
+ foreach ( $post_types as $post_type ):
141
 
142
+ $permalink = get_option($post_type.'_structure');
 
143
 
144
+ if( !$permalink ){
145
  $permalink = '/%year%/%monthnum%/%day%/%post_id%/';
146
  }
147
 
148
+ $permalink = str_replace('%postname%','%'.$post_type.'_name%',$permalink);
149
+ $permalink = str_replace('%post_id%','%'.$post_type.'_id%',$permalink);
150
+
151
+ $slug = get_post_type_object($post_type)->rewrite['slug'];
152
+ if( !$slug ) {
153
+ $slug = $post_type;
154
+ }
155
 
156
+ $permalink = '/'.$slug.'/'.$permalink.'/%'.$post_type.'_page%';
157
  $permalink = str_replace('//','/',$permalink);
158
 
 
159
  $wp_rewrite->add_rewrite_tag('%post_type%', '([^/]+)','post_type=');
160
+ $wp_rewrite->add_rewrite_tag('%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=');
161
+ $wp_rewrite->add_rewrite_tag('%'.$post_type.'_name%', '([^/]+)',$post_type.'=');
162
+ $wp_rewrite->add_rewrite_tag('%'.$post_type.'_page%', '([0-9]{1,}?)/?',"page=");
163
  $wp_rewrite->add_permastruct($post_type,$permalink, false);
164
 
 
 
165
  endforeach;
166
 
 
167
  $taxonomies = get_taxonomies(array("show_ui" => true),'objects');
168
  foreach ( $taxonomies as $taxonomy => $objects ):
169
+
170
  $wp_rewrite->add_rewrite_tag("%$taxonomy%", '(.+?)',"$taxonomy=");
171
+
172
  endforeach;
173
 
174
  $wp_rewrite->use_verbose_page_rules = true;
175
  }
176
 
 
 
 
 
 
 
 
 
 
 
177
 
178
+ function set_permalink( $post_link, $post,$leavename ) {
 
 
179
  global $wp_rewrite;
180
+ $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
181
+ if( $draft_or_pending and !$leavename )
182
+ return $post_link;
183
+
184
+ $post_type = $post->post_type;
185
 
186
+ $permalink = $wp_rewrite->get_extra_permastruct($post_type);
 
 
 
 
 
 
187
 
188
+ $permalink = str_replace('%post_type%', $post->post_type, $permalink);
189
+ $permalink = str_replace('%'.$post_type.'_id%', $post->ID, $permalink);
190
+ $permalink = str_replace('%'.$post_type.'_page%', "", $permalink);
191
+
192
+ if( !$leavename ){
193
+ $permalink = str_replace('%'.$post_type.'_name%', $post->post_name, $permalink);
194
+ }
195
 
196
 
197
+ $taxonomies = get_taxonomies( array('show_ui' => true),'objects');
 
198
  foreach ( $taxonomies as $taxonomy => $objects ) {
199
+ if ( strpos($permalink, "%$taxonomy%") !== false ) {
200
+ $terms = get_the_terms($post->ID,$taxonomy);
201
+
202
  if ( $terms ) {
203
  usort($terms, '_usort_terms_by_ID'); // order by ID
204
  $term = $terms[0]->slug;
205
+
206
+ if ( $parent = $terms[0]->parent ) {
207
  $term = get_taxonomy_parents($parent,$taxonomy, false, '/', true) . $term;
208
+ }
209
  }
210
+
211
+ $permalink = str_replace("%$taxonomy%", $term, $permalink);
212
  }
213
 
214
  }
217
 
218
 
219
  $user = get_userdata($post->post_author);
220
+ $permalink = str_replace("%author%", $user->user_login, $permalink);
221
 
222
 
223
 
224
  $post_date = strtotime($post->post_date);
225
 
226
+ $permalink = str_replace("%year%",date("Y",$post_date), $permalink);
227
+ $permalink = str_replace("%monthnum%",date("m",$post_date), $permalink);
228
+ $permalink = str_replace("%day%",date("d",$post_date), $permalink);
229
+ $permalink = str_replace("%hour%",date("H",$post_date), $permalink);
230
+ $permalink = str_replace("%minute%",date("i",$post_date), $permalink);
231
+ $permalink = str_replace("%second%",date("s",$post_date), $permalink);
232
 
233
+ $permalink = str_replace('//',"/",$permalink);
234
 
235
+ $permalink = home_url(user_trailingslashit($permalink));
236
+ return $permalink;
237
  }
238
+
239
 
240
 
241
  /**
297
  foreach ($post_types as $post_type){
298
  $slug = get_post_type_object($post_type)->rewrite["slug"];
299
  //add taxonomy slug
 
300
  add_rewrite_rule($slug.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]');
301
  add_rewrite_rule($post_type.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]');
302
  }
308
  if(empty($taxonomy)){
309
  return $termlink;
310
  }
311
+ $wp_home = get_option('home');
312
+ $wp_home = $wp_home.'/';
313
+ $wp_home = str_replace('//','/',$wp_home);
314
  $post_type = $taxonomy->object_type[0];
315
+ $slug = get_post_type_object($post_type)->rewrite['slug'];
316
  return str_replace($wp_home,$wp_home.$slug,$termlink);
317
  }
318
 
319
 
320
 
 
321
  static function uninstall_hook_custom_permalink () {
322
+ $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
323
+ foreach ( $post_types as $post_type ):
324
+ delete_option( $post_type.'_structure' );
325
  endforeach;
326
  }
327
 
347
 
348
  function settings_api_init(){
349
  add_settings_section('setting_section',
350
+ __("Permalink Setting for custom post type",'cptp'),
351
  array(&$this,'setting_section_callback_function'),
352
  'permalink');
353
 
354
 
355
 
356
+ $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
357
  foreach ($post_types as $post_type):
358
+ if(isset($_POST['submit'])){
359
+ if( strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE )
360
+ update_option($post_type.'_structure',esc_attr($_POST[$post_type.'_structure']));
361
  }
362
 
363
+ add_settings_field($post_type.'_structure',
364
  $post_type,
365
  array(&$this,'setting_callback_function'),
366
  'permalink',
367
  'setting_section',
368
+ $post_type.'_structure');
369
 
370
+ register_setting('permalink',$post_type.'_structure');
371
  endforeach;
372
 
373
  }
380
 
381
 
382
  function setting_section_callback_function() {
 
383
  ?>
384
+ <p><?php _e("Setting permalinks of custom post type.",'cptp');?><br />
385
+ <?php _e("The tags you can use is '%year%','%monthnum%','%day%','%hour%','%minute%','%second%','%postname%','%post_id%','%author%','%category%','%tag%' and '%{custom_taxonomy_slug}%(Replace the taxomomy term)'.",'cptp');?><br />
386
+ <?php _e("If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/.",'cptp');?>
387
  </p>
388
  <?php
389
  }
391
 
392
  function setting_callback_function( $option ) {
393
 
394
+ $post_type = str_replace('_structure',"" ,$option);
395
+ $slug = get_post_type_object($post_type)->rewrite['slug'];
396
+ if( !$slug ) {
397
+ $slug = $post_type;
398
+ }
399
+ echo '/'.$slug.'<input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />';
400
 
401
  }
402
 
readme.txt CHANGED
@@ -3,7 +3,7 @@ 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.1
7
 
8
  Lets you edit the permalink of custom post type.
9
 
@@ -25,13 +25,20 @@ That's it. You can access the permalinks setting by going to *Settings -> Permal
25
 
26
  == Screenshots ==
27
 
 
 
28
 
29
  == Changelog ==
30
 
31
- = 0.7.1 =
 
 
 
 
 
32
  * Bug fix.
33
 
34
- = 0.7 =
35
  * Add %{taxonomy}% tag.
36
  * A large number of Bug Fix.
37
  * Change Setting Page. Use Setting API.
3
  Tags: custom post type,permalink
4
  Requires at least: 3.2
5
  Tested up to: 3.3
6
+ Stable tag: 0.7.2
7
 
8
  Lets you edit the permalink of custom post type.
9
 
25
 
26
  == Screenshots ==
27
 
28
+ 1. screenshot-1.png
29
+
30
 
31
  == Changelog ==
32
 
33
+
34
+ =0.7.2=
35
+ * Reweite bug fix.
36
+ * Prewview bug fix.
37
+
38
+ =0.7.1=
39
  * Bug fix.
40
 
41
+ =0.7=
42
  * Add %{taxonomy}% tag.
43
  * A large number of Bug Fix.
44
  * Change Setting Page. Use Setting API.
screenshot-1.png CHANGED
Binary file