Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

on my site user profiles can be accessed on /user/xxx. Now i created a rewrite rule and all user links are handled on the template page. But how can i access the xxx(user) in the template file? The page should display the profile of xxx. Rewite rules:

add_filter( 'query_vars', 'wpleet_rewrite_add_var' );
function wpleet_rewrite_add_var( $vars )
{
$vars[] = 'user';
return $vars;
}
add_rewrite_tag( '%user%', '([^&]+)' );
add_rewrite_rule(
'^user/([^/]*)/?',
'index.php?user=$matches[1]',
'top'
 );
 add_action( 'template_redirect', 'wpleet_rewrite_catch' );
 function wpleet_rewrite_catch()
 {
 global $wp_query;

 if ( array_key_exists( 'user', $wp_query->query_vars ) ) {
    include (TEMPLATEPATH . '/user-profile.php');
    exit;
}
}
share|improve this question
add comment

2 Answers 2

I created a new function, with which i get the username. Is there any problem with using it? Or is there a cleaner way of doing it?

function getmyuser() {
$test = substr($_SERVER["REQUEST_URI"], strrpos($_SERVER["REQUEST_URI"],"/user/")+6);
return str_replace('/','', $test);
}
share|improve this answer
add comment

Although your question isn't completely clear to me I am trying to answer it based upon some assumption.

Assumption - You already changed the base slug with the following code:

add_action('init', 'wpa95490_new_author_base');
function wpa95490_new_author_base() 
{
    global $wp_rewrite;
    $author_slug = 'user';
    $wp_rewrite->author_base = $author_slug;
}

Assumption #2 - You want an author template for each user in your template.

If this is correct, then you want to use the author.php to process this as this file is being used to display the author information. The author template can be used for the following goals and sequence:

  • author-{id}.php
  • author-{nicename}.php
  • author.php

WordPress will first check is a pages exists for author-42.php or author-ruben.php. If no specific author template has been specified it will fall back on the author.php (and if author.php not found it will fall back either on the archive.php or index.php).

In your case I would suggest you to use the author-{nicename}.php in order to make this work. If you only want a specific section to be changed for an author you can ideally use the function get_template_part() for this.

On the other hand you can also extend the profile information based upon this article from Justin and display the added information with the following code the_author_meta( 'new-property' ).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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