Jump to content

PHP If Statement Help (Wordpress)


Man2u2uk

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?

Link to comment
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> 

Link to comment
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>

Link to comment
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.

Link to comment
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 :)

 

 

 

Link to comment
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?

 

Link to comment
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?

Link to comment
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

Link to comment
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.

Link to comment
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?

 

 

Link to comment
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?

 

 

Link to comment
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.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.