Custom Permalinks - Version 0.1

Version Description

Download this release

Release Info

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

Version 0.1

Files changed (2) hide show
  1. custom-permalinks.php +385 -0
  2. readme.txt +22 -0
custom-permalinks.php ADDED
@@ -0,0 +1,385 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ 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
8
+ Author: Michael Tyson
9
+ Author URI: http://michael.tyson.id.au
10
+ */
11
+
12
+ /* Copyright 2008 Michael Tyson <mike@tyson.id.au>
13
+
14
+ This program is free software; you can redistribute it and/or modify
15
+ it under the terms of the GNU General Public License as published by
16
+ the Free Software Foundation; either version 2 of the License, or
17
+ (at your option) any later version.
18
+
19
+ This program is distributed in the hope that it will be useful,
20
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License
25
+ along with this program; if not, write to the Free Software
26
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
+ */
28
+
29
+
30
+ /**
31
+ ** Actions and filters
32
+ **
33
+ **/
34
+
35
+ /**
36
+ * Filter to replace the post permalink with the custom one
37
+ *
38
+ * @package CustomPermalinks
39
+ * @since 0.1
40
+ */
41
+ function custom_permalinks_post_link($permalink, $post) {
42
+ $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
43
+ if ( $custom_permalink ) {
44
+ return get_option('home')."/".$custom_permalink;
45
+ }
46
+
47
+ return $permalink;
48
+ }
49
+
50
+
51
+ /**
52
+ * Filter to replace the term permalink with the custom one
53
+ *
54
+ * @package CustomPermalinks
55
+ * @since 0.1
56
+ */
57
+ function custom_permalinks_term_link($permalink, $term) {
58
+ $table = get_option('custom_permalink_table');
59
+ if ( is_object($term) ) $term = $term->term_id;
60
+
61
+ $custom_permalink = custom_permalinks_permalink_for_term($term);
62
+
63
+ if ( $custom_permalink ) {
64
+ return get_option('home')."/".$custom_permalink;
65
+ }
66
+
67
+ return $permalink;
68
+ }
69
+
70
+
71
+ /**
72
+ * Action to redirect to the custom permalink
73
+ *
74
+ * @package CustomPermalinks
75
+ * @since 0.1
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;
84
+ if ( !$post ) return;
85
+
86
+ if ( is_single() ) {
87
+ $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
88
+ if ( $custom_permalink && $custom_permalink != $request ) {
89
+ // There's a post with a matching custom permalink
90
+ wp_redirect( get_option('home')."/".$custom_permalink, 301 );
91
+ exit();
92
+ }
93
+ } else if ( is_tag() || is_category() ) {
94
+ $theTerm = $wp_query->get_queried_object();
95
+ $permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
96
+
97
+ if ( $permalink && $permalink != $request ) {
98
+ // The current term has a permalink that isn't where we're currently at
99
+ wp_redirect( get_option('home')."/".$permalink, 301 );
100
+ exit();
101
+ }
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Filter to rewrite the query if we have a matching post
107
+ *
108
+ * @package CustomPermalinks
109
+ * @since 0.1
110
+ */
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'] => $term['slug']);
126
+ }
127
+
128
+ return $query;
129
+ }
130
+
131
+
132
+
133
+ /**
134
+ ** Administration
135
+ **
136
+ **/
137
+
138
+
139
+ /**
140
+ * Per-post options
141
+ *
142
+ * @package CustomPermalinks
143
+ * @since 0.1
144
+ */
145
+ function custom_permalinks_post_options() {
146
+ global $post;
147
+ $post_id = $post;
148
+ if (is_object($post_id)) {
149
+ $post_id = $post_id->ID;
150
+ }
151
+
152
+ $permalink = get_post_meta( $post_id, 'custom_permalink', true );
153
+
154
+ ?>
155
+ <div class="postbox closed">
156
+ <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
157
+ <div class="inside">
158
+ <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
159
+ </div>
160
+ </div>
161
+ <?php
162
+ }
163
+
164
+
165
+ /**
166
+ * Per-category/tag options
167
+ *
168
+ * @package CustomPermalinks
169
+ * @since 0.1
170
+ */
171
+ function custom_permalinks_term_options($object) {
172
+
173
+ $permalink = custom_permalinks_permalink_for_term($object->term_id);
174
+
175
+ $originalPermalink = ($object->taxonomy == 'post_tag' ?
176
+ custom_permalinks_original_tag_link($object->term_id) :
177
+ custom_permalinks_original_category_link($object->term_id) );
178
+
179
+ custom_permalinks_form($permalink, $originalPermalink);
180
+
181
+ // Move the save button to above this form
182
+ wp_enqueue_script('jquery');
183
+ ?>
184
+ <script type="text/javascript">
185
+ jQuery(document).ready(function() {
186
+ var button = jQuery('#custom_permalink_form').parent().find('.submit');
187
+ button.remove().insertAfter(jQuery('#custom_permalink_form'));
188
+ });
189
+ </script>
190
+ <?php
191
+ }
192
+
193
+ /**
194
+ * Helper function to render form
195
+ *
196
+ * @package CustomPermalinks
197
+ * @since 0.1
198
+ */
199
+ function custom_permalinks_form($permalink, $original="") {
200
+ ?>
201
+ <input value="true" type="hidden" name="custom_permalinks_edit" />
202
+ <table class="form-table" id="custom_permalink_form">
203
+ <tr>
204
+ <th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th>
205
+ <td>
206
+ <?php echo get_option('home') ?>/
207
+ <input type="text" name="custom_permalink" class="text" value="<?php echo htmlspecialchars($permalink ? $permalink : $original) ?>"
208
+ style="width: 300px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
209
+ onfocus="if ( this.value == '<?php echo htmlspecialchars($original) ?>' ) { this.value = ''; this.style.color = '#000'; }"
210
+ onblur="if ( this.value == '' ) { this.value = '<?php echo htmlspecialchars($original) ?>'; this.style.color = '#ddd'; }"/><br />
211
+ <small><?php _e('Leave blank to disable', 'custom-permalink') ?></small>
212
+ </td>
213
+ </tr>
214
+ </table>
215
+ <?php
216
+ }
217
+
218
+
219
+ /**
220
+ * Save per-post options
221
+ *
222
+ * @package CustomPermalinks
223
+ * @since 0.1
224
+ */
225
+ function custom_permalinks_save_post($id) {
226
+ if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
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
+
234
+ /**
235
+ * Save per-tag options
236
+ *
237
+ * @package CustomPermalinks
238
+ * @since 0.1
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 = '';
246
+
247
+ $term = get_term($id, 'post_tag');
248
+ custom_permalinks_save_term($term, $newPermalink);
249
+ }
250
+
251
+ /**
252
+ * Save per-category options
253
+ *
254
+ * @package CustomPermalinks
255
+ * @since 0.1
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 = '';
263
+
264
+ $term = get_term($id, 'category');
265
+ custom_permalinks_save_term($term, $newPermalink);
266
+ }
267
+
268
+ /**
269
+ * Save term (common to tags and categories)
270
+ *
271
+ * @package CustomPermalinks
272
+ * @since 0.1
273
+ */
274
+ function custom_permalinks_save_term($term, $permalink) {
275
+
276
+ custom_permalinks_delete_term($term->term_id);
277
+ $table = get_option('custom_permalink_table');
278
+ if ( $permalink )
279
+ $table[$permalink] = array(
280
+ 'id' => $term->term_id,
281
+ 'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'),
282
+ 'slug' => $term->slug);
283
+
284
+ update_option('custom_permalink_table', $table);
285
+ }
286
+
287
+
288
+ /**
289
+ * Delete term
290
+ *
291
+ * @package CustomPermalinks
292
+ * @since 0.1
293
+ */
294
+ function custom_permalinks_delete_term($id) {
295
+
296
+ $table = get_option('custom_permalink_table');
297
+ if ( $table )
298
+ foreach ( $table as $link => $info ) {
299
+ if ( $info['id'] == $id ) {
300
+ unset($table[$link]);
301
+ break;
302
+ }
303
+ }
304
+
305
+ update_option('custom_permalink_table', $table);
306
+ }
307
+
308
+
309
+
310
+
311
+ /**
312
+ * Get original permalink
313
+ *
314
+ * @package CustomPermalinks
315
+ * @since 0.1
316
+ */
317
+ function custom_permalinks_original_post_link($post_id) {
318
+ remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
319
+ $originalPermalink = trim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/');
320
+ add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
321
+ return $originalPermalink;
322
+ }
323
+
324
+
325
+
326
+ /**
327
+ * Get original permalink for tag
328
+ *
329
+ * @package CustomPermalinks
330
+ * @since 0.1
331
+ */
332
+ function custom_permalinks_original_tag_link($tag_id) {
333
+ remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
334
+ $originalPermalink = trim(str_replace(get_option('home'), '', get_tag_link($tag_id)), '/');
335
+ add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
336
+ return $originalPermalink;
337
+ }
338
+
339
+ /**
340
+ * Get original permalink for category
341
+ *
342
+ * @package CustomPermalinks
343
+ * @since 0.1
344
+ */
345
+ function custom_permalinks_original_category_link($category_id) {
346
+ remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
347
+ $originalPermalink = trim(str_replace(get_option('home'), '', get_category_link($category_id)), '/');
348
+ add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
349
+ return $originalPermalink;
350
+ }
351
+
352
+ /**
353
+ * Get permalink for term
354
+ *
355
+ * @package CustomPermalinks
356
+ * @since 0.1
357
+ */
358
+ function custom_permalinks_permalink_for_term($id) {
359
+ $table = get_option('custom_permalink_table');
360
+ if ( $table )
361
+ foreach ( $table as $link => $info ) {
362
+ if ( $info['id'] == $id ) {
363
+ return $link;
364
+ }
365
+ }
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 );
372
+ add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
373
+ add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
374
+ add_filter( 'request', 'custom_permalinks_request', 10, 1 );
375
+
376
+ add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
377
+ add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
378
+ add_action( 'edit_category_form', 'custom_permalinks_term_options' );
379
+ add_action( 'save_post', 'custom_permalinks_save_post' );
380
+ 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
+ ?>
readme.txt ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Custom Permalinks ===
2
+
3
+ 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
8
+
9
+ Set custom permalinks on a per-post, per-tag or per-category basis.
10
+
11
+ == Description ==
12
+
13
+ Lay out your site the way *you* want it. Set the URL of any post, tag or category to anything you want.
14
+ Old permalinks will redirect properly to the new address. Custom Permalinks gives you ultimate control
15
+ over your site structure.
16
+
17
+
18
+ == Installation ==
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.