Skip to content

Commit

Permalink
Resolved the conflict with PolyLang Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
samiahmedsiddiqui committed Jul 16, 2017
1 parent 476c039 commit e6d7d6e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 45 deletions.
117 changes: 74 additions & 43 deletions custom-permalinks.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
<?php
/*
Plugin Name: Custom Permalinks
Plugin URI: https://wordpress.org/plugins/custom-permalinks/
Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
Description: Set custom permalinks on a per-post basis
Version: 0.8
Author: Michael Tyson
Author URI: http://atastypixel.com/blog
Text Domain: custom-permalinks
*/

/* Copyright 2008-2017 Michael Tyson <[email protected]> and Sami Ahmed Siddiqui <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/**
* Plugin Name: Custom Permalinks
* Plugin URI: https://wordpress.org/plugins/custom-permalinks/
* Donate link: https://www.paypal.me/yasglobal
* Description: Set custom permalinks on a per-post basis
* Version: 0.9
* Author: Sami Ahmed Siddiqui
* Author URI: https://www.yasglobal.com/web-design-development/wordpress/custom-permalinks/
* Text Domain: custom-permalinks
*/

/**
* Copyright 2008-2017 Michael Tyson <[email protected]> and Sami Ahmed Siddiqui <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // don't access directly
}

/**
** Actions and filters
Expand All @@ -48,7 +53,6 @@ function custom_permalinks_post_link($permalink, $post) {
return $permalink;
}


/**
* Filter to replace the page permalink with the custom one
*
Expand All @@ -64,7 +68,6 @@ function custom_permalinks_page_link($permalink, $page) {
return $permalink;
}


/**
* Filter to replace the term permalink with the custom one
*
Expand All @@ -84,7 +87,6 @@ function custom_permalinks_term_link($permalink, $term) {
return $permalink;
}


/**
* Action to redirect to the custom permalink
*
Expand All @@ -98,6 +100,8 @@ function custom_permalinks_redirect() {
$request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);

$request = custom_permalinks_check_conflicts($request);

global $wp_query;

$custom_permalink = '';
Expand Down Expand Up @@ -156,10 +160,12 @@ function custom_permalinks_request($query) {
$url = isset($url['path']) ? $url['path'] : '';
$request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
$request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
$request_noslash = preg_replace('@/+@','/', trim($request, '/'));

if ( !$request ) return $query;

$request = custom_permalinks_check_conflicts($request);
$request_noslash = preg_replace('@/+@','/', trim($request, '/'));

// Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
$sql = $wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type, $wpdb->posts.post_status FROM $wpdb->posts ".
"LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
Expand Down Expand Up @@ -280,9 +286,9 @@ function custom_permalinks_trailingslash($string, $type) {
if ( !trim($request) ) return $string;

if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
if( isset($url['path']) ){
if( isset($url['path']) ) {
return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
}else{
} else {
return ($string{0} == '/' ? '/' : '') . $_CPRegisteredURL;
}
}
Expand Down Expand Up @@ -354,7 +360,6 @@ function custom_permalinks_post_options() {
<?php
}


/**
* Per-page options (Wordpress < 2.9)
*
Expand All @@ -380,7 +385,6 @@ function custom_permalinks_page_options() {
<?php
}


/**
* Per-category/tag options
*
Expand Down Expand Up @@ -431,6 +435,13 @@ function custom_permalinks_form($permalink, $original="", $renderContainers=true
<th scope="row"><?php _e('Custom Permalink', 'custom-permalinks') ?></th>
<td>
<?php endif; ?>

<?php
if ($permalink == '') {
$original = custom_permalinks_check_conflicts($original);
}
?>

<?php echo home_url() ?>/
<span id="editable-post-name" title="Click to edit this part of the permalink">
<input type="text" id="new-post-slug" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
Expand All @@ -449,7 +460,6 @@ function custom_permalinks_form($permalink, $original="", $renderContainers=true
endif;
}


/**
* Save per-post options
*
Expand All @@ -469,7 +479,6 @@ function custom_permalinks_save_post($id) {
}
}


/**
* Save per-tag options
*
Expand Down Expand Up @@ -679,7 +688,6 @@ function custom_permalinks_admin_rows() {
return $rows;
}


/**
* Get original permalink for post
*
Expand Down Expand Up @@ -721,7 +729,6 @@ function custom_permalinks_original_page_link($post_id) {
return $permalink;
}


/**
* Get original permalink for tag
*
Expand Down Expand Up @@ -789,9 +796,33 @@ function custom_permalinks_setup_admin_head() {
wp_enqueue_script('admin-forms');
}

# Check whether we're running within the WP environment, to avoid showing errors like
# "Fatal error: Call to undefined function get_bloginfo() in C:\xampp\htdocs\custom-permalinks\custom-permalinks.php on line 753"
# and similar errors that occurs when the script is called directly to e.g. find out the full path.
/**
* Check Conflicts and resolve it (e.g: Polylang)
*
* @package CustomPermalinks
* @since 0.9
*/
function custom_permalinks_check_conflicts($requested_url = '') {

if ($requested_url == '') return;

// Check if the Polylang Plugin is installed so, make changes in the URL
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if (is_plugin_active("polylang/polylang.php")) {
$polylang_config = get_option('polylang');
if ($polylang_config['force_lang'] == 1) {

if(strpos($requested_url, 'language/') !== false)
$requested_url = str_replace("language/", "", $requested_url);

$remove_lang = ltrim(strstr($requested_url, '/'), '/');
if ($remove_lang != '')
return $remove_lang;
}
}

return $requested_url;
}

if (function_exists("add_action") && function_exists("add_filter")) {
add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
Expand Down Expand Up @@ -829,4 +860,4 @@ function custom_permalinks_setup_admin_head() {
add_action( 'admin_head', 'custom_permalinks_setup_admin_head' );
add_action( 'admin_menu', 'custom_permalinks_setup_admin_menu' );
}
?>
?>
8 changes: 6 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
=== Custom Permalinks ===

Contributors: sasiddiqui, michaeltyson
Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
Donate link: https://www.paypal.me/yasglobal
Tags: permalink, url, link, address, custom, redirect, custom post type
Requires at least: 2.6
Tested up to: 4.7
Stable tag: 0.8
Stable tag: 0.9
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -34,6 +34,10 @@ within that category.

== Changelog ==

= 0.9 =

* Resolved the conflict with PolyLang Plugin

= 0.8 =

* Fixed (Draft preview issue for custom post types + some PHP Warnings)
Expand Down

0 comments on commit e6d7d6e

Please sign in to comment.