0

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?

2
  • 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.
    – Jen
    Commented Jan 5, 2013 at 23:58
  • Yeah it was related to the p2 theme, however @pogoking has managed to solve the issue, thank you though
    – ameeromar
    Commented Jan 7, 2013 at 16:10

1 Answer 1

2

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;
        get_currentuserinfo();
        $username = $userdata->user_login;
        $url = 'http://my.url.org/mentions/' . $username;

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

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

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

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.