简体   繁体   中英

Conditional URL Rewrites in WordPress

I'm hosting an event and using WordPress to manage the site. We have a separate table (not WP user table) of attendees and I'd like for each of them to have a page on our site:

example.com/user1, example.com/user2, etc.

I've been reading up about WP_Rewrite but am a bit confused about how to conditionally redirect if the user exists, otherwise go about the normal flow.

Any help in achieving this would be much appreciated, thank you!

Edit:

There is a single one-off page that I'm trying to forward to. Right now, it's accessed with:

example.com/user?username=user1

If i understood your problem, you're going to create a page for each user, so in the permalink structure add tag %postname%, and maybe install no-category base plugin.

Check if the .htaccess it's created with the configuration that wp write for that structure, if it's not you can just copy and paste.

I ended up doing this by prepending a user's url with a tilde (~). The rewrite code is as follows:

// Uncomment only when new rewrites have been made 
//add_filter('init', 'wds_flush');

// Leave uncommented 
add_filter('rewrite_rules_array', 'wds_user_rewrite');
add_filter('query_vars','wds_query_vars');

function wds_flush()
{
     global $wp_rewrite;
     $wp_rewrite->flush_rules();
}
function wds_user_rewrite($rules)
{
        global $wp_rewrite;
        $newRule = array(
                '^~(.+)$' => 'index.php?pagename=user&uid=$matches[1]&act=show',
        );
        $newRules = $newRule + $rules;

        return $newRules;
}
function wds_query_vars($vars)
{
        array_push($vars, 'uid');
        array_push($vars, 'act');
        return $vars;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM