Created
December 4, 2022 16:56
-
-
Save sumanengbd/975e00d1abe01e033f5ad925bf8c057a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Custom URL Parameters Add Field Settings>Permalinks Page | |
* | |
*/ | |
if(!function_exists('http_build_url')) { | |
/** | |
* HTTP Build URL | |
* Combines arrays in the form of parse_url() into a new string based on specific options | |
* @name http_build_url | |
* @param string|array $url The existing URL as a string or result from parse_url | |
* @param string|array $parts Same as $url | |
* @param int $flags URLs are combined based on these | |
* @param array &$new_url If set, filled with array version of new url | |
* @return string | |
*/ | |
function http_build_url( $url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = false) | |
{ | |
// If the $url is a string | |
if(is_string($url)) | |
{ | |
$url = parse_url($url); | |
} | |
// If the $parts is a string | |
if(is_string($parts)) | |
{ | |
$parts = parse_url($parts); | |
} | |
// Scheme and Host are always replaced | |
if(isset($parts['scheme'])) $url['scheme'] = $parts['scheme']; | |
if(isset($parts['host'])) $url['host'] = $parts['host']; | |
// (If applicable) Replace the original URL with it's new parts | |
if(HTTP_URL_REPLACE & $flags) | |
{ | |
// Go through each possible key | |
foreach(array('user','pass','port','path','query','fragment') as $key) | |
{ | |
// If it's set in $parts, replace it in $url | |
if(isset($parts[$key])) $url[$key] = $parts[$key]; | |
} | |
} | |
else | |
{ | |
// Join the original URL path with the new path | |
if(isset($parts['path']) && (HTTP_URL_JOIN_PATH & $flags)) | |
{ | |
if(isset($url['path']) && $url['path'] != '') | |
{ | |
// If the URL doesn't start with a slash, we need to merge | |
if($url['path'][0] != '/') | |
{ | |
// If the path ends with a slash, store as is | |
if('/' == $parts['path'][strlen($parts['path'])-1]) | |
{ | |
$sBasePath = $parts['path']; | |
} | |
// Else trim off the file | |
else | |
{ | |
// Get just the base directory | |
$sBasePath = dirname($parts['path']); | |
} | |
// If it's empty | |
if('' == $sBasePath) $sBasePath = '/'; | |
// Add the two together | |
$url['path'] = $sBasePath . $url['path']; | |
// Free memory | |
unset($sBasePath); | |
} | |
if(false !== strpos($url['path'], './')) | |
{ | |
// Remove any '../' and their directories | |
while(preg_match('/\w+\/\.\.\//', $url['path'])){ | |
$url['path'] = preg_replace('/\w+\/\.\.\//', '', $url['path']); | |
} | |
// Remove any './' | |
$url['path'] = str_replace('./', '', $url['path']); | |
} | |
} | |
else | |
{ | |
$url['path'] = $parts['path']; | |
} | |
} | |
// Join the original query string with the new query string | |
if(isset($parts['query']) && (HTTP_URL_JOIN_QUERY & $flags)) | |
{ | |
if (isset($url['query'])) $url['query'] .= '&' . $parts['query']; | |
else $url['query'] = $parts['query']; | |
} | |
} | |
// Strips all the applicable sections of the URL | |
if(HTTP_URL_STRIP_USER & $flags) unset($url['user']); | |
if(HTTP_URL_STRIP_PASS & $flags) unset($url['pass']); | |
if(HTTP_URL_STRIP_PORT & $flags) unset($url['port']); | |
if(HTTP_URL_STRIP_PATH & $flags) unset($url['path']); | |
if(HTTP_URL_STRIP_QUERY & $flags) unset($url['query']); | |
if(HTTP_URL_STRIP_FRAGMENT & $flags) unset($url['fragment']); | |
// Store the new associative array in $new_url | |
$new_url = $url; | |
// Combine the new elements into a string and return it | |
return | |
((isset($url['scheme'])) ? $url['scheme'] . '://' : '') | |
.((isset($url['user'])) ? $url['user'] . ((isset($url['pass'])) ? ':' . $url['pass'] : '') .'@' : '') | |
.((isset($url['host'])) ? $url['host'] : '') | |
.((isset($url['port'])) ? ':' . $url['port'] : '') | |
.((isset($url['path'])) ? $url['path'] : '') | |
.((isset($url['query'])) ? '?' . $url['query'] : '') | |
.((isset($url['fragment'])) ? '#' . $url['fragment'] : '') | |
; | |
} | |
} | |
function custom_url_parameters() { | |
if( isset( $_POST['custom_url_parameters'] ) ) { | |
update_option( 'custom_url_parameters', esc_attr( $_POST['custom_url_parameters'] ) ); | |
} | |
// Add a settings field to the permalink page | |
add_settings_field( 'custom_url_parameters', __( 'URL Parameters' ), 'custom_url_parameters_field_callback', 'permalink', 'optional' ); | |
} | |
add_action( 'load-options-permalink.php', 'custom_url_parameters' ); | |
function custom_url_parameters_field_callback() { | |
$value = get_option( 'custom_url_parameters' ); | |
echo '<input type="text" value="' . esc_attr( $value ) . '" name="custom_url_parameters" id="custom_url_parameters" class="regular-text" placeholder="Enter your custom url parameters" />'; | |
} | |
function custom_url_parameters_redirect() { | |
$url_parameters = get_option( 'custom_url_parameters' ); | |
if ( isset( $url_parameters ) && !empty( $url_parameters ) ) { | |
$parameters_array = array_column(array_map(function($v){ | |
return explode("=", $v); | |
}, explode("&", html_entity_decode( $url_parameters ))), 1, 0); | |
//Prevent infinite loop | |
if ($_GET[array_keys($parameters_array)[0]] == array_values($parameters_array)[0]) return; | |
$url_parts = parse_url($_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); | |
parse_str(html_entity_decode( $url_parameters ), $params); | |
// $params["redirected"] = "1"; | |
foreach (array_keys($_GET) as $n) { | |
$params[$n] = $_GET[$n]; | |
} | |
// Note that this will url_encode all values | |
$url_parts['query'] = http_build_query($params); | |
wp_redirect(http_build_url($url_parts)); | |
exit; | |
} | |
} | |
add_action('template_redirect', 'custom_url_parameters_redirect'); | |
function custom_url_parameters_load_head() { | |
$url_parameters = get_option( 'custom_url_parameters' ); | |
if ( isset( $url_parameters ) && !empty( $url_parameters ) ): | |
$parameters_array = array_column(array_map(function($v){ | |
return explode("=", $v); | |
}, explode("&", html_entity_decode( $url_parameters ))), 1, 0); | |
?> | |
<script type="text/javascript"> | |
(function ($) { | |
var currentUrl = window.location.href; | |
var url = new URL(currentUrl); | |
var urlParameters = url.searchParams.getAll("<?php echo array_keys($parameters_array)[0]; ?>"); | |
$(document).ready(function () { | |
if (urlParameters != null) { | |
$('a:not([href*="tel"]):not([href*="mailto"])').each(function (index, element) { | |
var url = $(element).prop('href'); | |
if (url.indexOf('?') == -1) { | |
url += '?' + '<?php echo html_entity_decode( $url_parameters ); ?>'; | |
} | |
$(element).prop('href', url); | |
}); | |
} | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
endif; | |
} | |
add_action('wp_head', 'custom_url_parameters_load_head'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment