WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently have a button like this, that takes a user to their 'mentions' page

<a href="http://my.url.org/mentions/<?php global $userdata;
get_currentuserinfo(); echo( $userdata->user_login );?>">button</a>

What I'd like instead is that a user can see their 'mentions' when they log in to the page. so I'd like admin to be redirected to http://my.url.org/mentions/admin when he goes to a particular page (i.e. the homepage) - I think this would be best using a PHP redirect?

How could I do this?

share|improve this question
How is the "mentions" page output? Can you post the code for that page? Is this related to the P2 theme? If so, posting on their forums might give you insight as well. – guiniveretoo Jan 5 '13 at 23:58
Yeah it was related to the p2 theme, however @pogoking has managed to solve the issue, thank you though – ameeromar Jan 7 '13 at 16:10
up vote 2 down vote accepted

You have to add action to, for example, template_redirect action hook. The action would perform your desired checks and redirect user using the wp_redirect() function. The code could look something like this:

function my_redirect_function() {
    // Check if home page is being displayed
    if ( is_home() ) {
        global $userdata;
        $username = $userdata->user_login;
        $url = 'http://my.url.org/mentions/' . $username;

        wp_redirect( $url );
add_action( 'template_redirect', 'my_redirect_function' );

You place whatever checks (such as conditional tags) you'd like instead of is_home() check.

share|improve this answer
Thank you very much, this worked fantastically. Great explanation and links to conditional tags really helped, I've implemented this and it works! :) – ameeromar Jan 7 '13 at 0:44
@ameeromar Great to hear that. You can always mark the answer as accepted :) – Mateusz Hajdziony Jan 7 '13 at 0:55
is that the little tick? - I've done now... bit of an obscure button! – ameeromar Jan 7 '13 at 16:09

Your Answer


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.