• I’m working on a WP multisite theme that will be deployed across a network of sites. I’m trying to get mysite.com/profile to bring the user to a frontend profile template I’m building in my theme directory called profile.php.

    Currently, I’m using this:

    add_action('parse_request', 'profile_url_handler');
    function profile_url_handler() {
    if( isset($_GET['profile']) ) {
    	include('profile.php');
    	exit();
      }
    }

    The problem is, that leaves a ? in the URL which I’d dearly like to get rid of. I’ve tried using wp_rewrite to add a rewrite rule, but it won’t let me get to any file except for index.php.

    add_action('init', 'profile_urls');
    function profile_urls() {
    	$themedir = get_bloginfo('stylesheet_directory');
    	$profile = $themedir . '/profile.php';
    	$new_rules = array( 'profile/?$' => $profile );
    	$wp_rewrite->rules = array_unshift($new_rules, $wp_rewrite->rules); // also tried $wp_rewrite->rules = $new_rules + $wp_rewrite->rules
    }

    Any ideas on how to make this work?

Viewing 1 replies (of 1 total)
  • How about catching 404’s in the template_redirect action? This worked on my default twentyten theme, I dumped it into functions.php, created a file in the theme called profile.php and wrote a print_r($user). The status header is key here, if you remove that line you’ll still get the 404 page in some browsers, despiting the fact that your server will actually be echoing something.

    my_template_redirect($requested_url=null, $do_redirect=true) {
    	global $wp;
    	global $wp_query;
    
    	if (is_404())
    	{
    		$request = $wp->request;
    		if (ereg("^([0-9a-zA-Z]+)/?$", $request, $regs))
    		{
    			$username = $regs[1];
    			$user = get_userdatabylogin($username);
    			if ($user) {
    				status_header(200);
    				include(TEMPLATEPATH . "/profile.php");
    				die();
    			}
    		}
    	}
    }
    
    add_filter('template_redirect', 'my_template_redirect', 10, 2);
Viewing 1 replies (of 1 total)
  • The topic ‘Custom URL for a (non-WP) template’ is closed to new replies.