Last active
January 9, 2016 19:26
-
-
Save bryanwillis/9c13e0233c8e847693af to your computer and use it in GitHub Desktop.
Some examples using PHP url parameters functions like parsing the URL and getting the current URL for Wordpress useage. Also example using unparse_url() in javascript.
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
<?php | |
/** | |
* Gets the URL of current page or requested page | |
*/ | |
function bw_get_url($url) { | |
if ($_SERVER['REQUEST_URI'] == '') { | |
$url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REDIRECT_URL']; | |
} else { | |
$url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
} | |
return $url; | |
} | |
/* | |
Example | |
URL : https://gist.github.com/bryanwillis/9c13e0233c8e847693af/ | |
Useage: echo bw_get_url($url); | |
Returns: https://gist.github.com/bryanwillis/9c13e0233c8e847693af/ | |
*/ |
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
<?php | |
// Gets the current URL | |
function host_relative_url($parsed_url) { | |
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; | |
$host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; | |
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; | |
$user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; | |
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; | |
$pass = ($user || $pass) ? "$pass@" : ''; | |
$path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; | |
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; | |
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; | |
return "$query$fragment"; | |
} | |
function check_admin_url_path() { | |
$url = cbs_request_uri_request_redirect(); | |
$relative = host_relative_url(parse_url($url)); | |
$admin_url = admin_url(); | |
$directory = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); | |
$path = $admin_url; | |
echo "$relative"; | |
} | |
add_action('all_admin_notices', 'check_admin_url_path'); |
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
function parse_url($url) | |
{ | |
$dom = document.createElement('a'); | |
$dom.href = $url; | |
$obj = | |
{ | |
scheme : $dom.protocol.substr(0, $dom.protocol.indexOf(':')), | |
host : $dom.hostname, | |
port : $dom.port, | |
path : $dom.pathname, | |
query : $dom.search.substr($dom.search.indexOf('?') + 1), | |
hash : $dom.hash.substr($dom.hash.indexOf('#')) | |
} | |
return $obj; | |
} |
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
<?php | |
/** | |
* Relative URLs in Wordpress using unparse_url() | |
*/ | |
class RelativeLinksPlugin { | |
function __construct() { | |
add_filter( 'content_save_pre', array( $this, 'make_root_relative' ) ); | |
} | |
// Makes all of the URLs in the given string root relative for the current site | |
function make_root_relative( $string ) { | |
assert( is_string( $string ) ); | |
// The site URLs to trim | |
$site_urls = array( home_url(), ); | |
$site_urls = apply_filters( 'relative_urls_list', $site_urls ); | |
// Trim each of the site URLs in hte given content | |
foreach ( $site_urls as $site_url ) { | |
// Remove the path from the url | |
$url_bits = parse_url( $site_url ); | |
$url_bits['path'] = ''; | |
// Make URL relative in string content (for both http & https) | |
foreach ( array( 'http', 'https' ) as $scheme ) { | |
$url_bits['scheme'] = $scheme; | |
$site_url = $this->unparse_url( $url_bits ); | |
$string = str_replace( $site_url, '', $string ); | |
} | |
} | |
return $string; | |
} | |
function unparse_url( $parsed_url ) { | |
assert( ! empty( $parsed_url ) ); | |
$scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : ''; | |
$host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; | |
$port = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : ''; | |
$user = isset( $parsed_url['user'] ) ? $parsed_url['user'] : ''; | |
$pass = isset( $parsed_url['pass'] ) ? ':' . $parsed_url['pass'] : ''; | |
$pass = ( $user || $pass ) ? "$pass@" : ''; | |
$path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; | |
$query = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : ''; | |
$fragment = isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : ''; | |
return "$scheme$user$pass$host$port$path$query$fragment"; | |
} | |
} | |
// Boot | |
new RelativeLinksPlugin(); |
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
<?php | |
/** | |
* Rebuilds URL from parameters | |
*/ | |
function unparse_url($parsed_url) { | |
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; | |
$host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; | |
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; | |
$user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; | |
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; | |
$pass = ($user || $pass) ? "$pass@" : ''; | |
$path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; | |
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; | |
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; | |
return "$scheme$user$pass$host$port$path$query$fragment"; | |
} | |
/* | |
Example useage with bw_get_url($url) function | |
$current_url = bw_get_url($url); | |
$parse_url = parse_url($current_url); | |
$unparsed_url = unparse_url(parse_url($current_url)); | |
if ($current_url === $unparsed_url) { | |
print "YES, both url's equal the current URL"; | |
} | |
Example with Wordpress: | |
$home = site_url(); | |
$unparsed_url = unparse_url(parse_url($home)); | |
if ($current_url === $unparsed_url) { | |
echo "Your on the homepage"; | |
} | |
*/ | |
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
<?php | |
/** | |
* Get the active URL directory | |
*/ | |
function bw_active_dir() { | |
$dir = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); | |
return $dir; | |
} | |
/* | |
Example | |
URL : https://gist.github.com/bryanwillis/9c13e0233c8e847693af/ | |
Useage: echo bw_active_dir(); | |
Returns: /bryanwillis | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment