Custom Permalinks - Version 0.2

Version Description

Download this release

Release Info

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

Code changes from version 0.1.1 to 0.2

Files changed (2) hide show
  1. custom-permalinks.php +136 -3
  2. readme.txt +10 -2
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.1.1
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
@@ -77,7 +77,7 @@ function custom_permalinks_term_link($permalink, $term) {
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,7 +111,7 @@ 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) );
@@ -305,7 +305,127 @@ function custom_permalinks_delete_term($id) {
305
  update_option('custom_permalink_table', $table);
306
  }
307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
 
310
 
311
  /**
@@ -366,6 +486,18 @@ function custom_permalinks_permalink_for_term($id) {
366
  return false;
367
  }
368
 
 
 
 
 
 
 
 
 
 
 
 
 
369
 
370
  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
371
  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
@@ -381,5 +513,6 @@ add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
381
  add_action( 'edited_category', 'custom_permalinks_save_category' );
382
  add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
383
  add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
 
384
 
385
  ?>
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
  */
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
  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) );
305
  update_option('custom_permalink_table', $table);
306
  }
307
 
308
+ /**
309
+ * Options page
310
+ *
311
+ * @package CustomPermalinks
312
+ * @since 0.1
313
+ */
314
+ function custom_permalinks_options_page() {
315
+
316
+ // Handle revert
317
+ if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) {
318
+ check_admin_referer('custom-permalinks-bulk');
319
+ foreach ( (array)$_REQUEST['revert'] as $identifier ) {
320
+ list($kind, $id) = explode('.', $identifier);
321
+ switch ( $kind ) {
322
+ case 'post':
323
+ delete_post_meta( $id, 'custom_permalink' );
324
+ break;
325
+ case 'tag':
326
+ case 'category':
327
+ custom_permalinks_delete_term($id);
328
+ break;
329
+ }
330
+ }
331
+
332
+ // Redirect
333
+ $redirectUrl = $_SERVER['REQUEST_URI'];
334
+ ?>
335
+ <script type="text/javascript">
336
+ document.location = '<?php echo $redirectUrl ?>';
337
+ </script>
338
+ <?php
339
+ }
340
+
341
+ ?>
342
+ <div class="wrap">
343
+ <h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2>
344
+
345
+ <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
346
+ <?php wp_nonce_field('custom-permalinks-bulk') ?>
347
+
348
+ <div class="tablenav">
349
+ <div class="alignleft">
350
+ <input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" />
351
+ </div>
352
+ <br class="clear" />
353
+ </div>
354
+ <br class="clear" />
355
+ <table class="widefat">
356
+ <thead>
357
+ <tr>
358
+ <th scope="col" class="check-column"><input type="checkbox" /></th>
359
+ <th scope="col"><?php _e('Title', 'custom-permalinks') ?></th>
360
+ <th scope="col"><?php _e('Type', 'custom-permalinks') ?></th>
361
+ <th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th>
362
+ </tr>
363
+ </thead>
364
+ <tbody>
365
+ <?php
366
+ $rows = custom_permalinks_admin_rows();
367
+ foreach ( $rows as $row ) {
368
+ ?>
369
+ <tr valign="top">
370
+ <th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th>
371
+ <td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td>
372
+ <td><?php echo htmlspecialchars($row['type']) ?></td>
373
+ <td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="Visit <?php echo htmlspecialchars($row['title']) ?>">
374
+ <?php echo htmlspecialchars($row['permalink']) ?>
375
+ </a>
376
+ </td>
377
+ </tr>
378
+ <?php
379
+ }
380
+ ?>
381
+ </tbody>
382
+ </table>
383
+ </form>
384
+ </div>
385
+ <?php
386
+ }
387
 
388
+ /**
389
+ * Get rows for management view
390
+ *
391
+ * @package CustomPermalinks
392
+ * @since 0.1
393
+ */
394
+ function custom_permalinks_admin_rows() {
395
+ $rows = array();
396
+
397
+ // List tags/categories
398
+ $table = get_option('custom_permalink_table');
399
+ if ( $table && is_array($table) ) {
400
+ foreach ( $table as $permalink => $info ) {
401
+ $row = array();
402
+ $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category'));
403
+ $row['id'] = $info['kind'].'.'.$info['id'];
404
+ $row['permalink'] = get_option('home')."/".$permalink;
405
+ $row['type'] = ucwords($info['kind']);
406
+ $row['title'] = $term->name;
407
+ $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&tag_ID='.$info['id'] : 'categories.php?action=edit&cat_ID='.$info['id'] );
408
+ $rows[] = $row;
409
+ }
410
+ }
411
+
412
+ // List posts
413
+ global $wpdb;
414
+ $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
415
+ "$wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != ''";
416
+ $posts = $wpdb->get_results($query);
417
+ foreach ( $posts as $post ) {
418
+ $row = array();
419
+ $row['id'] = 'post.'.$post->ID;
420
+ $row['permalink'] = get_permalink($post->ID);
421
+ $row['type'] = 'Post';
422
+ $row['title'] = $post->post_title;
423
+ $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
424
+ $rows[] = $row;
425
+ }
426
+
427
+ return $rows;
428
+ }
429
 
430
 
431
  /**
486
  return false;
487
  }
488
 
489
+ /**
490
+ * Set up administration
491
+ *
492
+ * @package CustomPermalinks
493
+ * @since 0.1
494
+ */
495
+ function custom_permalinks_setup_admin() {
496
+ add_management_page( 'Custom Permalinks', 'Custom Permalinks', 5, __FILE__, 'custom_permalinks_options_page' );
497
+ if ( is_admin() )
498
+ wp_enqueue_script('admin-forms');
499
+ }
500
+
501
 
502
  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
503
  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
513
  add_action( 'edited_category', 'custom_permalinks_save_category' );
514
  add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
515
  add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
516
+ add_action( 'admin_menu', 'custom_permalinks_setup_admin' );
517
 
518
  ?>
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.1.1
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
@@ -19,4 +19,12 @@ over your site structure.
19
 
20
  1. Unzip the package, and upload `custom-permalinks` to the `/wp-content/plugins/` directory
21
  2. Activate the plugin through the 'Plugins' menu in WordPress
22
- 3. Edit any post, tag or category to set a custom permalink.
 
 
 
 
 
 
 
 
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
 
19
 
20
  1. Unzip the package, and upload `custom-permalinks` to the `/wp-content/plugins/` directory
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
+
30
+ 0.1.1: Fixed bug with categories