Join 3,562 readers in helping fund MetaFilter (Hide)


Actually PHP, your mom is an unexpected T_ELSE
August 31, 2012 8:14 AM   Subscribe

PHP / Wordpress filter: I need to hide some aspects of my site behind if ( is_user_logged_in() ) functions, but being a newbie to PHP I don't think I'm getting the syntax right. Specific code is beneath the jump.

The basic idea is that I want some parts of my site to be front-facing, while retaining basic privacy of user content against non-users and search robots. Part of this includes my custom made widget areas, here, and part of this also includes custom loops that are displaying specific categories on my site, which is pretty typical loop-type stuff.

I've tried a couple of different ways to wrap the is_user_logged_in () loop around these different snippets of code, but I think that my general lack of knowledge in PHP is making it not work properly, either throwing errors, or not throwing errors but also not working. This is an example of a current attempt which is currently throwing "Parse error: syntax error, unexpected T_ELSE in the-file-name.php on line 55" when it tries to render.

Apart from the main question, is there a better way to do this that I'm missing? I know that I can include not-logged-in redirect code in to my functions.php file, but the project manager is concerned that this sort of redirect would also be confusing to our users, who are typically younger and less technologically savvy.
posted by codacorolla to Computers & Internet (4 answers total)
 
In that particular example with the syntax error, you're missing a closing brace } before else.

More generally, you're mixing up the two different ways of opening and closing blocks of code in PHP. There's the IF : ENDIF way and the IF { } way. You'll probably find it easier to keep track if you stick to one or the other. I tend to use the {} way when it's pure PHP code, and the : way when it's HTML with bits of PHP stuck in it.

To me, this seems like a fine way to do what you're doing - show one bit of content to logged in users, and a different bit of content to the rest.
posted by moonmilk at 8:42 AM on August 31, 2012 [1 favorite]


You're not matching your braces correctly. Why do you mix the {} syntax and the :, endif syntax? You should pick one and stick with it. I've written a little gist that will hopefully clarify.
posted by yaymukund at 8:43 AM on August 31, 2012


Thanks! I looked over the code so many times and didn't catch that. It works perfectly now.

The main reason for the disparity in if/then statements is that I don't really know what I'm doing, so a main part of my workflow is trying to format the right search term in Google to get sample code and then tweak it for my purposes. The custom widget area just happened to use the endif syntax, I guess. Once I go through and try to clean up the code (probably a long-term goal) I'll try to standarize stuff.
posted by codacorolla at 8:48 AM on August 31, 2012


Yeah, my response came off a little more brusque than I had intended. For what it's worth, everyone makes that mistake and PHP does kind of suck at telling you what you did wrong. Have fun!
posted by yaymukund at 8:49 AM on August 31, 2012


« Older Following Jacob Appelbaum and ...   |  Back pain and swallowing diffi... Newer »
This thread is closed to new comments.