Jump to content


Photo
- - - - -

Php Function To Customize Css Based On User Logged In Status


Best Answer Simon_P , 20 December 2012 - 06:12 PM

You dont need any php, WordPress adds a body tag of 'logged-in' if a user is logged in.

 

View the source of your page and look for the <body> tag, WordPress adds a lot of useful classes here for just this kind of thing.

Go to the full post


  • Please log in to reply
9 replies to this topic

#1 thomstark

thomstark

    Super Member

  • Members

  • 147 posts
  • Framework Version:Pagelines 2.37
  • Country: Country Flag

Posted 20 December 2012 - 05:49 PM

It's pretty simple; I'm just too much of novice to quite make it work. 

 

If a user is logged in, I want the nav pushed further to the right. If they are not logged in, I want the nav pushed not quite as far to the right. Why I want this is complicated, and tiring. It has to do with my login modal, and hooking it to the nav (via the branding and absolute positioning), and resizing the nav section so it wouldn't block the modal's link, etc. etc., and with the fact that when the user is logged in the login button goes away, so I want the nav pushed further to end where it did when the (fake) login nav item was there. Anyway, here's the crappy version of what I want to put in functions.php:

 

 

<?php if ( is_user_logged_in() ) : ?>
<style type="text/css">
#nav {padding-left:290px;}
</style>
<?php if ( !is_user_logged_in() ) : ?>
<style type="text/css">
#nav {padding-left:240px;}
</style>
<?php endif; ?>
 
 
Of course, it would be in functions, so it wouldn't need the php opener at the start. It might need some sort of initial "function" command, the likes of which is beyond me. Point is, I haven't been able to get it to work, after several iterations. 
 
And the reason I'm doing padding and not margin is because I have the nav centered via exact width+margin auto, so zooming doesn't affect it's placement. 
 
Thanks for any help.


#2 thomstark

thomstark

    Super Member

  • Members

  • 147 posts
  • Framework Version:Pagelines 2.37
  • Country: Country Flag

Posted 20 December 2012 - 05:52 PM

Oh yeah, the site is bringtheharvest.com. 



#3 Simon_P

Simon_P

    Messer

  • Administrators



  • 8388607 posts
  • LocationDevon
  • Framework Version:2.1.1
  • Country: Country Flag

Posted 20 December 2012 - 06:12 PM   Best Answer

You dont need any php, WordPress adds a body tag of 'logged-in' if a user is logged in.

 

View the source of your page and look for the <body> tag, WordPress adds a lot of useful classes here for just this kind of thing.


  • thomstark likes this

#4 thomstark

thomstark

    Super Member

  • Members

  • 147 posts
  • Framework Version:Pagelines 2.37
  • Country: Country Flag

Posted 20 December 2012 - 06:15 PM

Cool. I see the logged in class in the body tag. Now how would I go about putting that to use to control my variable? Pretend I have no idea what I'm doing. (You won't have to pretend very hard.)



#5 Simon_P

Simon_P

    Messer

  • Administrators



  • 8388607 posts
  • LocationDevon
  • Framework Version:2.1.1
  • Country: Country Flag

Posted 20 December 2012 - 06:19 PM

Please Login or Register to see this Hidden Content



#6 thomstark

thomstark

    Super Member

  • Members

  • 147 posts
  • Framework Version:Pagelines 2.37
  • Country: Country Flag

Posted 20 December 2012 - 06:19 PM

Would it be something like this?

 

 

Please Login or Register to see this Hidden Content



#7 thomstark

thomstark

    Super Member

  • Members

  • 147 posts
  • Framework Version:Pagelines 2.37
  • Country: Country Flag

Posted 20 December 2012 - 06:19 PM

Ah, I was way off! :) Thanks much!



#8 thomstark

thomstark

    Super Member

  • Members

  • 147 posts
  • Framework Version:Pagelines 2.37
  • Country: Country Flag

Posted 20 December 2012 - 06:20 PM

Hmmm, that didn't quite work.



#9 thomstark

thomstark

    Super Member

  • Members

  • 147 posts
  • Framework Version:Pagelines 2.37
  • Country: Country Flag

Posted 20 December 2012 - 06:31 PM

I had to play around with the measurements, but this is the one that ended up working how I needed it to:

 

body.logged-in nav { margin-left:35px; padding-left:0px; width:665px;}

 

Thanks tonnes, Simon!



#10 Simon_P

Simon_P

    Messer

  • Administrators



  • 8388607 posts
  • LocationDevon
  • Framework Version:2.1.1
  • Country: Country Flag

Posted 20 December 2012 - 06:32 PM

Your welcome :)