Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Need help setting up Private page (or posts) in user profile

@durkk

Participant

Heheh,

You see the only coding experience I have is with music related software or game middleware. The creative process of pulling pages per user is very abstract to me 😀

I do know my French classics…

“Perhaps the world’s second worst crime is boredom. The first is being a bore.”
-Jean Baudrillard

Hoping not being guilty of the first but here goes:

First I added a filter to bypass the members in the profile URL so I could use dynamic links via this plugin: Buddy Menu Buddy Links. Then I make a page with the username as parent and a child with the same title for all students (lessons). resulting in a workin url looking this mydomain.com/username/lessons.

I made a CPT to keep the it more organized only and the pages index less cluttered.

/**
 * Remove the slug from published post permalinks.
 */
function custom_remove_cpt_slug( $post_link, $post, $leavename ) {
 
    if ( 'student' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
 
    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
 
    return $post_link;
}
add_filter( 'post_type_link', 'custom_remove_cpt_slug', 10, 3 );
/**
 * Some hackery to have WordPress match postname to any of our public post types
 * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
 * Typically core only accounts for posts and pages where the slug is /post-name/
 */
function custom_parse_request_tricksy( $query ) {
 
    // Only noop the main query
    if ( ! $query->is_main_query() )
        return;
 
    // Only noop our very specific rewrite rule match
    if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }
 
    // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'student', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'custom_parse_request_tricksy' );
Skip to toolbar