0

We have pages with urls like this:

http://example.com/?page_id=3&user_id=5

http://example.com/?page_id=6&user_id=9

etc.

page_id and user_id located in table MySQL Database. And this table has the below structure:

ID, page_id and user_id

If I know ID I can make query to database and get page_id and user_id.

Question: how to make custom redirect? That user can use ID in short url like this

http://example.com/page/[ID]/

with redirect to

http://example.com/?page_id=[page_id]&user_id=[user_id]

Thank you!

0

I wrote function for short url. Maybe it will be helpful for someone:

function short_url(){
    global $post;
    if (preg_match('/^http:\/\/example.com\/page\/[0-9]{1,4}$/', "http://".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]))
    { 
        $my_id = intval(preg_replace('/[^0-9]+/', '', $_SERVER["REQUEST_URI"]), 10);
        global $wpdb;
        $myquery = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_mytable WHERE ID=%d", $my_id));
        $short_url="http://example.com/?page_id=".$myquery->page_id."&user_id=".$myqery->user_id;

        wp_redirect($short_url);
        exit();
    }
}
add_action( 'template_redirect', 'short_url' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.