Plugin Directory

Changeset 1062911


Ignore:
Timestamp:
01/08/2015 09:29:54 AM (11 years ago)
Author:
michaeltyson
Message:

Converted tabs to spaces

File:
1 edited

Legend:

Unmodified
Added
Removed
  • custom-permalinks/trunk/custom-permalinks.php

    r1062908 r1062911  
    4040 */
    4141function custom_permalinks_post_link($permalink, $post) {
    42     $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
    43     if ( $custom_permalink ) {
    44         return home_url()."/".$custom_permalink;
    45     }
    46    
    47     return $permalink;
     42  $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
     43  if ( $custom_permalink ) {
     44    return home_url()."/".$custom_permalink;
     45  }
     46 
     47  return $permalink;
    4848}
    4949
     
    5656 */
    5757function custom_permalinks_page_link($permalink, $page) {
    58     $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
    59     if ( $custom_permalink ) {
    60         return home_url()."/".$custom_permalink;
    61     }
    62    
    63     return $permalink;
     58  $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
     59  if ( $custom_permalink ) {
     60    return home_url()."/".$custom_permalink;
     61  }
     62 
     63  return $permalink;
    6464}
    6565
     
    7272 */
    7373function custom_permalinks_term_link($permalink, $term) {
    74     $table = get_option('custom_permalink_table');
    75     if ( is_object($term) ) $term = $term->term_id;
    76    
    77     $custom_permalink = custom_permalinks_permalink_for_term($term);
    78    
    79     if ( $custom_permalink ) {
    80         return home_url()."/".$custom_permalink;
    81     }
    82    
    83     return $permalink;
     74  $table = get_option('custom_permalink_table');
     75  if ( is_object($term) ) $term = $term->term_id;
     76 
     77  $custom_permalink = custom_permalinks_permalink_for_term($term);
     78 
     79  if ( $custom_permalink ) {
     80    return home_url()."/".$custom_permalink;
     81  }
     82 
     83  return $permalink;
    8484}
    8585
     
    9292 */
    9393function custom_permalinks_redirect() {
    94    
    95     // Get request URI, strip parameters
    96     $url = parse_url(get_bloginfo('url'));
    97     $url = isset($url['path']) ? $url['path'] : '';
    98     $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
    99     if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
    100    
    101     global $wp_query;
    102    
    103     $custom_permalink = '';
    104     $original_permalink = '';
    105 
    106     // If the post/tag/category we're on has a custom permalink, get it and check against the request
    107     if ( is_single() || is_page() ) {
    108         $post = $wp_query->post;
    109         $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
    110         $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) );
    111     } else if ( is_tag() || is_category() ) {
    112         $theTerm = $wp_query->get_queried_object();
    113         $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
    114         $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) :
    115                                           custom_permalinks_original_category_link($theTerm->term_id));
    116     }
    117 
    118     if ( $custom_permalink &&
    119             (substr($request, 0, strlen($custom_permalink)) != $custom_permalink ||
    120             $request == $custom_permalink."/" ) ) {
    121         // Request doesn't match permalink - redirect
    122         $url = $custom_permalink;
    123 
    124         if ( substr($request, 0, strlen($original_permalink)) == $original_permalink &&
    125                 trim($request,'/') != trim($original_permalink,'/') ) {
    126             // This is the original link; we can use this url to derive the new one
    127             $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
    128             $url = preg_replace('@([^?]*)&@', '\1?', $url);
    129         }
    130        
    131         // Append any query compenent
    132         $url .= strstr($_SERVER['REQUEST_URI'], "?");
    133        
    134         wp_redirect( home_url()."/".$url, 301 );
    135         exit();
    136     }   
     94 
     95  // Get request URI, strip parameters
     96  $url = parse_url(get_bloginfo('url'));
     97  $url = isset($url['path']) ? $url['path'] : '';
     98  $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
     99  if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
     100 
     101  global $wp_query;
     102 
     103  $custom_permalink = '';
     104  $original_permalink = '';
     105
     106  // If the post/tag/category we're on has a custom permalink, get it and check against the request
     107  if ( is_single() || is_page() ) {
     108    $post = $wp_query->post;
     109    $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
     110    $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) );
     111  } else if ( is_tag() || is_category() ) {
     112    $theTerm = $wp_query->get_queried_object();
     113    $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
     114    $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) :
     115                        custom_permalinks_original_category_link($theTerm->term_id));
     116  }
     117
     118  if ( $custom_permalink &&
     119      (substr($request, 0, strlen($custom_permalink)) != $custom_permalink ||
     120      $request == $custom_permalink."/" ) ) {
     121    // Request doesn't match permalink - redirect
     122    $url = $custom_permalink;
     123
     124    if ( substr($request, 0, strlen($original_permalink)) == $original_permalink &&
     125        trim($request,'/') != trim($original_permalink,'/') ) {
     126      // This is the original link; we can use this url to derive the new one
     127      $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
     128      $url = preg_replace('@([^?]*)&@', '\1?', $url);
     129    }
     130   
     131    // Append any query compenent
     132    $url .= strstr($_SERVER['REQUEST_URI'], "?");
     133   
     134    wp_redirect( home_url()."/".$url, 301 );
     135    exit();
     136  }
    137137}
    138138
     
    144144 */
    145145function custom_permalinks_request($query) {
    146     global $wpdb;
    147     global $_CPRegisteredURL;
    148    
    149     // First, search for a matching custom permalink, and if found, generate the corresponding
    150     // original URL
    151    
    152     $originalUrl = NULL;
    153    
    154     // Get request URI, strip parameters and /'s
    155     $url = parse_url(get_bloginfo('url'));
    156     $url = isset($url['path']) ? $url['path'] : '';
    157     $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
    158     $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
    159     $request_noslash = preg_replace('@/+@','/', trim($request, '/'));
    160 
    161     if ( !$request ) return $query;
    162    
    163     // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
     146  global $wpdb;
     147  global $_CPRegisteredURL;
     148 
     149  // First, search for a matching custom permalink, and if found, generate the corresponding
     150  // original URL
     151 
     152  $originalUrl = NULL;
     153 
     154  // Get request URI, strip parameters and /'s
     155  $url = parse_url(get_bloginfo('url'));
     156  $url = isset($url['path']) ? $url['path'] : '';
     157  $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
     158  $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
     159  $request_noslash = preg_replace('@/+@','/', trim($request, '/'));
     160
     161  if ( !$request ) return $query;
     162 
     163  // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
    164164    $sql = $wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts  ".
    165                 "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
    166                 "  meta_key = 'custom_permalink' AND ".
    167                 "  meta_value != '' AND ".
    168                 "  ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR ".
    169                 "    LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) ".
    170                 "  AND post_status != 'trash' AND post_type != 'nav_menu_item'".
    171                 " ORDER BY LENGTH(meta_value) DESC, ".
    172                 " FIELD(post_status,'publish','private','draft','auto-draft','inherit'),".
    173                 " FIELD(post_type,'post','page'),".
    174                 "$wpdb->posts.ID ASC  LIMIT 1",
    175             $request_noslash,
    176             $request_noslash."/"
    177             );
    178 
    179     $posts = $wpdb->get_results($sql);
    180 
    181     if ( $posts ) {
    182         // A post matches our request
    183        
    184         // Preserve this url for later if it's the same as the permalink (no extra stuff)
    185         if ( $request_noslash == trim($posts[0]->meta_value,'/') )
    186             $_CPRegisteredURL = $request;
    187                
    188         $originalUrl =  preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ),
    189                                     ( $posts[0]->post_type == 'page' ?
    190                                             custom_permalinks_original_page_link($posts[0]->ID)
    191                                             : custom_permalinks_original_post_link($posts[0]->ID) ),
    192                                    strtolower($request_noslash) ) );
    193     }
    194 
    195     if ( $originalUrl === NULL ) {
    196         // See if any terms have a matching permalink
    197         $table = get_option('custom_permalink_table');
    198         if ( !$table ) return $query;
    199    
    200         foreach ( array_keys($table) as $permalink ) {
    201             if ( $permalink == substr($request_noslash, 0, strlen($permalink)) ||
    202                  $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) {
    203                 $term = $table[$permalink];
    204                
    205                 // Preserve this url for later if it's the same as the permalink (no extra stuff)
    206                 if ( $request_noslash == trim($permalink,'/') )
    207                     $_CPRegisteredURL = $request;
    208                
    209                
    210                 if ( $term['kind'] == 'category') {
    211                     $originalUrl = str_replace(trim($permalink,'/'),
    212                                                custom_permalinks_original_category_link($term['id']),
    213                                                trim($request,'/'));
    214                 } else {
    215                     $originalUrl = str_replace(trim($permalink,'/'),
    216                                                custom_permalinks_original_tag_link($term['id']),
    217                                                trim($request,'/'));
    218                 }
    219             }
    220         }
    221     }
    222        
    223     if ( $originalUrl !== NULL ) {
    224         $originalUrl = str_replace('//', '/', $originalUrl);
    225        
    226         if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) {
    227             $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1);
    228             $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars;
    229         }
    230        
    231         // Now we have the original URL, run this back through WP->parse_request, in order to
    232         // parse parameters properly.  We set $_SERVER variables to fool the function.
    233         $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING'];
    234         $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/');
    235         $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : '');
    236         parse_str($_SERVER['QUERY_STRING'], $queryArray);
    237         $oldValues = array();
    238         if ( is_array($queryArray) )
    239         foreach ( $queryArray as $key => $value ) {
    240             $oldValues[$key] = $_REQUEST[$key];
    241             $_REQUEST[$key] = $_GET[$key] = $value;
    242         }
    243 
    244         // Re-run the filter, now with original environment in place
    245         remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
    246         global $wp;
    247         $wp->parse_request();
    248         $query = $wp->query_vars;
    249         add_filter( 'request', 'custom_permalinks_request', 10, 1 );
    250        
    251         // Restore values
    252         $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString;
    253         foreach ( $oldValues as $key => $value ) {
    254             $_REQUEST[$key] = $value;
    255         }
    256     }
    257 
    258     return $query;
     165              "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
     166              "  meta_key = 'custom_permalink' AND ".
     167              "  meta_value != '' AND ".
     168              "  ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR ".
     169              "    LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) ".
     170              "  AND post_status != 'trash' AND post_type != 'nav_menu_item'".
     171              " ORDER BY LENGTH(meta_value) DESC, ".
     172              " FIELD(post_status,'publish','private','draft','auto-draft','inherit'),".
     173              " FIELD(post_type,'post','page'),".
     174              "$wpdb->posts.ID ASC  LIMIT 1",
     175      $request_noslash,
     176      $request_noslash."/"
     177        );
     178
     179  $posts = $wpdb->get_results($sql);
     180
     181  if ( $posts ) {
     182    // A post matches our request
     183   
     184    // Preserve this url for later if it's the same as the permalink (no extra stuff)
     185    if ( $request_noslash == trim($posts[0]->meta_value,'/') )
     186      $_CPRegisteredURL = $request;
     187       
     188    $originalUrl =  preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ),
     189                  ( $posts[0]->post_type == 'page' ?
     190                      custom_permalinks_original_page_link($posts[0]->ID)
     191                      : custom_permalinks_original_post_link($posts[0]->ID) ),
     192                   strtolower($request_noslash) ) );
     193  }
     194
     195  if ( $originalUrl === NULL ) {
     196      // See if any terms have a matching permalink
     197    $table = get_option('custom_permalink_table');
     198    if ( !$table ) return $query;
     199 
     200    foreach ( array_keys($table) as $permalink ) {
     201      if ( $permalink == substr($request_noslash, 0, strlen($permalink)) ||
     202           $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) {
     203        $term = $table[$permalink];
     204       
     205        // Preserve this url for later if it's the same as the permalink (no extra stuff)
     206        if ( $request_noslash == trim($permalink,'/') )
     207          $_CPRegisteredURL = $request;
     208       
     209       
     210        if ( $term['kind'] == 'category') {
     211          $originalUrl = str_replace(trim($permalink,'/'),
     212                           custom_permalinks_original_category_link($term['id']),
     213                         trim($request,'/'));
     214        } else {
     215          $originalUrl = str_replace(trim($permalink,'/'),
     216                           custom_permalinks_original_tag_link($term['id']),
     217                         trim($request,'/'));
     218        }
     219      }
     220    }
     221  }
     222   
     223  if ( $originalUrl !== NULL ) {
     224    $originalUrl = str_replace('//', '/', $originalUrl);
     225   
     226    if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) {
     227      $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1);
     228      $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars;
     229    }
     230   
     231    // Now we have the original URL, run this back through WP->parse_request, in order to
     232    // parse parameters properly.  We set $_SERVER variables to fool the function.
     233    $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING'];
     234    $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/');
     235    $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : '');
     236    parse_str($_SERVER['QUERY_STRING'], $queryArray);
     237    $oldValues = array();
     238    if ( is_array($queryArray) )
     239    foreach ( $queryArray as $key => $value ) {
     240      $oldValues[$key] = $_REQUEST[$key];
     241      $_REQUEST[$key] = $_GET[$key] = $value;
     242    }
     243
     244    // Re-run the filter, now with original environment in place
     245    remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
     246    global $wp;
     247    $wp->parse_request();
     248    $query = $wp->query_vars;
     249    add_filter( 'request', 'custom_permalinks_request', 10, 1 );
     250   
     251    // Restore values
     252    $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString;
     253    foreach ( $oldValues as $key => $value ) {
     254      $_REQUEST[$key] = $value;
     255    }
     256  }
     257
     258  return $query;
    259259}
    260260
     
    266266 */
    267267function custom_permalinks_trailingslash($string, $type) {     
    268     global $_CPRegisteredURL;
    269 
    270     $url = parse_url(get_bloginfo('url'));
    271     $request = ltrim(isset($url['path']) ? substr($string, strlen($url['path'])) : $string, '/');
    272 
    273     if ( !trim($request) ) return $string;
    274 
    275     if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
    276         return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
    277     }
    278     return $string;
     268  global $_CPRegisteredURL;
     269
     270  $url = parse_url(get_bloginfo('url'));
     271  $request = ltrim(isset($url['path']) ? substr($string, strlen($url['path'])) : $string, '/');
     272
     273  if ( !trim($request) ) return $string;
     274
     275  if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
     276    return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
     277  }
     278  return $string;
    279279}
    280280
     
    292292function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
    293293    $permalink = get_post_meta( $id, 'custom_permalink', true );
    294     $post = &get_post($id);
    295    
    296     ob_start();
    297     ?>
    298     <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
    299     <?php
    300     $content = ob_get_contents();
    301     ob_end_clean();
     294  $post = &get_post($id);
     295 
     296  ob_start();
     297  ?>
     298  <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
     299  <?php
     300  $content = ob_get_contents();
     301  ob_end_clean();
    302302   
    303303    if ( 'publish' == $post->post_status ) {
    304304        $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post');
    305     }
    306    
    307     if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
    308         $permalink = $matches[1];
     305  }
     306 
     307  if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
     308      $permalink = $matches[1];
    309309    } else {
    310310        list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
     
    313313        }
    314314    }
    315    
    316     return '<strong>' . __('Permalink:') . "</strong>\n" . $content .
    317          ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n" : "" );
     315 
     316  return '<strong>' . __('Permalink:') . "</strong>\n" . $content .
     317       ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n" : "" );
    318318}
    319319
     
    326326 */
    327327function custom_permalinks_post_options() {
    328     global $post;
    329     $post_id = $post;
    330     if (is_object($post_id)) {
    331         $post_id = $post_id->ID;
    332     }
    333    
    334     $permalink = get_post_meta( $post_id, 'custom_permalink', true );
    335    
    336     ?>
    337     <div class="postbox closed">
    338     <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
    339     <div class="inside">
    340     <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
    341     </div>
    342     </div>
    343     <?php
     328  global $post;
     329  $post_id = $post;
     330  if (is_object($post_id)) {
     331    $post_id = $post_id->ID;
     332  }
     333 
     334  $permalink = get_post_meta( $post_id, 'custom_permalink', true );
     335 
     336  ?>
     337  <div class="postbox closed">
     338  <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
     339  <div class="inside">
     340  <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
     341  </div>
     342  </div>
     343  <?php
    344344}
    345345
     
    352352 */
    353353function custom_permalinks_page_options() {
    354     global $post;
    355     $post_id = $post;
    356     if (is_object($post_id)) {
    357         $post_id = $post_id->ID;
    358     }
    359    
    360     $permalink = get_post_meta( $post_id, 'custom_permalink', true );
    361    
    362     ?>
    363     <div class="postbox closed">
    364     <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
    365     <div class="inside">
    366     <?php custom_permalinks_form($permalink, custom_permalinks_original_page_link($post_id)); ?>
    367     </div>
    368     </div>
    369     <?php
     354  global $post;
     355  $post_id = $post;
     356  if (is_object($post_id)) {
     357    $post_id = $post_id->ID;
     358  }
     359 
     360  $permalink = get_post_meta( $post_id, 'custom_permalink', true );
     361 
     362  ?>
     363  <div class="postbox closed">
     364  <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
     365  <div class="inside">
     366  <?php custom_permalinks_form($permalink, custom_permalinks_original_page_link($post_id)); ?>
     367  </div>
     368  </div>
     369  <?php
    370370}
    371371
     
    378378 */
    379379function custom_permalinks_term_options($object) {
    380     $permalink = custom_permalinks_permalink_for_term($object->term_id);
    381    
    382     if ( $object->term_id ) {
    383         $originalPermalink = ($object->taxonomy == 'post_tag' ?
    384                                     custom_permalinks_original_tag_link($object->term_id) :
    385                                     custom_permalinks_original_category_link($object->term_id) );
    386     }
    387        
    388     custom_permalinks_form($permalink, $originalPermalink);
    389 
    390     // Move the save button to above this form
    391     wp_enqueue_script('jquery');
    392     ?>
    393     <script type="text/javascript">
    394     jQuery(document).ready(function() {
    395         var button = jQuery('#custom_permalink_form').parent().find('.submit');
    396         button.remove().insertAfter(jQuery('#custom_permalink_form'));
    397     });
    398     </script>
    399     <?php
     380  $permalink = custom_permalinks_permalink_for_term($object->term_id);
     381 
     382  if ( $object->term_id ) {
     383      $originalPermalink = ($object->taxonomy == 'post_tag' ?
     384                    custom_permalinks_original_tag_link($object->term_id) :
     385                    custom_permalinks_original_category_link($object->term_id) );
     386    }
     387     
     388  custom_permalinks_form($permalink, $originalPermalink);
     389
     390  // Move the save button to above this form
     391  wp_enqueue_script('jquery');
     392  ?>
     393  <script type="text/javascript">
     394  jQuery(document).ready(function() {
     395    var button = jQuery('#custom_permalink_form').parent().find('.submit');
     396    button.remove().insertAfter(jQuery('#custom_permalink_form'));
     397  });
     398  </script>
     399  <?php
    400400}
    401401
     
    407407 */
    408408function custom_permalinks_form($permalink, $original="", $renderContainers=true) {
    409     ?>
    410     <input value="true" type="hidden" name="custom_permalinks_edit" />
    411     <input value="<?php echo htmlspecialchars(urldecode($permalink)) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
    412    
    413     <?php if ( $renderContainers ) : ?>
    414     <table class="form-table" id="custom_permalink_form">
    415     <tr>
    416         <th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th>
    417         <td>
    418     <?php endif; ?>
    419             <?php echo home_url() ?>/
    420             <input type="text" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
    421                 style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
    422                 onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
    423                 onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' || this.value == '<?php echo htmlspecialchars(urldecode($original)) ?>' ) { this.value = '<?php echo htmlspecialchars(urldecode($original)) ?>'; this.style.color = '#ddd'; }"/>
    424     <?php if ( $renderContainers ) : ?>             
    425             <br />
    426             <small><?php _e('Leave blank to disable', 'custom-permalink') ?></small>
    427            
    428         </td>
    429     </tr>
    430     </table>
    431     <?php
    432     endif;
     409  ?>
     410  <input value="true" type="hidden" name="custom_permalinks_edit" />
     411  <input value="<?php echo htmlspecialchars(urldecode($permalink)) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
     412 
     413  <?php if ( $renderContainers ) : ?>
     414  <table class="form-table" id="custom_permalink_form">
     415  <tr>
     416    <th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th>
     417    <td>
     418  <?php endif; ?>
     419      <?php echo home_url() ?>/
     420      <input type="text" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
     421        style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
     422        onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
     423        onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' || this.value == '<?php echo htmlspecialchars(urldecode($original)) ?>' ) { this.value = '<?php echo htmlspecialchars(urldecode($original)) ?>'; this.style.color = '#ddd'; }"/>
     424  <?php if ( $renderContainers ) : ?>       
     425      <br />
     426      <small><?php _e('Leave blank to disable', 'custom-permalink') ?></small>
     427     
     428    </td>
     429  </tr>
     430  </table>
     431  <?php
     432  endif;
    433433}
    434434
     
    441441 */
    442442function custom_permalinks_save_post($id) {
    443     if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
    444    
    445     delete_post_meta( $id, 'custom_permalink' );
    446    
    447     $original_link = custom_permalinks_original_post_link($id);
    448     $permalink_structure = get_option('permalink_structure');
    449    
    450     if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != $original_link ) {
    451         add_post_meta( $id, 'custom_permalink', str_replace('%2F', '/', urlencode(ltrim(stripcslashes($_REQUEST['custom_permalink']),"/"))) );
    452     }
     443  if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
     444 
     445  delete_post_meta( $id, 'custom_permalink' );
     446 
     447  $original_link = custom_permalinks_original_post_link($id);
     448  $permalink_structure = get_option('permalink_structure');
     449 
     450  if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != $original_link ) {
     451      add_post_meta( $id, 'custom_permalink', str_replace('%2F', '/', urlencode(ltrim(stripcslashes($_REQUEST['custom_permalink']),"/"))) );
     452  }
    453453}
    454454
     
    461461 */
    462462function custom_permalinks_save_tag($id) {
    463     if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
    464     $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
    465    
    466     if ( $newPermalink == custom_permalinks_original_tag_link($id) )
    467         $newPermalink = '';
    468    
    469     $term = get_term($id, 'post_tag');
    470     custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
     463  if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
     464  $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
     465 
     466  if ( $newPermalink == custom_permalinks_original_tag_link($id) )
     467    $newPermalink = '';
     468 
     469  $term = get_term($id, 'post_tag');
     470  custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
    471471}
    472472
     
    478478 */
    479479function custom_permalinks_save_category($id) {
    480     if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
    481     $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
    482    
    483     if ( $newPermalink == custom_permalinks_original_category_link($id) )
    484         $newPermalink = '';
    485    
    486     $term = get_term($id, 'category');
    487     custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
     480  if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
     481  $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
     482 
     483  if ( $newPermalink == custom_permalinks_original_category_link($id) )
     484    $newPermalink = '';
     485 
     486  $term = get_term($id, 'category');
     487  custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
    488488}
    489489
     
    495495 */
    496496function custom_permalinks_save_term($term, $permalink) {
    497    
    498     custom_permalinks_delete_term($term->term_id);
    499     $table = get_option('custom_permalink_table');
    500     if ( $permalink )
    501         $table[$permalink] = array(
    502             'id' => $term->term_id,
    503             'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'),
    504             'slug' => $term->slug);
    505 
    506     update_option('custom_permalink_table', $table);
     497 
     498  custom_permalinks_delete_term($term->term_id);
     499  $table = get_option('custom_permalink_table');
     500  if ( $permalink )
     501    $table[$permalink] = array(
     502      'id' => $term->term_id,
     503      'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'),
     504      'slug' => $term->slug);
     505
     506  update_option('custom_permalink_table', $table);
    507507}
    508508
     
    515515 */
    516516function custom_permalinks_delete_permalink( $id ){
    517     global $wpdb;
    518     // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
    519     $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE `meta_key` = 'custom_permalink' AND `post_id` = %d",$id));
     517  global $wpdb;
     518  // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
     519  $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE `meta_key` = 'custom_permalink' AND `post_id` = %d",$id));
    520520}
    521521
     
    527527 */
    528528function custom_permalinks_delete_term($id) {
    529    
    530     $table = get_option('custom_permalink_table');
    531     if ( $table )
    532     foreach ( $table as $link => $info ) {
    533         if ( $info['id'] == $id ) {
    534             unset($table[$link]);
    535             break;
    536         }
    537     }
    538    
    539     update_option('custom_permalink_table', $table);
     529 
     530  $table = get_option('custom_permalink_table');
     531  if ( $table )
     532  foreach ( $table as $link => $info ) {
     533    if ( $info['id'] == $id ) {
     534      unset($table[$link]);
     535      break;
     536    }
     537  }
     538 
     539  update_option('custom_permalink_table', $table);
    540540}
    541541
     
    547547 */
    548548function custom_permalinks_options_page() {
    549    
    550     // Handle revert
    551     if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) {
    552         check_admin_referer('custom-permalinks-bulk');
    553         foreach ( (array)$_REQUEST['revert'] as $identifier ) {
    554             list($kind, $id) = explode('.', $identifier);
    555             switch ( $kind ) {
    556                 case 'post':
    557                 case 'page':
    558                     delete_post_meta( $id, 'custom_permalink' );
    559                     break;
    560                 case 'tag':
    561                 case 'category':
    562                     custom_permalinks_delete_term($id);
    563                     break;
    564             }
    565         }
    566        
    567         // Redirect
    568         $redirectUrl = $_SERVER['REQUEST_URI'];
    569         ?>
    570         <script type="text/javascript">
    571         document.location = '<?php echo $redirectUrl ?>'
    572         </script>
    573         <?php ;
    574     }
    575    
    576     ?>
    577     <div class="wrap">
    578     <h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2>
    579    
    580     <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
    581     <?php wp_nonce_field('custom-permalinks-bulk') ?>
    582    
    583     <div class="tablenav">
    584     <div class="alignleft">
    585     <input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" />
    586     </div>
    587     <br class="clear" />
    588     </div>
    589     <br class="clear" />
    590     <table class="widefat">
    591         <thead>
    592         <tr>
    593             <th scope="col" class="check-column"><input type="checkbox" /></th>
    594             <th scope="col"><?php _e('Title', 'custom-permalinks') ?></th>
    595             <th scope="col"><?php _e('Type', 'custom-permalinks') ?></th>
    596             <th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th>
    597         </tr>
    598         </thead>
    599         <tbody>
    600     <?php
    601     $rows = custom_permalinks_admin_rows();
    602     foreach ( $rows as $row ) {
    603         ?>
    604         <tr valign="top">
    605         <th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th>
    606         <td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td>
    607         <td><?php echo htmlspecialchars($row['type']) ?></td>
    608         <td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="Visit <?php echo htmlspecialchars($row['title']) ?>">
    609             <?php echo htmlspecialchars(urldecode($row['permalink'])) ?>
    610             </a>
    611         </td>
    612         </tr>
    613         <?php
    614     }
    615     ?>
    616     </tbody>
    617     </table>
    618     </form>
    619     </div>
    620     <?php
     549 
     550  // Handle revert
     551  if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) {
     552    check_admin_referer('custom-permalinks-bulk');
     553    foreach ( (array)$_REQUEST['revert'] as $identifier ) {
     554      list($kind, $id) = explode('.', $identifier);
     555      switch ( $kind ) {
     556        case 'post':
     557        case 'page':
     558          delete_post_meta( $id, 'custom_permalink' );
     559          break;
     560        case 'tag':
     561        case 'category':
     562          custom_permalinks_delete_term($id);
     563          break;
     564      }
     565    }
     566   
     567    // Redirect
     568    $redirectUrl = $_SERVER['REQUEST_URI'];
     569    ?>
     570    <script type="text/javascript">
     571    document.location = '<?php echo $redirectUrl ?>'
     572    </script>
     573    <?php ;
     574  }
     575 
     576  ?>
     577  <div class="wrap">
     578  <h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2>
     579 
     580  <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
     581  <?php wp_nonce_field('custom-permalinks-bulk') ?>
     582 
     583  <div class="tablenav">
     584  <div class="alignleft">
     585  <input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" />
     586  </div>
     587  <br class="clear" />
     588  </div>
     589  <br class="clear" />
     590  <table class="widefat">
     591    <thead>
     592    <tr>
     593      <th scope="col" class="check-column"><input type="checkbox" /></th>
     594      <th scope="col"><?php _e('Title', 'custom-permalinks') ?></th>
     595      <th scope="col"><?php _e('Type', 'custom-permalinks') ?></th>
     596      <th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th>
     597    </tr>
     598    </thead>
     599    <tbody>
     600  <?php
     601  $rows = custom_permalinks_admin_rows();
     602  foreach ( $rows as $row ) {
     603    ?>
     604    <tr valign="top">
     605    <th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th>
     606    <td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td>
     607    <td><?php echo htmlspecialchars($row['type']) ?></td>
     608    <td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="Visit <?php echo htmlspecialchars($row['title']) ?>">
     609      <?php echo htmlspecialchars(urldecode($row['permalink'])) ?>
     610      </a>
     611    </td>
     612    </tr>
     613    <?php
     614  }
     615  ?>
     616  </tbody>
     617  </table>
     618  </form>
     619  </div>
     620  <?php
    621621}
    622622
     
    628628 */
    629629function custom_permalinks_admin_rows() {
    630     $rows = array();
    631    
    632     // List tags/categories
    633     $table = get_option('custom_permalink_table');
    634     if ( $table && is_array($table) ) {
    635         foreach ( $table as $permalink => $info ) {
    636             $row = array();
    637             $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category'));
    638             $row['id'] = $info['kind'].'.'.$info['id'];
    639             $row['permalink'] = home_url()."/".$permalink;
    640             $row['type'] = ucwords($info['kind']);
    641             $row['title'] = $term->name;
    642             $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$info['id'] : 'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$info['id'] );
    643             $rows[] = $row;
    644         }
    645     }
    646    
    647     // List posts/pages
    648     global $wpdb;
    649     $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE
    650             $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';";
    651     $posts = $wpdb->get_results($query);
    652     foreach ( $posts as $post ) {
    653         $row = array();
    654         $row['id'] = 'post.'.$post->ID;
    655         $row['permalink'] = get_permalink($post->ID);
    656         $row['type'] = ucwords( $post->post_type );
    657         $row['title'] = $post->post_title;
    658         $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
    659         $rows[] = $row;
    660     }
    661    
    662     return $rows;
     630  $rows = array();
     631 
     632  // List tags/categories
     633  $table = get_option('custom_permalink_table');
     634  if ( $table && is_array($table) ) {
     635    foreach ( $table as $permalink => $info ) {
     636      $row = array();
     637      $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category'));
     638      $row['id'] = $info['kind'].'.'.$info['id'];
     639      $row['permalink'] = home_url()."/".$permalink;
     640      $row['type'] = ucwords($info['kind']);
     641      $row['title'] = $term->name;
     642      $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$info['id'] : 'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$info['id'] );
     643      $rows[] = $row;
     644    }
     645  }
     646 
     647  // List posts/pages
     648  global $wpdb;
     649  $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE
     650      $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';";
     651  $posts = $wpdb->get_results($query);
     652  foreach ( $posts as $post ) {
     653    $row = array();
     654    $row['id'] = 'post.'.$post->ID;
     655    $row['permalink'] = get_permalink($post->ID);
     656    $row['type'] = ucwords( $post->post_type );
     657    $row['title'] = $post->post_title;
     658    $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
     659    $rows[] = $row;
     660  }
     661 
     662  return $rows;
    663663}
    664664
     
    671671 */
    672672function custom_permalinks_original_post_link($post_id) {
    673     remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
    674     remove_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
    675     $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
    676     add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
    677     add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
    678     return $originalPermalink;
     673  remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
     674  remove_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
     675  $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
     676  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
     677  add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
     678  return $originalPermalink;
    679679}
    680680
     
    686686 */
    687687function custom_permalinks_original_page_link($post_id) {
    688     remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
    689     remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
    690     $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
    691     add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
    692     add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
    693     return $originalPermalink;
     688  remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
     689  remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
     690  $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
     691  add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
     692  add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
     693  return $originalPermalink;
    694694}
    695695
     
    702702 */
    703703function custom_permalinks_original_tag_link($tag_id) {
    704     remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
    705     remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
    706     $originalPermalink = ltrim(str_replace(home_url(), '', get_tag_link($tag_id)), '/');
    707     add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
    708     add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
    709     return $originalPermalink;
     704  remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
     705  remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
     706  $originalPermalink = ltrim(str_replace(home_url(), '', get_tag_link($tag_id)), '/');
     707  add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
     708  add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
     709  return $originalPermalink;
    710710}
    711711
     
    717717 */
    718718function custom_permalinks_original_category_link($category_id) {
    719     remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
    720     remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
    721     $originalPermalink = ltrim(str_replace(home_url(), '', get_category_link($category_id)), '/');
    722     add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
    723     add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
    724     return $originalPermalink;
     719  remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
     720  remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
     721  $originalPermalink = ltrim(str_replace(home_url(), '', get_category_link($category_id)), '/');
     722  add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
     723  add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
     724  return $originalPermalink;
    725725}
    726726
     
    732732 */
    733733function custom_permalinks_permalink_for_term($id) {
    734     $table = get_option('custom_permalink_table');
    735     if ( $table )
    736     foreach ( $table as $link => $info ) {
    737         if ( $info['id'] == $id ) {
    738             return $link;
    739         }
    740     }
    741     return false;
     734  $table = get_option('custom_permalink_table');
     735  if ( $table )
     736  foreach ( $table as $link => $info ) {
     737    if ( $info['id'] == $id ) {
     738      return $link;
     739    }
     740  }
     741  return false;
    742742}
    743743
     
    749749 */
    750750function custom_permalinks_setup_admin_menu() {
    751     add_management_page( 'Custom Permalinks', 'Custom Permalinks', 'edit_others_pages', 'custom_permalinks', 'custom_permalinks_options_page' );
     751  add_management_page( 'Custom Permalinks', 'Custom Permalinks', 'edit_others_pages', 'custom_permalinks', 'custom_permalinks_options_page' );
    752752}
    753753
     
    759759 */
    760760function custom_permalinks_setup_admin_head() {
    761     wp_enqueue_script('admin-forms');
     761  wp_enqueue_script('admin-forms');
    762762}
    763763
     
    767767
    768768if (function_exists("add_action") && function_exists("add_filter")) {
    769     add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
    770     add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
    771     add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
    772     add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
    773     add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
    774     add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
    775     add_filter( 'request', 'custom_permalinks_request', 10, 1 );
    776     add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
    777 
    778     if (function_exists("get_bloginfo")) {
    779         $v = explode('.', get_bloginfo('version'));
    780     }
    781 
    782     if ( $v[0] >= 2 ) {
    783         add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 10, 4 );
    784     } else {
    785         add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
    786         add_action( 'edit_page_form', 'custom_permalinks_page_options' );
    787     }
    788 
    789     add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
    790     add_action( 'add_tag_form', 'custom_permalinks_term_options' );
    791     add_action( 'edit_category_form', 'custom_permalinks_term_options' );
    792     add_action( 'save_post', 'custom_permalinks_save_post' );
    793     add_action( 'save_page', 'custom_permalinks_save_post' );
    794     add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
    795     add_action( 'edited_category', 'custom_permalinks_save_category' );
    796     add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
    797     add_action( 'create_category', 'custom_permalinks_save_category' );
    798     add_action( 'delete_post', 'custom_permalinks_delete_permalink', 10);
    799     add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
    800     add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
    801     add_action( 'admin_head', 'custom_permalinks_setup_admin_head' );
    802     add_action( 'admin_menu', 'custom_permalinks_setup_admin_menu' );
     769  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
     770  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
     771  add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
     772  add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
     773  add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
     774  add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
     775  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
     776  add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
     777
     778  if (function_exists("get_bloginfo")) {
     779    $v = explode('.', get_bloginfo('version'));
     780  }
     781
     782  if ( $v[0] >= 2 ) {
     783      add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 10, 4 );
     784  } else {
     785      add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
     786      add_action( 'edit_page_form', 'custom_permalinks_page_options' );
     787  }
     788
     789  add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
     790  add_action( 'add_tag_form', 'custom_permalinks_term_options' );
     791  add_action( 'edit_category_form', 'custom_permalinks_term_options' );
     792  add_action( 'save_post', 'custom_permalinks_save_post' );
     793  add_action( 'save_page', 'custom_permalinks_save_post' );
     794  add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
     795  add_action( 'edited_category', 'custom_permalinks_save_category' );
     796  add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
     797  add_action( 'create_category', 'custom_permalinks_save_category' );
     798  add_action( 'delete_post', 'custom_permalinks_delete_permalink', 10);
     799  add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
     800  add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
     801  add_action( 'admin_head', 'custom_permalinks_setup_admin_head' );
     802  add_action( 'admin_menu', 'custom_permalinks_setup_admin_menu' );
    803803}
    804804?>
Note: See TracChangeset for help on using the changeset viewer.