Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active December 23, 2015 08:29
Show Gist options
  • Save cferdinandi/6608505 to your computer and use it in GitHub Desktop.
Save cferdinandi/6608505 to your computer and use it in GitHub Desktop.
Simple helper functions to get, sanitize, and process URLs in wordpress. (Find and replace `_s_` with your own namespacing.)
<?php
// Get and sanitize the current URL
function _s_get_url() {
// Get URL
$url = @( $_SERVER['HTTPS'] != 'on' ) ? 'http://' . $_SERVER['SERVER_NAME'] : 'https://' . $_SERVER['SERVER_NAME'];
// $url .= ( $_SERVER['SERVER_PORT'] !== 80 ) ? ":" . $_SERVER['SERVER_PORT'] : ''; // Uncomment to add port number
$url .= $_SERVER['REQUEST_URI'];
return $url;
}
// Get the site domain and remove the www.
function _s_get_site_domain() {
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
if ( substr( $domain, 0, 4 ) == 'www.' ) {
$sitename = substr( $sitename, 4 );
}
return $sitename;
}
// Prepare URL for status string
function _s_prepare_url( $url ) {
// If URL has a '?', add an '&'.
// Otherwise, add a '?'.
$url_status = strpos($url, '?');
if ( $url_status === false ) {
$concate = '?';
}
else {
$concate = '&';
}
return $url . $concate;
}
// Remove a $_GET variable from the URL
function _s_clean_url( $variable, $url ) {
$new_url = preg_replace('/(?:&|(\?))' . $variable . '=[^&]*(?(1)&|)?/i', '$1', $url);
$last_char = substr( $new_url, -1 );
if ( $last_char == '?' ) {
$new_url = substr($new_url, 0, -1);
}
return $new_url;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment