Jump to content

Archived

This topic is now archived and is closed to further replies.

Man2u2uk

PHP If Statement Help (Wordpress)

Recommended Posts

Hi All,

 

I'm currently using the s2member plugin for wordpress to allow me to create a client portal where users can log in and visit their own secure unique page. (This uses custom capability and the page must be called the same as their username.)

 

But then i also have people that just log on to view my blog and are not clients and do not have pages created for them.

 

I have the following code:

 

 

<a href="?php echo S2MEMBER_CURRENT_USER_LOGIN; ?>/">Client Area</a> 

 

This works fine when logged in as a user who has a page created but gives an 404 error for a user that does not have a page.

 

My question is:

 

Could the above code be wrapped in an if statement to say that if the S2MEMBER_CURRENT_USER_LOGIN does not = post_name then redirect them to the home page?

Share this post


Link to post
Share on other sites

I doubt this is correct.

 

But i need something like this:

 

$S2MEMBER_CURRENT_USER_LOGIN = username;

if ( $username == post_name ) {
header( 'Location: http://www.yoursite.com?username' ) ;

} else {
header( 'Location: http://www.example.com/home' ) ;

}

 

to link in with:

 

<a href="?php echo S2MEMBER_CURRENT_USER_LOGIN; ?>/">Client Area</a> 

Share this post


Link to post
Share on other sites

Unfortunatly not darkfreaks,

 

Maybe i did not explain my self correctly.

 

The way my client portal works is that if i have a user called Alex and a page called Alex and he clicks on the "Client Area" link.

 

The following code will take them to their page

<a href="?php echo S2MEMBER_CURRENT_USER_LOGIN; ?>/">Client Area</a>

 

The issue i have is that if i have a user called Ben and he does not have a page and clicks on the "Client Area" link he will get a 404 error.

 

I somehow need to wrap an if statement around the following link

<a href="?php echo S2MEMBER_CURRENT_USER_LOGIN; ?>/">Client Area</a>

Share this post


Link to post
Share on other sites

<a href="?php echo (isset(S2MEMBER_CURRENT_USER_LOGIN) ? S2MEMBER_CURRENT_USER_LOGIN : 'home_page_url');?>/">Client Area</a>

 

You might need to also check the string length of the login.

Also, you have it set as a constant in some of your examples, and a variable in others, so figure out which it is.

Share this post


Link to post
Share on other sites

Thanks for the reply.

 

I will test this code out this evening when i get home. As per your comment:

 

Also, you have it set as a constant in some of your examples, and a variable in others, so figure out which it is.

 

It should be S2MEMBER_CURRENT_USER_LOGIN :)

 

 

 

Share this post


Link to post
Share on other sites

Hi,

 

I have just logged onto my machine and pasted the following code,

<a href="<?php echo (isset(S2MEMBER_CURRENT_USER_LOGIN) ? S2MEMBER_CURRENT_USER_LOGIN : 'home');?>/">Client Area</a>

 

but it gives the following error:

 

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM A quick Google mentions its about the scope operator ( :: ) but i'm not sure what this means or how to fix it?

 

Share this post


Link to post
Share on other sites

try

<a href="<?php echo (defined('S2MEMBER_CURRENT_USER_LOGIN') ? S2MEMBER_CURRENT_USER_LOGIN : 'home');?>/">Client Area</a>

Share this post


Link to post
Share on other sites

Thanks,

 

<a href="<?php echo (defined('S2MEMBER_CURRENT_USER_LOGIN') ? S2MEMBER_CURRENT_USER_LOGIN : 'home');?>/">Client Area</a>

 

That code stopped the error but its still redirecting the users who don't have a page created to an 404 error.

 

i think if S2MEMBER_CURRENT_USER_LOGIN does not match post_name then redirect needs to be added into the code somehow?

Share this post


Link to post
Share on other sites

maybe just because your 'home' could be incorrectly defined... so <a href="home/"> need to be reviewed for  existence

Share this post


Link to post
Share on other sites

Sorry.

 

I understand what you mean but not sure how to implement it?

 

i tried the following:

 

<a href="<?php echo (defined('S2MEMBER_CURRENT_USER_LOGIN') ? S2MEMBER_CURRENT_USER_LOGIN : '<a href="home/"</a>>');?>/">Client Area</a>

 

But that did nothing as i thought it was as i'm a PHP noob lol

Share this post


Link to post
Share on other sites

How does the value of S2MEMBER_CURRENT_USER_LOGIN relate to whether or not they have a page?

Share this post


Link to post
Share on other sites

It doesn't.

 

S2MEMBER_CURRENT_USER_LOGIN just grabs the current logged in users username and because i hyperlinked it if a page exists with the same name it just takes them to it and if it does not match takes the user to a 404 page..

 

post_name is what wordpress uses to store the page name in the database.

Share this post


Link to post
Share on other sites

in your website... if you move your cursor to whatever point that link to your home page, how that link looks like exactly... maybe something like "www.yourwebsitehere/index.php" ??

Share this post


Link to post
Share on other sites

So do you realize why, if they don't relate to each other, making the IF statement dependent on that won't work?

 

try it with the post_name variable instead.

Share this post


Link to post
Share on other sites

Sorry jesirose, maybe they do sort of link together as they equal the same value?

 

I tried the following but that just redirected everyone to the home page:

 

<a href="<?php echo (defined('post_name') ? post_name : 'home');?>/">Client Area</a>

 

I thought an IF statement would work as we could check to see if S2MEMBER_CURRENT_USER_LOGIN is equal to post_name if show redirect to their page and if not redirect to the home page?

 

 

Share this post


Link to post
Share on other sites

and what about

<a href="<?php echo (defined('S2MEMBER_CURRENT_USER_LOGIN') ? S2MEMBER_CURRENT_USER_LOGIN : post_name);?>/">Client Area</a>

Share this post


Link to post
Share on other sites

before this line

<a href="<?php echo (defined('S2MEMBER_CURRENT_USER_LOGIN') ? S2MEMBER_CURRENT_USER_LOGIN : post_name);?>/">Client Area</a>

did you check that the constant S2MEMBER_CURRENT_USER_LOGIN and/or post_name are not null or empty?

 

 

Share this post


Link to post
Share on other sites

is post_name a constant or a variable?

defined() is for constants.

Share this post


Link to post
Share on other sites

I think post_name is a variable. its stored in the database as post_name.

Share this post


Link to post
Share on other sites

Where is it in the code though?

Just because the column is called post_name, the variable might be $post_name or $postName; or $post, you need to find it in the code.

Share this post


Link to post
Share on other sites

before this line

<a href="<?php echo (defined('S2MEMBER_CURRENT_USER_LOGIN') ? S2MEMBER_CURRENT_USER_LOGIN : post_name);?>/">Client Area</a>

did you check that the constant S2MEMBER_CURRENT_USER_LOGIN and/or post_name are not null or empty?

 

Hi mikosiko, that is the only line of code i have.

 

Where is it in the code though?

Just because the column is called post_name, the variable might be $post_name or $postName; or $post, you need to find it in the code.

 

Hi jesirose, post_name (without the $ )is referenced alot throughout the wordpress code.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.