Forum Moderators: coopster

Message Too Old, No Replies

Redirect Logged in Users

         

almo136

3:24 pm on Aug 5, 2009 (gmt 0)

10+ Year Member



Hi,

I have this piece of wordpress code which does something if the user is logged in and has at least one post:


<?php if ( is_user_logged_in()) : ?>
<?php if(get_usernumposts(wp_get_current_user()->ID) > 0) : ?>

DO SOMETHING

<?php endif; ?>
<?php endif; ?>

When users arrive at my homepgae I would like to:

a) If they meet the above conditions redirect them to a specific page
b) If they don't meet the above conditions leave them on the home page.

I would like it so that users are only redirected when they initally arrive on the homepage ie. if they navigate back to the home page from another page within my site they shouldn't be redirected again (only if arriving from an external location)

Is this possible? If so, does anyone know how to do this?

Thanks!

eelixduppy

12:15 am on Aug 6, 2009 (gmt 0)




if(is_user_logged_in() && (get_usernumposts(wp_get_current_user()->ID) > 0))
{
header('Location: http://www.example.com/good.php');
exit;
} else {
header('Location: http://www.example.com');
exit;
}

Something like that should do the trick. :)

almo136

12:36 am on Aug 6, 2009 (gmt 0)

10+ Year Member



I tried this by placing your code in the <head> section but got this error:

Warning: Cannot modify header information - headers already sent by (output started at /mysite/wp-content/themes/filebroswer/header.php:5) in /mysite/wp-content/themes/filebroswer/header.php on line 11

eelixduppy

12:39 am on Aug 6, 2009 (gmt 0)



You cannot call the header() function before content is output to the browser (because the headers will have already been sent out by then). You must place this before anything is output or it won't work correctly.

almo136

1:36 am on Aug 6, 2009 (gmt 0)

10+ Year Member



okay, got it working but now it is always going with the logged out option even when I'm logged in.

eelixduppy

2:19 am on Aug 6, 2009 (gmt 0)



Check to see what your functions are returning, they may be returning an incorrect value or a value you were not expecting. The code I gave you should work, so the error is elsewhere. I would start with those functions, though...

almo136

2:47 am on Aug 6, 2009 (gmt 0)

10+ Year Member



you're right. My site was always logging me out when I closed it.

All good now.

Thanks again!

almo136

4:11 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



I have one more question regarding this. I am now using this redirect code:

<?php
if(is_user_logged_in() && (get_usernumposts(wp_get_current_user()->ID) > 0) && is_home())
{
header('Location: http://example.com');
exit;
}
?>

I would like to replace http://example.com with a url which gets output using this code:

<?php $numposts = get_posts('sort_column=menu_order&numberposts=-1'); ?>
<?php foreach ($numposts as $numpost) {
if($numpost->post_author == wp_get_current_user()->ID) {
echo get_page_link($numpost->ID);
}
}
?>

How would I go about doing this?

Thanks.

eelixduppy

12:59 am on Aug 7, 2009 (gmt 0)



If I understand you correctly, perhaps something like this?...


$numposts = get_posts('sort_column=menu_order&numberposts=-1');
foreach ($numposts as $numpost)
{
if($numpost->post_author == wp_get_current_user()->ID)
{
header('Location: ' . urlencode(get_page_link($numpost->ID)));
exit;
}
}

almo136

8:53 pm on Aug 8, 2009 (gmt 0)

10+ Year Member



Thanks. That code works and redirects the user when they arrive at my site. The problem is it also redirects them when they navigate to the home page from within the site.

Is it possible to only redirect when they arrive at the home page from an external source?

almo136

5:26 pm on Aug 10, 2009 (gmt 0)

10+ Year Member



After some research I've realised that I should use HTTP Referer. I'm trying this code but i'm getting redirected even when the referrer is my site:

if(is_user_logged_in() && (get_usernumposts(wp_get_current_user()->ID) > 0) && is_front_page() && ($HTTP_REFERER != mysite.com ¦ www.mysite.com))