Custom Permalinks - Version 0.9

Version Description

  • Resolved the conflict with PolyLang Plugin
Download this release

Release Info

Developer sasiddiqui
Plugin Icon Custom Permalinks
Version 0.9
Comparing to
See all releases

Code changes from version 0.8 to 0.9

Files changed (2) hide show
  1. custom-permalinks.php +74 -43
  2. readme.txt +6 -2
custom-permalinks.php CHANGED
@@ -1,32 +1,37 @@
1
  <?php
2
- /*
3
- Plugin Name: Custom Permalinks
4
- Plugin URI: https://wordpress.org/plugins/custom-permalinks/
5
- Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
6
- Description: Set custom permalinks on a per-post basis
7
- Version: 0.8
8
- Author: Michael Tyson
9
- Author URI: http://atastypixel.com/blog
10
- Text Domain: custom-permalinks
11
- */
12
-
13
- /* Copyright 2008-2017 Michael Tyson <michael@atastypixel.com> and Sami Ahmed Siddiqui <sami@samisiddiqui.com>
14
-
15
- This program is free software; you can redistribute it and/or modify
16
- it under the terms of the GNU General Public License as published by
17
- the Free Software Foundation; either version 2 of the License, or
18
- (at your option) any later version.
19
-
20
- This program is distributed in the hope that it will be useful,
21
- but WITHOUT ANY WARRANTY; without even the implied warranty of
22
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
- GNU General Public License for more details.
24
-
25
- You should have received a copy of the GNU General Public License
26
- along with this program; if not, write to the Free Software
27
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
- */
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  /**
32
  ** Actions and filters
@@ -48,7 +53,6 @@ function custom_permalinks_post_link($permalink, $post) {
48
  return $permalink;
49
  }
50
 
51
-
52
  /**
53
  * Filter to replace the page permalink with the custom one
54
  *
@@ -64,7 +68,6 @@ function custom_permalinks_page_link($permalink, $page) {
64
  return $permalink;
65
  }
66
 
67
-
68
  /**
69
  * Filter to replace the term permalink with the custom one
70
  *
@@ -84,7 +87,6 @@ function custom_permalinks_term_link($permalink, $term) {
84
  return $permalink;
85
  }
86
 
87
-
88
  /**
89
  * Action to redirect to the custom permalink
90
  *
@@ -98,6 +100,8 @@ function custom_permalinks_redirect() {
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 = '';
@@ -156,10 +160,12 @@ function custom_permalinks_request($query) {
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)
164
  $sql = $wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type, $wpdb->posts.post_status FROM $wpdb->posts ".
165
  "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
@@ -280,9 +286,9 @@ function custom_permalinks_trailingslash($string, $type) {
280
  if ( !trim($request) ) return $string;
281
 
282
  if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
283
- if( isset($url['path']) ){
284
  return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
285
- }else{
286
  return ($string{0} == '/' ? '/' : '') . $_CPRegisteredURL;
287
  }
288
  }
@@ -354,7 +360,6 @@ function custom_permalinks_post_options() {
354
  <?php
355
  }
356
 
357
-
358
  /**
359
  * Per-page options (Wordpress < 2.9)
360
  *
@@ -380,7 +385,6 @@ function custom_permalinks_page_options() {
380
  <?php
381
  }
382
 
383
-
384
  /**
385
  * Per-category/tag options
386
  *
@@ -431,6 +435,13 @@ function custom_permalinks_form($permalink, $original="", $renderContainers=true
431
  <th scope="row"><?php _e('Custom Permalink', 'custom-permalinks') ?></th>
432
  <td>
433
  <?php endif; ?>
 
 
 
 
 
 
 
434
  <?php echo home_url() ?>/
435
  <span id="editable-post-name" title="Click to edit this part of the permalink">
436
  <input type="text" id="new-post-slug" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
@@ -449,7 +460,6 @@ function custom_permalinks_form($permalink, $original="", $renderContainers=true
449
  endif;
450
  }
451
 
452
-
453
  /**
454
  * Save per-post options
455
  *
@@ -469,7 +479,6 @@ function custom_permalinks_save_post($id) {
469
  }
470
  }
471
 
472
-
473
  /**
474
  * Save per-tag options
475
  *
@@ -679,7 +688,6 @@ function custom_permalinks_admin_rows() {
679
  return $rows;
680
  }
681
 
682
-
683
  /**
684
  * Get original permalink for post
685
  *
@@ -721,7 +729,6 @@ function custom_permalinks_original_page_link($post_id) {
721
  return $permalink;
722
  }
723
 
724
-
725
  /**
726
  * Get original permalink for tag
727
  *
@@ -789,9 +796,33 @@ function custom_permalinks_setup_admin_head() {
789
  wp_enqueue_script('admin-forms');
790
  }
791
 
792
- # Check whether we're running within the WP environment, to avoid showing errors like
793
- # "Fatal error: Call to undefined function get_bloginfo() in C:\xampp\htdocs\custom-permalinks\custom-permalinks.php on line 753"
794
- # and similar errors that occurs when the script is called directly to e.g. find out the full path.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
795
 
796
  if (function_exists("add_action") && function_exists("add_filter")) {
797
  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
@@ -829,4 +860,4 @@ if (function_exists("add_action") && function_exists("add_filter")) {
829
  add_action( 'admin_head', 'custom_permalinks_setup_admin_head' );
830
  add_action( 'admin_menu', 'custom_permalinks_setup_admin_menu' );
831
  }
832
- ?>
1
  <?php
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ /**
4
+ * Plugin Name: Custom Permalinks
5
+ * Plugin URI: https://wordpress.org/plugins/custom-permalinks/
6
+ * Donate link: https://www.paypal.me/yasglobal
7
+ * Description: Set custom permalinks on a per-post basis
8
+ * Version: 0.9
9
+ * Author: Sami Ahmed Siddiqui
10
+ * Author URI: https://www.yasglobal.com/web-design-development/wordpress/custom-permalinks/
11
+ * Text Domain: custom-permalinks
12
+ */
13
+
14
+ /**
15
+ * Copyright 2008-2017 Michael Tyson <michael@atastypixel.com> and Sami Ahmed Siddiqui <sami@samisiddiqui.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
+ if ( ! defined( 'ABSPATH' ) ) {
33
+ exit; // don't access directly
34
+ }
35
 
36
  /**
37
  ** Actions and filters
53
  return $permalink;
54
  }
55
 
 
56
  /**
57
  * Filter to replace the page permalink with the custom one
58
  *
68
  return $permalink;
69
  }
70
 
 
71
  /**
72
  * Filter to replace the term permalink with the custom one
73
  *
87
  return $permalink;
88
  }
89
 
 
90
  /**
91
  * Action to redirect to the custom permalink
92
  *
100
  $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
101
  if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
102
 
103
+ $request = custom_permalinks_check_conflicts($request);
104
+
105
  global $wp_query;
106
 
107
  $custom_permalink = '';
160
  $url = isset($url['path']) ? $url['path'] : '';
161
  $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
162
  $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
 
163
 
164
  if ( !$request ) return $query;
165
 
166
+ $request = custom_permalinks_check_conflicts($request);
167
+ $request_noslash = preg_replace('@/+@','/', trim($request, '/'));
168
+
169
  // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
170
  $sql = $wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type, $wpdb->posts.post_status FROM $wpdb->posts ".
171
  "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
286
  if ( !trim($request) ) return $string;
287
 
288
  if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
289
+ if( isset($url['path']) ) {
290
  return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
291
+ } else {
292
  return ($string{0} == '/' ? '/' : '') . $_CPRegisteredURL;
293
  }
294
  }
360
  <?php
361
  }
362
 
 
363
  /**
364
  * Per-page options (Wordpress < 2.9)
365
  *
385
  <?php
386
  }
387
 
 
388
  /**
389
  * Per-category/tag options
390
  *
435
  <th scope="row"><?php _e('Custom Permalink', 'custom-permalinks') ?></th>
436
  <td>
437
  <?php endif; ?>
438
+
439
+ <?php
440
+ if ($permalink == '') {
441
+ $original = custom_permalinks_check_conflicts($original);
442
+ }
443
+ ?>
444
+
445
  <?php echo home_url() ?>/
446
  <span id="editable-post-name" title="Click to edit this part of the permalink">
447
  <input type="text" id="new-post-slug" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
460
  endif;
461
  }
462
 
 
463
  /**
464
  * Save per-post options
465
  *
479
  }
480
  }
481
 
 
482
  /**
483
  * Save per-tag options
484
  *
688
  return $rows;
689
  }
690
 
 
691
  /**
692
  * Get original permalink for post
693
  *
729
  return $permalink;
730
  }
731
 
 
732
  /**
733
  * Get original permalink for tag
734
  *
796
  wp_enqueue_script('admin-forms');
797
  }
798
 
799
+ /**
800
+ * Check Conflicts and resolve it (e.g: Polylang)
801
+ *
802
+ * @package CustomPermalinks
803
+ * @since 0.9
804
+ */
805
+ function custom_permalinks_check_conflicts($requested_url = '') {
806
+
807
+ if ($requested_url == '') return;
808
+
809
+ // Check if the Polylang Plugin is installed so, make changes in the URL
810
+ include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
811
+ if (is_plugin_active("polylang/polylang.php")) {
812
+ $polylang_config = get_option('polylang');
813
+ if ($polylang_config['force_lang'] == 1) {
814
+
815
+ if(strpos($requested_url, 'language/') !== false)
816
+ $requested_url = str_replace("language/", "", $requested_url);
817
+
818
+ $remove_lang = ltrim(strstr($requested_url, '/'), '/');
819
+ if ($remove_lang != '')
820
+ return $remove_lang;
821
+ }
822
+ }
823
+
824
+ return $requested_url;
825
+ }
826
 
827
  if (function_exists("add_action") && function_exists("add_filter")) {
828
  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
860
  add_action( 'admin_head', 'custom_permalinks_setup_admin_head' );
861
  add_action( 'admin_menu', 'custom_permalinks_setup_admin_menu' );
862
  }
863
+ ?>
readme.txt CHANGED
@@ -1,11 +1,11 @@
1
  === Custom Permalinks ===
2
 
3
  Contributors: sasiddiqui, michaeltyson
4
- Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
5
  Tags: permalink, url, link, address, custom, redirect, custom post type
6
  Requires at least: 2.6
7
  Tested up to: 4.7
8
- Stable tag: 0.8
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -34,6 +34,10 @@ within that category.
34
 
35
  == Changelog ==
36
 
 
 
 
 
37
  = 0.8 =
38
 
39
  * Fixed (Draft preview issue for custom post types + some PHP Warnings)
1
  === Custom Permalinks ===
2
 
3
  Contributors: sasiddiqui, michaeltyson
4
+ Donate link: https://www.paypal.me/yasglobal
5
  Tags: permalink, url, link, address, custom, redirect, custom post type
6
  Requires at least: 2.6
7
  Tested up to: 4.7
8
+ Stable tag: 0.9
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
34
 
35
  == Changelog ==
36
 
37
+ = 0.9 =
38
+
39
+ * Resolved the conflict with PolyLang Plugin
40
+
41
  = 0.8 =
42
 
43
  * Fixed (Draft preview issue for custom post types + some PHP Warnings)