PluginProbe ʕ •ᴥ•ʔ
Custom Post Type Permalinks / 0.6.2
Custom Post Type Permalinks v0.6.2
1.2.0 1.3.0 1.3.1 1.4.0 1.5.1 1.5.2 1.5.4 2.0.0 2.0.1 2.0.2 2.1.1 2.1.2 2.1.3 2.2.0 3.0.0 3.0.1 3.1.0 3.1.1 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.3.0 3.3.1 3.3.4 3.3.5 3.4.0 3.4.0-rc.1 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.5.2 3.5.3 3.5.4 3.5.5 trunk 0.6 0.6.1 0.6.2 0.7 0.7.1 0.7.10 0.7.2 0.7.2.1 0.7.3 0.7.3.1 0.7.4 0.7.4.1 0.7.5 0.7.5.1 0.7.5.2 0.7.5.6 0.7.6 0.7.8 0.7.9 0.7.9.1 0.7.9.2 0.8 0.8.1 0.8.6 0.8.7 0.8.7.1 0.8.7.5 0.8.7.6 0.9 0.9.1 0.9.2.1 0.9.3.1 0.9.3.2 0.9.3.3 0.9.5 0.9.5.1 0.9.5.2 0.9.5.3 0.9.5.4 0.9.5.6 0.9.6 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0
custom-post-type-permalinks / custom-post-type-permalinks.php
custom-post-type-permalinks Last commit date
cptp-ja.mo 14 years ago cptp-ja.po 14 years ago cptp.pot 14 years ago custom-post-type-permalinks.php 14 years ago readme.txt 14 years ago screenshot-1.png 14 years ago
custom-post-type-permalinks.php
379 lines
1 <?php
2 /*
3 Plugin Name: Custom Post Type Permalinks
4 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.6.2
9 */
10
11
12
13 /* Copyright 2011 Toro_Unit (email : mail@torounit.com)
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29
30
31
32
33 /* This plugin don't support Multisite yet.*/
34
35
36
37 class Custom_Post_Type_Permalinks {
38
39 function init_function(){
40 add_action('init',array(&$this,'set_archive_rewrite'),99);
41 add_action('init', array(&$this,'set_rewrite'),100);
42 add_filter('post_type_link', array(&$this,'set_permalink'),10,2);
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('generate_rewrite_rules', array(&$this,'add_tax_rewrite'));
48 add_filter('term_link', array(&$this,'set_term_link'),10,3);
49
50
51 if ( function_exists('register_uninstall_hook') ) {
52 register_uninstall_hook(__FILE__, array(&$this,'uninstall_hook_custom_permalink'));
53 }
54
55 }
56
57
58
59
60
61
62 //カスタム投稿タイプのアーカイブのリライトルールの追加
63 function set_archive_rewrite() {
64 $post_types = get_post_types(array("_builtin"=>false));
65
66 foreach ($post_types as $post_type):
67 if(!$post_type) continue;
68 $permalink = get_option($post_type."_structure");
69 $slug = get_post_type_object($post_type)->rewrite["slug"];
70
71 if($slug){
72
73 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");
74 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");
75 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");
76 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");
77 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");
78 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");
79 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");
80 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");
81 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");
82 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");
83 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");
84 add_rewrite_rule($slug.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,"top");
85 add_rewrite_rule($slug.'/([0-9]{1,})/?$','index.php?p=$matches[1]&post_type='.$post_type,"top");
86 add_rewrite_rule($slug.'/?$','index.php?post_type='.$post_type,"top");
87 }
88
89
90 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");
91 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");
92 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");
93 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");
94 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");
95 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");
96 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");
97 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");
98 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");
99 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");
100 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");
101 add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,"top");
102 add_rewrite_rule($post_type.'/([0-9]{1,})/?$','index.php?p=$matches[1]&post_type='.$post_type,"top");
103 add_rewrite_rule($post_type.'/?$','index.php?post_type='.$post_type,"top");
104 endforeach;
105 }
106
107
108
109
110 //パーマリンク構造を登録
111 function set_rewrite() {
112 global $wp_rewrite;
113 $post_types = get_post_types(array("_builtin"=>false));
114 foreach ($post_types as $post_type):
115
116 $permalink = get_option($post_type."_structure");
117 $slug = get_post_type_object($post_type)->rewrite["slug"];
118 if(!$permalink){
119 $permalink = '/%year%/%monthnum%/%day%/%post_id%/';
120 }
121
122 //フラグ
123 $post_id_count = 0;
124 $postname_count = 0;
125
126 //個別記事のパーマリンク
127 $permalink = str_replace('%post_id%','%'.$post_type.'_id%',$permalink,$post_id_count);
128 $permalink = str_replace('%postname%','%'.$post_type.'name%',$permalink,$postname_count);
129
130 $wp_rewrite->add_rewrite_tag('%'.$post_type.'_id%', '([^/]+)','post_type='.$post_type.'&p=');
131 $wp_rewrite->add_rewrite_tag('%'.$post_type.'name%', '([^/]+)',$post_type.'=');
132 $wp_rewrite->add_permastruct($post_type,$slug.$permalink, false);
133
134
135
136 //個別投稿のページング
137 $permalink_paged = $permalink;
138
139 $permalink_paged = str_replace("%year%","[0-9]{4}", $permalink_paged);
140 $permalink_paged = str_replace("%monthnum%","[0-9]{1,2}", $permalink_paged);
141 $permalink_paged = str_replace("%day%","[0-9]{1,2}", $permalink_paged);
142 $permalink_paged = str_replace("%hour%","[0-9]{1,2}", $permalink_paged);
143 $permalink_paged = str_replace("%minute%","[0-9]{1,2}", $permalink_paged);
144 $permalink_paged = str_replace("%second%","[0-9]{1,2}", $permalink_paged);
145
146 $permalink_paged = str_replace('%'.$post_type.'_id%',"([0-9]{1,})", $permalink_paged);
147 $permalink_paged = str_replace('%'.$post_type.'name%',"([^/]+)", $permalink_paged);
148
149
150 $permalink_paged = $permalink_paged."/([0-9]{1,})/?$";
151 $permalink_paged = str_replace("//","/", $permalink_paged);
152
153 $count = $post_id_count + $postname_count;
154
155 if( $count == 1 ){
156 if($post_id_count) {
157 add_rewrite_rule($slug.$permalink_paged,'index.php?p=$matches[1]&page=$matches[2]&post_type='.$post_type,"top");
158 }
159 elseif($postname_count) {
160 add_rewrite_rule($slug.$permalink_paged,'index.php?'.$post_type.'=$matches[1]&page=$matches[2]&post_type='.$post_type,"top");
161 }
162
163 }elseif( $count == 2 ) {
164 if(strpos($permalink,'%'.$post_type.'_id%') < strpos($permalink,'%'.$post_type.'name%')){
165 add_rewrite_rule($slug.$permalink_paged,'index.php?p=$matches[1]&page=$matches[3]&post_type='.$post_type,"top");
166 }else{
167 add_rewrite_rule($slug.$permalink_paged,'index.php?p=$matches[2]&page=$matches[3]&post_type='.$post_type,"top");
168 }
169
170 }
171
172
173 endforeach;
174 }
175
176
177 //個別投稿の出力URLの変更
178 function set_permalink($post_link, $id = 0) {
179 global $wp_rewrite;
180
181 $post = &get_post($id);
182 if (is_wp_error($post)){
183 return $post;
184 }
185
186 $newlink = $wp_rewrite->get_extra_permastruct($post->post_type);
187
188 $newlink = str_replace("%".$post->post_type."%", $post->post_type, $newlink);
189 $newlink = str_replace("%".$post->post_type."_id%", $post->ID, $newlink);
190 $newlink = str_replace("%".$post->post_type."name%", $post->post_name, $newlink);
191
192
193 $user = get_userdata($post->post_author);
194 $newlink = str_replace("%author%", $user->user_login, $newlink);
195
196
197
198 $post_date = strtotime($post->post_date);
199
200 $newlink = str_replace("%year%",date("Y",$post_date), $newlink);
201 $newlink = str_replace("%monthnum%",date("m",$post_date), $newlink);
202 $newlink = str_replace("%day%",date("d",$post_date), $newlink);
203 $newlink = str_replace("%hour%",date("H",$post_date), $newlink);
204 $newlink = str_replace("%minute%",date("i",$post_date), $newlink);
205 $newlink = str_replace("%second%",date("s",$post_date), $newlink);
206
207
208 $newlink = home_url(user_trailingslashit($newlink));
209 return $newlink;
210 }
211
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 $post_type_archives;
231
232 function get_archives_where($where, $r) {
233 if ( isset($r['post_type']) ) {
234 $this->post_type_archives = $r['post_type'];
235 $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
236 }else {
237 $this->post_type_archives = '';
238 }
239 return $where;
240 }
241
242 function get_archives_link($link_html) {
243 if ( '' != $this->post_type_archives )
244 $blog_url = get_bloginfo("url");
245 $link_html = str_replace($blog_url,$blog_url.$this->post_type_archives,$link_html);
246 return $link_html;
247 }
248
249
250
251 /**
252 *
253 *fix permalink custom taxonomy
254 *
255 *Ex:
256 * example.org/posttype/taxonomy/term/
257 *
258 */
259
260 function add_tax_rewrite(){
261 global $wp_rewrite;
262 $taxonomies = get_taxonomies(array("_builtin"=>false));
263 if(empty($taxonomies)){
264 return ;
265 }
266
267 foreach ($taxonomies as $taxonomy) :
268
269 $post_types = get_taxonomy($taxonomy)->object_type;
270 foreach ($post_types as $post_type){
271 $slug = get_post_type_object($post_type)->rewrite["slug"];
272 //add taxonomy slug
273
274 add_rewrite_rule($slug.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]');
275 add_rewrite_rule($post_type.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]');
276 }
277 endforeach;
278 }
279
280 function set_term_link($termlink,$term,$taxonomy){
281 $taxonomy = get_taxonomy($taxonomy);
282 if(empty($taxonomy)){
283 return $termlink;
284 }
285 $wp_home = get_option("home");
286 $wp_home = $wp_home."/";
287 $wp_home = str_replace("//","/",$wp_home);
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
294
295 //アンインストール時
296 function uninstall_hook_custom_permalink () {
297 $post_types = get_post_types(array("_builtin"=>false));
298 foreach ($post_types as $post_type):
299 delete_option($post_type."_structure");
300 endforeach;
301 }
302
303
304
305 }
306
307 if(get_option("permalink_structure") != ""){
308 $custom_post_type_permalinks = new Custom_Post_Type_Permalinks;
309 $custom_post_type_permalinks->init_function();
310 }
311
312
313
314
315 class Custom_Post_Type_Permalinks_Admin {
316
317 function init_function(){
318 add_action('init', array(&$this,'load_textdomain'));
319 add_action('admin_menu', array(&$this,'admin_menu_custom_permalink'));
320
321 }
322 function load_textdomain(){
323 load_plugin_textdomain('cptp',false,'custom-post-type-permalinks');
324 }
325
326 function admin_menu_custom_permalink () {
327 // 設定メニュー下にサブメニューを追加:
328 $menuName = __("Permalinks of Custom post type","cptp");
329 add_options_page($menuName, $menuName, 'manage_options', __FILE__, array(&$this,'admin_menu_custom_permalink_callback'));
330 }
331
332 // プラグインページのコンテンツを表示
333 function admin_menu_custom_permalink_callback () {
334 global $wp_rewrite;
335 $wp_rewrite->flush_rules();
336
337 // 設定変更画面を表示する
338 ?>
339 <div class="wrap">
340 <div class="icon32" id="icon-options-general"></div>
341 <h2><?php echo __("Permalinks of Custom post type","cptp"); //カスタム投稿タイプのパーマリンク?></h2>
342 <form method="post" action="options.php">
343 <?php wp_nonce_field('update-options'); ?>
344 <p><?php _e("Setting permalinks of custom post type.","cptp");//カスタム投稿タイプごとのパーマリンク構造を設定できます。?><br />
345 <?php _e("The tags you can use is '%year%','%monthnum%','%day%','%hour%','%minute%','%second%','%postname%','%post_id%' and '%author%'.","cptp");//使用できるタグは、%year%,%monthnum%,%day%,%hour%,%minute%,%second%,%postname%,%post_id%,%author%です。?><br />
346 <?php _e("If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/.","cptp");//未�
347 �力のときは、/%year%/%monthnum%/%day%/%post_id%/に設定されます。?>
348 </p>
349 <table class="form-table">
350 <?php
351 $post_types = get_post_types(array("_builtin"=>false));
352 $page_options = "";
353 foreach ($post_types as $post_type):
354
355 ?>
356 <tr valign="top"><th scope="row"><?php echo $post_type;?></th><td>/<?php echo get_post_type_object($post_type)->rewrite["slug"];?>&nbsp;<input type="text" name="<?php echo $post_type."_structure";?>" value="<?php echo get_option($post_type."_structure"); ?>" class="regular-text code" />
357 </td></tr>
358 <?php
359 $page_options .= $post_type."_structure".",";
360 endforeach;?>
361 </table>
362 <input type="hidden" name="action" value="update" />
363 <?php $page_options = rtrim($page_options, ",");?>
364
365 <input type="hidden" name="page_options" value="<?php echo $page_options;?>" />
366 <p class="submit">
367 <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
368 </p>
369 </form>
370 </div>
371
372 <?php
373
374 }
375
376 }
377 $custom_post_type_permalinks_admin = new Custom_Post_Type_Permalinks_Admin;
378 $custom_post_type_permalinks_admin->init_function();
379