Custom Permalinks - Version 0.2.1

Version Description

Download this release

Release Info

Developer michaeltyson
Plugin Icon Custom Permalinks
Version 0.2.1
Comparing to
See all releases

Code changes from version 0.2 to 0.2.1

Files changed (2) hide show
  1. custom-permalinks.php +17 -12
  2. readme.txt +3 -3
custom-permalinks.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Custom Permalinks
4
  Plugin URI: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
5
  Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
6
  Description: Set custom permalinks on a per-post basis
7
- Version: 0.2
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
@@ -76,8 +76,9 @@ function custom_permalinks_term_link($permalink, $term) {
76
  */
77
  function custom_permalinks_redirect() {
78
 
79
- // Get request URI, strip parameters and /'s
80
- $request = trim((($pos=strpos($_SERVER['REQUEST_URI'], "?")) ? substr($_SERVER['REQUEST_URI'], 0, $pos) : $_SERVER['REQUEST_URI']), "/");
 
81
 
82
  global $wp_query;
83
  $post = $wp_query->post;
@@ -111,17 +112,21 @@ function custom_permalinks_redirect() {
111
  function custom_permalinks_request($query) {
112
 
113
  // Get request URI, strip parameters and /'s
114
- $request = trim((($pos=strpos($_SERVER['REQUEST_URI'], "?")) ? substr($_SERVER['REQUEST_URI'], 0, $pos) : $_SERVER['REQUEST_URI']), "/");
 
 
115
  if ( !$request ) return $query;
116
 
117
  $posts = get_posts( array('meta_key' => 'custom_permalink', 'meta_value' => $request) );
 
 
118
  if ( $posts ) {
119
  // A post matches our request
120
- return array("p" => $posts[0]->ID);
121
  }
122
 
123
  $table = get_option('custom_permalink_table');
124
- if ( $table && ($term = $table[$request]) ) {
125
  return array(($term['kind'] == 'category' ? 'category_name' : 'tag') => $term['slug']);
126
  }
127
 
@@ -227,7 +232,7 @@ function custom_permalinks_save_post($id) {
227
 
228
  delete_post_meta( $id, 'custom_permalink' );
229
  if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != custom_permalinks_original_post_link($id) )
230
- add_post_meta( $id, 'custom_permalink', stripcslashes($_REQUEST['custom_permalink']) );
231
  }
232
 
233
 
@@ -239,7 +244,7 @@ function custom_permalinks_save_post($id) {
239
  */
240
  function custom_permalinks_save_tag($id) {
241
  if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
242
- $newPermalink = $_REQUEST['custom_permalink'];
243
 
244
  if ( $newPermalink == custom_permalinks_original_tag_link($id) )
245
  $newPermalink = '';
@@ -256,7 +261,7 @@ function custom_permalinks_save_tag($id) {
256
  */
257
  function custom_permalinks_save_category($id) {
258
  if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
259
- $newPermalink = $_REQUEST['custom_permalink'];
260
 
261
  if ( $newPermalink == custom_permalinks_original_category_link($id) )
262
  $newPermalink = '';
@@ -436,7 +441,7 @@ function custom_permalinks_admin_rows() {
436
  */
437
  function custom_permalinks_original_post_link($post_id) {
438
  remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
439
- $originalPermalink = trim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/');
440
  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
441
  return $originalPermalink;
442
  }
@@ -451,7 +456,7 @@ function custom_permalinks_original_post_link($post_id) {
451
  */
452
  function custom_permalinks_original_tag_link($tag_id) {
453
  remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
454
- $originalPermalink = trim(str_replace(get_option('home'), '', get_tag_link($tag_id)), '/');
455
  add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
456
  return $originalPermalink;
457
  }
@@ -464,7 +469,7 @@ function custom_permalinks_original_tag_link($tag_id) {
464
  */
465
  function custom_permalinks_original_category_link($category_id) {
466
  remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
467
- $originalPermalink = trim(str_replace(get_option('home'), '', get_category_link($category_id)), '/');
468
  add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
469
  return $originalPermalink;
470
  }
4
  Plugin URI: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
5
  Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
6
  Description: Set custom permalinks on a per-post basis
7
+ Version: 0.2.1
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
76
  */
77
  function custom_permalinks_redirect() {
78
 
79
+ // Get request URI, strip parameters
80
+ $request = substr($_SERVER['REQUEST_URI'], 1);
81
+ if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
82
 
83
  global $wp_query;
84
  $post = $wp_query->post;
112
  function custom_permalinks_request($query) {
113
 
114
  // Get request URI, strip parameters and /'s
115
+ $request = (($pos=strpos($_SERVER['REQUEST_URI'], '?')) ? substr($_SERVER['REQUEST_URI'], 0, $pos) : $_SERVER['REQUEST_URI']);
116
+ $request = preg_replace('@/+@','/', ltrim($request, '/'));
117
+
118
  if ( !$request ) return $query;
119
 
120
  $posts = get_posts( array('meta_key' => 'custom_permalink', 'meta_value' => $request) );
121
+ if ( !$posts ) // Try adding trailing /
122
+ $posts = get_posts( array('meta_key' => 'custom_permalink', 'meta_value' => $request.'/') );
123
  if ( $posts ) {
124
  // A post matches our request
125
+ return array('p' => $posts[0]->ID);
126
  }
127
 
128
  $table = get_option('custom_permalink_table');
129
+ if ( $table && ($term = $table[$request]) || ($term = $table[$request.'/']) ) {
130
  return array(($term['kind'] == 'category' ? 'category_name' : 'tag') => $term['slug']);
131
  }
132
 
232
 
233
  delete_post_meta( $id, 'custom_permalink' );
234
  if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != custom_permalinks_original_post_link($id) )
235
+ add_post_meta( $id, 'custom_permalink', ltrim(stripcslashes($_REQUEST['custom_permalink']),"/") );
236
  }
237
 
238
 
244
  */
245
  function custom_permalinks_save_tag($id) {
246
  if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
247
+ $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
248
 
249
  if ( $newPermalink == custom_permalinks_original_tag_link($id) )
250
  $newPermalink = '';
261
  */
262
  function custom_permalinks_save_category($id) {
263
  if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
264
+ $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
265
 
266
  if ( $newPermalink == custom_permalinks_original_category_link($id) )
267
  $newPermalink = '';
441
  */
442
  function custom_permalinks_original_post_link($post_id) {
443
  remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
444
+ $originalPermalink = ltrim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/');
445
  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
446
  return $originalPermalink;
447
  }
456
  */
457
  function custom_permalinks_original_tag_link($tag_id) {
458
  remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
459
+ $originalPermalink = ltrim(str_replace(get_option('home'), '', get_tag_link($tag_id)), '/');
460
  add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
461
  return $originalPermalink;
462
  }
469
  */
470
  function custom_permalinks_original_category_link($category_id) {
471
  remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
472
+ $originalPermalink = ltrim(str_replace(get_option('home'), '', get_category_link($category_id)), '/');
473
  add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
474
  return $originalPermalink;
475
  }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
4
  Tags: permalink, url, link, address, custom, redirect
5
  Requires at least: 2.6
6
  Tested up to: 2.6.2
7
- Stable tag: 0.2
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
@@ -21,9 +21,9 @@ over your site structure.
21
  2. Activate the plugin through the 'Plugins' menu in WordPress
22
  3. Edit any post, tag or category to set a custom permalink.
23
 
24
- == Notes ==
25
 
26
- Changelog
27
 
28
  0.2: Added 'Custom Permalinks' section under 'Manage' to show existing custom permalinks, and allow reverting to the defaults
29
 
4
  Tags: permalink, url, link, address, custom, redirect
5
  Requires at least: 2.6
6
  Tested up to: 2.6.2
7
+ Stable tag: 0.2.1
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
21
  2. Activate the plugin through the 'Plugins' menu in WordPress
22
  3. Edit any post, tag or category to set a custom permalink.
23
 
24
+ == Changelog ==
25
 
26
+ 0.2.1: Better handling of trailing slashes
27
 
28
  0.2: Added 'Custom Permalinks' section under 'Manage' to show existing custom permalinks, and allow reverting to the defaults
29