0

Situation: I'm building a Wordpress Site. Menu becomes 'fixed' to the top of the page when scrolling down the page (http://deerfielddesigns.com.mlseo.net/). When i am logged into wordpress, the dashboard admin bar cover the menu. I wanted to create a different class for the menu when I am logged in as opposed to when I am logged out.

Code:

    if ( direction === 'down' ) {

        $('#main-header').addClass( 'et-fixed-header' );
    } else {
        $('#main-header').removeClass( 'et-fixed-header' );
    }

I wanted to insert an "if ( is_user_logged_in() )" statement in there to change the class output but I don't really understand too much about javascript and if it plays nice with php. Does anyone have any insight as to what i need to do to make this work? Thanks all!

2
  • You can't call a PHP function from Javascript. You'd need an AJAX call to a PHP script to do that. Commented Jan 8, 2014 at 1:07
  • 1
    He doesn't really want to call a PHP function, he just poorly stated the question. What he wants is to use jQuery to manipulate classes if user is logged in (see his code). Commented Jan 8, 2014 at 1:09

5 Answers 5

1

if this is not an AJAX driven login system, simply open your header.php file and perform the following

$the_class = is_user_logged_in() ? 'logged-in-class' : 'logged-out-class';

Then find your main-header and do what's required.

<header id="main-header" class="<?php echo $the_class;">

If it is an ajax driven login system, you'll need to understand how to work with wordpress' add_action, but that is an answer for an entirely different question.

Sign up to request clarification or add additional context in comments.

Comments

0

Make an ajax request to the required php function whenever required. You may use ajax features of various JS libraries like jQuery, YUI etc. to do so.

2 Comments

This is true in general, but if you read his question you'll see that he's trying to tell if the user is logged in inside a scroll event. Sending out ajax requests every time the user scrolls is a bad idea...
Agreed; but why should one actually check if a user is logged in inside a scroll event? That is insane right?
0

PHP is a server side language (which quite likely is generating Javascript). Javascript is in the browser.

The short answer is "You can't easily call PHP from JS".

Comments

0

The simple way to identify from you javascript if a user is logged in is to populate a javascript variable with their status. In your theme's header.php, add:

<script type="text/javascript">
    var is_logged_in = <?php echo json_encode(is_user_logged_in()) ?>;
</script>

Then, in your javascript elsewhere, you can use:

if (is_logged_in) {
   //....
}

1 Comment

I attempted this but for some reason my 'else' statements weren't working when I wanted to add a class if the user was not logged in. Probably some error on my end, but I got it working using Ohgodwhy's solution. Thanks for your help!
0

If you write inline javascript in your php files you can do this:

<?php  // some php code ?>

<script type="text/javascript">

<?php if ( is_user_logged_in() ) { ?>
    // Place some javascript here
<?php } else { ?>
    // Place some javascript here
<?php ?>

</script>

<?php  // some php code ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.