Simple “Catch URI” function

This function is vaguely inspired by the function bp_core_set_uri_globals from BuddyPress.

I use it often for more advanced plugins and themes. With it I can check which URL is being viewed and perform actions depending on the url.

function mytheme_catch_uri() {
 $uri = $_SERVER['REQUEST_URI'];
 $uri = str_replace( '?' . $_SERVER['QUERY_STRING'], '', $uri );
 $uri = split( '/', $uri );
 $uri = array_values( array_filter( $uri ) );
}
add_action( 'init', 'mytheme_catch_uri', 5 );

This functions gives me an array with the pieces of the URL in an array. If I’m viewing /user/vincent the array will look like

Array (
[0] => 'user',
[1] => 'vincent'
)

With this I can do a lot of fun. For example, do some simple AJAX callbacks if I post to the URL /ajax/get_user/vincent.

function mytheme_catch_uri() {
 $uri = $_SERVER['REQUEST_URI'];
 $uri = str_replace( '?' . $_SERVER['QUERY_STRING'], '', $uri );
 $uri = split( '/', $uri );
 $uri = array_values( array_filter( $uri ) );

if ( isset($uri[0]) && $uri[0] == 'ajax' ) {
$action = $uri[1];
if ( $action =='get_user' ) {
$user = $uri[2];
// Do some more
exit;
}
}
}
 add_action( 'init', 'mytheme_catch_uri', 5 );

This is just one examples of all the things you can do!

Advertisements
About

Web Developer @ Oakwood Creative - www.oakwood.se

Tagged with: , ,
Posted in Advanced
2 comments on “Simple “Catch URI” function
  1. Ross says:

    This is nice way but it will break if you don’t have the site in the root of the server. If you for example have your wp site on the url mydomain.com/mywpsite/ the array with pieces will contain the “/mywpsite/” part aswell. To make it a little bit generic you can check the what the URL to your wp site is and then remove the extra folders if there are any. This will do the trick:

    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https://':'http://';
    	$uri = substr($protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],strlen(site_url()));
    	$uri = str_replace( '?' . $_SERVER['QUERY_STRING'], '', $uri );
    	$uri = split( '/', $uri );
    	$uri = array_values( array_filter( $uri ) );
    • Ross says:

      Hmm, the code seemed get f*** up. It should be:

      $protocol = strtolower(substr($_SERVER[“SERVER_PROTOCOL”],0,5))==’https’?’https://’:’http://’;
      $uri = substr($protocol.$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’],strlen(site_url()));
      $uri = str_replace( ‘?’ . $_SERVER[‘QUERY_STRING’], ”, $uri );
      $uri = split( ‘/’, $uri );
      $uri = array_values( array_filter( $uri ) );

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 120 other followers

%d bloggers like this: