views:

173

answers:

2

Of course I can always edit WordPress' .htaccess file, but that would kind of make my WP plugin a little non-standard. Instead, what is the way to hijack a URL via a custom plugin in WordPress?

For instance, let's say I want to build an elaborate product catalog that could be installed in WordPress as a plugin. Once activated, you could go into an admin panel on the plugin and tell it that all URLs starting with "/catalog" (or whatever you choose) would go to this product catalog app. Now the app would reside in the wp-content/plugins/mycatalog folder. So, /catalog/product/400 would go to wp-content/plugins/mycatalog/app.php which would then parse "product" and "400" out of the URL.

A: 

I'm not sure I understand exactly what you want, but you can always inspect the requested URL of the document in PHP with $_SERVER['REQUEST_URI'] and use something like parse_url or explode("/", $_SERVER['REQUEST_URI']) to dissect it.

As for redirecting the page you could use a header() call with an HTTP redirect or a META redirect. None of that is specific to WordPress though. The redirection plugin probably does what you need:

http://wordpress.org/extend/plugins/redirection/

Kristopher Ives
+1  A: 

I created a plugin folder in the path wp-content/plugins/hijack and then stuck this file plugin.php inside.

<?php
/*
Plugin Name: Hijack
Plugin URI: http://example.com/contact
Description: Sample URL Hijacker Plugin
Author: John Doe
Version: 1.1
Author URI: http://example.com/contact
*/

add_filter('init','Hijack');

function Hijack() {
    $URL = get_bloginfo('wpurl');
    $PLUGIN = $_SERVER['SCRIPT_FILENAME'];
    $PLUGIN = str_replace('/index.php','',$PLUGIN);
    $PLUGIN = $PLUGIN . '/wp-content/plugins/hijack';

    $sHijack = '/catalog';

    $sURL = $_SERVER['REDIRECT_URL'];
    if (strpos(' ' . $sURL,$sHijack)>0) {
        $sYank = $URL;
        $sYank = str_replace('http://','',$sYank);
        $sYank = str_replace('https://','',$sYank);
        $sYank = str_replace($_SERVER['SERVER_NAME'] . '/','',$sYank);
        $sYank = $sYank . $sHijack;
        $sTemp = str_replace($sYank,'',$sURL);
        $sTemp = str_replace($sYank . '/','',$sTemp);
        $sTemp = str_replace('//','/',$sTemp);
        if ((substr($sTemp,-1,1) == '/') and ($sTemp != '/')) {
            $sTemp = substr($sTemp, 0, -1);
        }
        define('VARS',$sTemp);
        include($PLUGIN . '/index.php');
        exit(0);
    }
}

Then, from there, I added wp-content/plugins/hijack/index.php as just a test case and used this code:

<?php

echo VARS;
echo "<br />\n";
print_r($_GET);

At that point, when the plugin was activated, I could redirect all URLs like /catalog, /catalog/products/400, etc. to the index.php file in my hijack plugin folder, and then index.php would act like a front controller for my web app loaded inside of WordPress. (I hope you are familiar with the MVC model of PHP programming.) The front controller could then be rewritten to call page controllers based on what was in VARS.

Volomike