0

I understand how to register a custom post type, customize the slug and decide whether to add the 'front' (the WP permalink settings) or not.

What I'd like to do is customize the permalink by adding

?username

At the end of the custom post type's url. Now I know the cleaned up Author Name can be added in the WP permalink settings but I don't want that - I'm looking for the username used to login.

For example: I create a custom post type called 'Home' (singular) with the custom slug 'homes.' The url would be:

www.mydomain.com/homes/postname

I want the url to look like this instead:

www.mydomain.com/homes/postname?username

I'm not sure how to do it by modifying the usual method of registering a content type:

add_action( 'init', 'create_posttype' );
function create_posttype() {
  register_post_type( 'home',
    array(
      'labels' => array(
        'name' => __( 'Homes' ),
        'singular_name' => __( 'Home' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'homes'),
    )
  );
}

I experimented a bit and ended up making my site completely inaccessible so I decided some help would be worth asking for :P

1
  • btw I did try setting the WP permalink settings to %postname%?%authorname% Which resulted in 404s. Commented Aug 31, 2014 at 13:47

2 Answers 2

0

Note to curators/admin: I wanted to leave a comment but I didn't have enough reputation. I know I am not formally providing an answer, but attempting to help the person asking the question along.

Are you using any membership plugins? Can you give us a little more information on the overall site map and functionality? We are seeing a very narrow field of a bigger question to assess.

You're on the right path with the custom post type, but I think there is a better way to go about the situation.

I will assume from your provided code example that you have a decent understanding of PHP/Wordpress codex to best help explain what I mean to say would be the best idea here.

A more practical approach to generating public facing user profile pages would be a controlling theme function such as /profile/<foo> that looks up relevant user information. From there, you would pass permissions to each profile page to the user, who would need to login and edit their information either through the Wordpress dashboard. Or, users fill out profile information from another front-end page that feeds the public page.

If you are getting confused writing custom post types, you may want to install a turn-key solution. Without mentioning any brand names, there are many existing (and free) membership plugins that give the user a public profile + account management area.

1
  • The use-case is this: a tutorial website using a membership plugin (Paid Memberships Pro) with an affiliate program. Tutorial authors are the only affiliates and by adding their usernames to the permalink, my goal is to guarantee them commissions from their content. Commented Aug 31, 2014 at 13:45
0

To add query strings to post permalink you should use the post_link filter in combination with the add_query_arg function.

add_filter( 'post_link', 'cyb_append_query_string', 10, 3 );
function cyb_append_query_string( $url, $post, $leavename ) {
    //Change for the post type you want
    if ( $post->post_type == 'home' ) {
        $url = add_query_arg( 'username', 'username_value', $url );
    }
    return $url;
}

If you want to use that query argument in wp_query you should also run the query_vars filter:

add_filter( 'query_vars', 'cyb_query_vars' , 10, 1 );
function cyb_query_vars( $qvars ) {
    $qvars[] = 'username';
    return $qvars;
 }

Now you can get the username query var like this:

 $username = get_query_var( 'username' );
 //Or, if working with a WP_Query object
 $username = $wp_query->get( 'username' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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