0

Building a website on wordpress Trying to make a couple of divs visible to logged in users only. I've set display:none in stylesheet and here is how i'm trying to make it visible to logged in users. Not working though...

<?php
if ( is_user_logged_in() ) {
?>
<script type="text/javascript">
$(document).ready(function(){
$(".single_add_to_cart_button").css('display','block');
$(".add_to_cart_button").css('display','block');
});
</script>
<? } ?>

If anyone can think of other ways- it will be greatly appreciated as well

1
  • Can you post the markup (HTML) of your div? Commented May 1, 2014 at 22:56

2 Answers 2

1

As long as is_user_logged_in() is actually working, I like to just output a class to the body. This class can then be used in numerous ways (both js and css).

Something like:

<body<?php echo (is_user_logged_in() ? ' class="logged-in"' : ''); ?>>

This will produce either

<body>

Or

<body class="logged-in">

And some css like this:

.single_add_to_cart_button {
    display: none;
}
.logged-in .single_add_to_cart_button {
    display: block;
}

For js, you can just check

if ($('body').hasClass('logged-in')) {
    // Do whatever
}
Sign up to request clarification or add additional context in comments.

Comments

0

The best way to do this is to forget having a hidden div for unauthenticated users - anyone could unhide this content if they want to, which in some cases could be quite bad depending on the rest of your system. It also means you have to run JavaScript to make things work as well as PHP.

What you should really do is simply check if the user is logged in with PHP and only output the div at all in that case. Something like this:

<?php
if ( is_user_logged_in() ) {
?>
<div class="blah...">
Content here
</div>
<?php } ?>

If you want to post the markup of your hidden divs though, and still want to do it how you're doing it, maybe we can find why it's not working.

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.