therealbischero
therealbischero

Reputation: 224

Wordpress: show a page for each user with same url

I'm trying to create a members area inside a website made with WordPress but I'm stuck with a problem.

In this member area, there is a page (for example 'My Products) where I want to show different pieces of information for each user.

I'd like to create a page "My Products" for each user called by the same URL in the menu.

So if I click on "My Products" in the menu, I want to see the products for the currently logged user. Can you help me?

Thanks in advance.

Upvotes: 0

Views: 153

Answers (1)

Obzi
Obzi

Reputation: 2390

so here is the way i've done it :

You need to change "XXXX" with your page id, and "user_custom_page" with your user meta key.

add_filter( 'request', 'custom_request', 1 );
function custom_request($query) {
    global $wpdb;

    if( substr($_SERVER['SERVER_PROTOCOL'], 0, 5) == "HTTP/" )
        $current_link = "http://";
    else
        $current_link = "https://";
    $current_link .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    $user_page = get_permalink( XXXX );
    $user_id = get_current_user_id();

    if($current_link == $user_page && $user_id !== 0) {
        $custom_user_page_id = get_user_meta($user_id, "user_custom_page", true);
        if(!empty($custom_user_page_id)) {

            $p_g = $_GET;
            $p_ru = $_SERVER['REQUEST_URI'];
            $p_qs = $_SERVER['QUERY_STRING'];


            $_SERVER['REQUEST_URI'] = "/?post_type=page&p=".$custom_user_page_id;
            $_SERVER['QUERY_STRING'] = "post_type=page&p=".$custom_user_page_id;
            $_GET = array(
                'post_type' => 'page',
                'p' => $custom_user_page_id
            );

            remove_filter( 'request', 'custom_request', 1 );
            global $wp;
            $wp->parse_request();
            $query = $wp->query_vars;
            add_filter( 'request', 'custom_request', 1 );

            $_SERVER['REQUEST_URI'] = $p_ru;
            $_SERVER['QUERY_STRING'] = $p_qs;
            $_GET = $p_g;
        }
    }

    return $query;
}

EDIT : This code will be in the functions.php of the theme.

Upvotes: 1

Related Questions