custom-post-type-permalinks
Last commit date
cptp-ja.mo
14 years ago
cptp-ja.po
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
443 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.7.8 |
| 9 | Text Domain: cptp |
| 10 | Domain Path: / |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | |
| 15 | /* Copyright 2011 Toro_Unit (email : mail@torounit.com) |
| 16 | |
| 17 | This program is free software; you can redistribute it and/or modify |
| 18 | it under the terms of the GNU General Public License as published by |
| 19 | the Free Software Foundation; either version 2 of the License, or |
| 20 | (at your option) any later version. |
| 21 | |
| 22 | This program is distributed in the hope that it will be useful, |
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | GNU General Public License for more details. |
| 26 | |
| 27 | You should have received a copy of the GNU General Public License |
| 28 | along with this program; if not, write to the Free Software |
| 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 | public 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_action('wp_loaded', array(&$this,'add_tax_rewrite')); |
| 43 | |
| 44 | if(get_option("permalink_structure") != "") { |
| 45 | add_filter('post_type_link', array(&$this,'set_permalink'),10,3); |
| 46 | |
| 47 | add_filter('getarchives_where', array(&$this,'get_archives_where'), 10, 2); |
| 48 | add_filter('get_archives_link', array(&$this,'get_archives_link'),20,1); |
| 49 | add_filter('term_link', array(&$this,'set_term_link'),10,3); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) { |
| 54 | $chain = ''; |
| 55 | $parent = &get_term( $id, $taxonomy, OBJECT, 'raw'); |
| 56 | if ( is_wp_error( $parent ) ) { |
| 57 | return $parent; |
| 58 | } |
| 59 | |
| 60 | if ( $nicename ){ |
| 61 | $name = $parent->slug; |
| 62 | }else { |
| 63 | $name = $parent->name; |
| 64 | } |
| 65 | |
| 66 | if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { |
| 67 | $visited[] = $parent->parent; |
| 68 | $chain .= $this->get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited ); |
| 69 | } |
| 70 | |
| 71 | if ( $link ) { |
| 72 | $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator; |
| 73 | }else { |
| 74 | $chain .= $name.$separator; |
| 75 | } |
| 76 | return $chain; |
| 77 | } |
| 78 | |
| 79 | public function set_archive_rewrite() { |
| 80 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) ); |
| 81 | |
| 82 | foreach ( $post_types as $post_type ): |
| 83 | if( !$post_type ) continue; |
| 84 | $permalink = get_option( $post_type.'_structure' ); |
| 85 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 86 | |
| 87 | 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' ); |
| 88 | 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' ); |
| 89 | 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' ); |
| 90 | 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' ); |
| 91 | 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' ); |
| 92 | 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' ); |
| 93 | 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' ); |
| 94 | 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' ); |
| 95 | 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' ); |
| 96 | 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' ); |
| 97 | 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' ); |
| 98 | add_rewrite_rule( $slug.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' ); |
| 99 | add_rewrite_rule( $slug.'/author/([^/]+)/?$', 'index.php?author=$matches[1]&post_type='.$post_type, 'top' ); |
| 100 | add_rewrite_rule( $slug.'/?$', 'index.php?post_type='.$post_type, 'top' ); |
| 101 | |
| 102 | if( $slug != $post_type ){ |
| 103 | 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' ); |
| 104 | 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' ); |
| 105 | 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' ); |
| 106 | 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' ); |
| 107 | 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' ); |
| 108 | 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' ); |
| 109 | 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' ); |
| 110 | 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' ); |
| 111 | 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' ); |
| 112 | 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' ); |
| 113 | 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' ); |
| 114 | add_rewrite_rule( $post_type.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' ); |
| 115 | add_rewrite_rule( $post_type.'/author/([0-9]{4})/?$', 'index.php?author=$matches[1]&post_type='.$post_type, 'top' ); |
| 116 | add_rewrite_rule( $post_type.'/?$', 'index.php?post_type='.$post_type, 'top' ); |
| 117 | } |
| 118 | endforeach; |
| 119 | } |
| 120 | |
| 121 | public function set_rewrite() { |
| 122 | global $wp_rewrite; |
| 123 | |
| 124 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) ); |
| 125 | foreach ( $post_types as $post_type ): |
| 126 | $permalink = get_option( $post_type.'_structure' ); |
| 127 | |
| 128 | if( !$permalink ) |
| 129 | $permalink = self::$default_structure; |
| 130 | |
| 131 | $permalink = str_replace( '%postname%', '%'.$post_type.'%', $permalink ); |
| 132 | $permalink = str_replace( '%post_id%', '%'.$post_type.'_id%', $permalink ); |
| 133 | |
| 134 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 135 | |
| 136 | if( !$slug ) |
| 137 | $slug = $post_type; |
| 138 | |
| 139 | $permalink = '/'.$slug.'/'.$permalink; |
| 140 | $permalink = $permalink.'/%'.$post_type.'_page%'; |
| 141 | $permalink = str_replace( '//', '/', $permalink ); |
| 142 | |
| 143 | $wp_rewrite->add_rewrite_tag( '%post_type%', '([^/]+)', 'post_type=' ); |
| 144 | $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=' ); |
| 145 | $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_page%', '/?([0-9]{1,}?)/?',"page=" ); |
| 146 | //test |
| 147 | if(is_post_type_hierarchical($post_type)) { |
| 148 | $wp_rewrite->add_rewrite_tag( '%'.$post_type.'%', '(?:[^/]+/){1}([^/]+)/?','name=' ); |
| 149 | } |
| 150 | $wp_rewrite->add_permastruct( $post_type, $permalink, false ); |
| 151 | |
| 152 | endforeach; |
| 153 | |
| 154 | $taxonomies = get_taxonomies( array("show_ui" => true, "_builtin" => false), 'objects' ); |
| 155 | foreach ( $taxonomies as $taxonomy => $objects ): |
| 156 | $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" ); |
| 157 | endforeach; |
| 158 | |
| 159 | $wp_rewrite->use_verbose_page_rules = true; |
| 160 | } |
| 161 | |
| 162 | public function set_permalink( $post_link, $post,$leavename ) { |
| 163 | global $wp_rewrite; |
| 164 | $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); |
| 165 | if( $draft_or_pending and !$leavename ) |
| 166 | return $post_link; |
| 167 | |
| 168 | $post_type = $post->post_type; |
| 169 | $permalink = $wp_rewrite->get_extra_permastruct( $post_type ); |
| 170 | |
| 171 | $permalink = str_replace( '%post_type%', $post->post_type, $permalink ); |
| 172 | $permalink = str_replace( '%'.$post_type.'_id%', $post->ID, $permalink ); |
| 173 | $permalink = str_replace( '%'.$post_type.'_page%', "", $permalink ); |
| 174 | |
| 175 | $parentsDirs = ""; |
| 176 | $postId = $post->ID; |
| 177 | while ($parent = get_post($postId)->post_parent) { |
| 178 | $parentsDirs = get_post($parent)->post_name."/".$parentsDirs; |
| 179 | $postId = $parent; |
| 180 | } |
| 181 | |
| 182 | $permalink = str_replace( '%'.$post_type.'%', $parentsDirs.'%'.$post_type.'%', $permalink ); |
| 183 | |
| 184 | |
| 185 | if( !$leavename ){ |
| 186 | |
| 187 | $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink ); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | $taxonomies = get_taxonomies( array('show_ui' => true),'objects' ); |
| 192 | |
| 193 | foreach ( $taxonomies as $taxonomy => $objects ) { |
| 194 | |
| 195 | if ( strpos($permalink, "%$taxonomy%") !== false ) { |
| 196 | $terms = get_the_terms( $post->ID, $taxonomy ); |
| 197 | |
| 198 | if ( $terms ) { |
| 199 | usort($terms, '_usort_terms_by_ID'); // order by ID |
| 200 | $term = $terms[0]->slug; |
| 201 | |
| 202 | if ( $parent = $terms[0]->parent ) |
| 203 | $term = $this->get_taxonomy_parents( $parent,$taxonomy, false, '/', true ) . $term; |
| 204 | } |
| 205 | |
| 206 | if(isset($term)) { |
| 207 | $permalink = str_replace( "%$taxonomy%", $term, $permalink ); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | $user = get_userdata( $post->post_author ); |
| 213 | $permalink = str_replace( "%author%", $user->user_nicename, $permalink ); |
| 214 | |
| 215 | $post_date = strtotime( $post->post_date ); |
| 216 | $permalink = str_replace( "%year%", date("Y",$post_date), $permalink ); |
| 217 | $permalink = str_replace( "%monthnum%", date("m",$post_date), $permalink ); |
| 218 | $permalink = str_replace( "%day%", date("d",$post_date), $permalink ); |
| 219 | $permalink = str_replace( "%hour%", date("H",$post_date), $permalink ); |
| 220 | $permalink = str_replace( "%minute%", date("i",$post_date), $permalink ); |
| 221 | $permalink = str_replace( "%second%", date("s",$post_date), $permalink ); |
| 222 | |
| 223 | $permalink = str_replace('//', "/", $permalink ); |
| 224 | |
| 225 | $permalink = home_url( user_trailingslashit( $permalink ) ); |
| 226 | $str = rtrim( preg_replace("/%[a-z,_]*%/","",get_option("permalink_structure")) ,'/'); |
| 227 | return $permalink = str_replace($str, "", $permalink ); |
| 228 | |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | *wp_get_archives fix for custom post |
| 233 | *Ex:wp_get_archives('&post_type='.get_query_var( 'post_type' )); |
| 234 | */ |
| 235 | |
| 236 | public $get_archives_where_r; |
| 237 | |
| 238 | public function get_archives_where( $where, $r ) { |
| 239 | $this->get_archives_where_r = $r; |
| 240 | |
| 241 | if ( isset($r['post_type']) ) |
| 242 | $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where ); |
| 243 | |
| 244 | return $where; |
| 245 | } |
| 246 | |
| 247 | public function get_archives_link( $link ) { |
| 248 | //$slug = get_post_type_object($this->get_archives_where_r['post_type'])->rewrite['slug']; |
| 249 | if (isset($this->get_archives_where_r['post_type']) and $this->get_archives_where_r['type'] != 'postbypost'){ |
| 250 | $blog_url = get_bloginfo("url"); |
| 251 | |
| 252 | |
| 253 | // /archive/%post_id% |
| 254 | if($str = rtrim( preg_replace("/%[a-z,_]*%/","",get_option("permalink_structure")) ,'/')) { |
| 255 | $ret_link = str_replace($str, '/'.'%link_dir%', $link); |
| 256 | }else{ |
| 257 | $blog_url = rtrim($blog_url,"/"); |
| 258 | $ret_link = str_replace($blog_url,$blog_url.'/'.'%link_dir%',$link); |
| 259 | } |
| 260 | $link_dir = $this->get_archives_where_r['post_type']; |
| 261 | |
| 262 | if(!strstr($link,'/date/')){ |
| 263 | $link_dir = $link_dir .'/date'; |
| 264 | } |
| 265 | |
| 266 | $ret_link = str_replace('%link_dir%',$link_dir,$ret_link); |
| 267 | |
| 268 | return $ret_link; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | return $link; |
| 273 | |
| 274 | |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
| 279 | /** |
| 280 | * fix permalink custom taxonomy |
| 281 | */ |
| 282 | public function add_tax_rewrite() { |
| 283 | if(get_option('no_taxonomy_structure')) |
| 284 | return false; |
| 285 | |
| 286 | global $wp_rewrite; |
| 287 | $taxonomies = get_taxonomies(array( '_builtin' => false)); |
| 288 | if(empty($taxonomies)) |
| 289 | return false; |
| 290 | |
| 291 | foreach ($taxonomies as $taxonomy) : |
| 292 | $post_types = get_taxonomy($taxonomy)->object_type; |
| 293 | |
| 294 | foreach ($post_types as $post_type): |
| 295 | $slug = get_post_type_object($post_type)->rewrite["slug"]; |
| 296 | //add taxonomy slug |
| 297 | add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/?$', 'index.php?'.$taxonomy.'=$matches[1]', 'top' ); |
| 298 | add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' ); |
| 299 | add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' ); |
| 300 | add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/page/?([0-9]{1,})/?$', 'index.php?'.$taxonomy.'=$matches[1]&paged=$matches[2]', 'top' ); |
| 301 | |
| 302 | if( $slug != $post_type ){ |
| 303 | add_rewrite_rule( $post_type.'/'.$taxonomy.'/(.+?)/?$', 'index.php?'.$taxonomy.'=$matches[1]', 'top' ); |
| 304 | add_rewrite_rule( $post_type.'/'.$taxonomy.'/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' ); |
| 305 | add_rewrite_rule( $post_type.'/'.$taxonomy.'/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' ); |
| 306 | add_rewrite_rule( $post_type.'/'.$taxonomy.'/(.+?)/page/?([0-9]{1,})/?$', 'index.php?'.$taxonomy.'=$matches[1]&paged=$matches[2]', 'top' ); |
| 307 | } |
| 308 | |
| 309 | endforeach; |
| 310 | |
| 311 | endforeach; |
| 312 | } |
| 313 | |
| 314 | public function set_term_link( $termlink, $term, $taxonomy ) { |
| 315 | if( get_option('no_taxonomy_structure') ) |
| 316 | return $termlink; |
| 317 | |
| 318 | $taxonomy = get_taxonomy($taxonomy); |
| 319 | if( $taxonomy->_builtin ) |
| 320 | return $termlink; |
| 321 | |
| 322 | if( empty($taxonomy) ) |
| 323 | return $termlink; |
| 324 | |
| 325 | $wp_home = rtrim( get_option('home'), '/' ); |
| 326 | |
| 327 | $post_type = $taxonomy->object_type[0]; |
| 328 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 329 | |
| 330 | |
| 331 | //$termlink = str_replace( $term->slug.'/', $this->get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink ); |
| 332 | $termlink = str_replace( $wp_home, $wp_home.'/'.$slug, $termlink ); |
| 333 | $str = rtrim( preg_replace("/%[a-z_]*%/","",get_option("permalink_structure")) ,'/'); |
| 334 | return str_replace($str, "", $termlink ); |
| 335 | |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | class Custom_Post_Type_Permalinks_Admin { |
| 340 | |
| 341 | public function __construct () { |
| 342 | add_action('init', array(&$this,'load_textdomain')); |
| 343 | add_action('admin_init', array(&$this,'settings_api_init'),30); |
| 344 | } |
| 345 | |
| 346 | public function load_textdomain() { |
| 347 | load_plugin_textdomain('cptp',false,'custom-post-type-permalinks'); |
| 348 | } |
| 349 | |
| 350 | public function settings_api_init() { |
| 351 | add_settings_section('cptp_setting_section', |
| 352 | __("Permalink Setting for custom post type",'cptp'), |
| 353 | array(&$this,'setting_section_callback_function'), |
| 354 | 'permalink' |
| 355 | ); |
| 356 | |
| 357 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) ); |
| 358 | foreach ($post_types as $post_type): |
| 359 | if(isset($_POST['submit'])){ |
| 360 | if( strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) { |
| 361 | |
| 362 | $structure = trim(esc_attr($_POST[$post_type.'_structure']));#get setting |
| 363 | |
| 364 | #default permalink structure |
| 365 | if( !$structure ) |
| 366 | $structure = Custom_Post_Type_Permalinks::$default_structure; |
| 367 | |
| 368 | $structure = str_replace('//','/','/'.$structure);# first "/" |
| 369 | |
| 370 | #last "/" |
| 371 | $lastString = substr(trim(esc_attr($_POST['permalink_structure'])),-1); |
| 372 | $structure = rtrim($structure,'/'); |
| 373 | |
| 374 | if ( $lastString == '/') |
| 375 | $structure = $structure.'/'; |
| 376 | |
| 377 | update_option($post_type.'_structure', $structure ); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | add_settings_field($post_type.'_structure', |
| 382 | $post_type, |
| 383 | array(&$this,'setting_structure_callback_function'), |
| 384 | 'permalink', |
| 385 | 'cptp_setting_section', |
| 386 | $post_type.'_structure' |
| 387 | ); |
| 388 | |
| 389 | register_setting('permalink',$post_type.'_structure'); |
| 390 | endforeach; |
| 391 | |
| 392 | add_settings_field( |
| 393 | 'no_taxonomy_structure', |
| 394 | __("Use custom permalink of custom taxonomy archive.",'cptp'), |
| 395 | array(&$this,'setting_no_tax_structure_callback_function'), |
| 396 | 'permalink', |
| 397 | 'cptp_setting_section' |
| 398 | ); |
| 399 | |
| 400 | register_setting('permalink','no_taxonomy_structure'); |
| 401 | |
| 402 | if(isset($_POST['submit']) && isset($_POST['_wp_http_referer']) && strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) { |
| 403 | |
| 404 | if(!isset($_POST['no_taxonomy_structure'])){ |
| 405 | $set = true; |
| 406 | }else { |
| 407 | $set = false; |
| 408 | } |
| 409 | update_option('no_taxonomy_structure', $set); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | public function setting_section_callback_function() { |
| 414 | ?> |
| 415 | <p><?php _e("Setting permalinks of custom post type.",'cptp');?><br /> |
| 416 | <?php _e("The tags you can use is WordPress Structure Tags and '%{custom_taxonomy_slug}%'.",'cptp');?><br /> |
| 417 | <?php _e("%{custom_taxonomy_slug}% is replaced the taxonomy's term.'.",'cptp');?></p> |
| 418 | |
| 419 | <p><?php _e("Presence of the trailing '/' is unified into a standard permalink structure setting.",'cptp');?><br /> |
| 420 | <?php _e("If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/.",'cptp');?> |
| 421 | </p> |
| 422 | <?php |
| 423 | } |
| 424 | |
| 425 | public function setting_structure_callback_function( $option ) { |
| 426 | $post_type = str_replace('_structure',"" ,$option); |
| 427 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 428 | if( !$slug ) |
| 429 | $slug = $post_type; |
| 430 | |
| 431 | echo '/'.$slug.' <input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />'; |
| 432 | } |
| 433 | |
| 434 | public function setting_no_tax_structure_callback_function(){ |
| 435 | echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked( false, get_option('no_taxonomy_structure'),false) . ' /> '; |
| 436 | _e("If you check,The custom taxonomy's permalinks is example.com/post_type/taxonomy/term.","cptp"); |
| 437 | } |
| 438 | |
| 439 | } |
| 440 | |
| 441 | new Custom_Post_Type_Permalinks; |
| 442 | new Custom_Post_Type_Permalinks_Admin; |
| 443 |