WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] rewrite and template_redirect (4 posts)

  1. hanifb
    Member
    Posted 6 years ago #

    I have used the nicename permalinks for some time and it works like a charm. I use the /%postname%/ structure.

    I have written a plugin that:

    add_action('template_redirect','miljo_redirect');
    
    function netwrok_redirect()
    {
    if(isset($_GET['network'])
    	{
    		include(TEMPLATEPATH."/network.php");
    		exit();
    	}
    }

    How can i make the http://www.wordpressblog.com/?network rewrite to http://www.wordpressblog.com/network/
    AND make the template_redirect detect the /network tag?

    I've tried to resolve the rewrite with:

    add_action('generate_rewrite_rules','network_add_rewrite_rule');
    function network_add_rewrite_rule($wp_rewrite)
    	{
    $new_rules = array('natverk/(.+)' => 'index.php?natverk='.$wp_rewrite->preg_index(1) );
    	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    	}

    But it doesnt work.

  2. josh@bauguss.net
    Member
    Posted 6 years ago #

    well, I just went through a day of trying to figure this out. I finally had my eureka moment.

    the BIG thing you are missing. You must let wordpress know about your query variables or it will not grab them.

    add_action('query_vars', 'yourqueryaddfunction');

    function yourqueryaddfunction($qvars) {
    $qvars[] = 'network';
    return $qvars;
    }

    and per your code above, you would need add_action('template_redirect','miljo_redirect');
    to be
    add_action('template_redirect','netwrok_redirect');

    not sure if you had some typos or just cut and paste issues.

  3. josh@bauguss.net
    Member
    Posted 6 years ago #

    also, i think this is more efficient. I got this tidbit from jerome's tag keyword plugin.

    in an init function for your plugin (make sure to hook it in)

    global $wp_redirect;
    if(isset($wp_redirect))
    add_action('generate_rewrite_rules', 'myrewriterulefunction');

    doing that i think ensures that it only runs when necessary. (on the permalink option page) Otherwise you run the risk of updating the permalinks every page load which would be bad.

    I think I understand that right at least.

  4. hanifb
    Member
    Posted 6 years ago #

    Thank you both! Worked perfect!

Topic Closed

This topic has been closed to new replies.

About this Topic