If user is admin, show this content. Need a suggestion.

0

I'm planning to setup a simple draft/publish pick field for a staging site. I want the logged in user to see all records whether published or in draft, and users that aren't logged in to see only published items.

I was going to run the check to see if the user is logged in and, if so, display wordpress template A. If they aren't logged in, display template B. This would work for the listing pages where i could change findRecords to display published/draft or both, but I'm kind of stumped for the Detail pages. A user could still get to a detail page if they have the URL. It's also not ideal to maintain two template files.

Any suggestions for a better way of doing it?

asked Feb 8 at 3:17

cmacholz

5

add comment
enter at least 15 characters

1 Answer

1

I do something similar on one of my sites.

For showing all items to logged in users and only published items to the unwashed masses I use code sort of like this:

if (is_user_logged_in() ) {
    $where = '';
} else {
    $where = 't.published = 1'
}
$pods->findRecords('t.id ASC', -1, $where);

On detail pages, I don't hide the page from them. I just tell them they aren't allowed to see it:

 $pods = new Pod ('my-pod', pods_url_variable('last') );
 $is_draft = $pods->get_field('published');
 if ($is_draft && ! is_user_logged_in() ) :
     echo "<h2>Sorry</h2>\n<p>You must be logged in to see this page</p>\n";
 else :
     // Show Data
 endif;

You could also redirect them to a 404 page if you wanted to hide the existence of that page completely. Take a look at the import package to see how that is done.

answered Feb 10 at 12:25

chris.pilko

889

You are quite an asset to this forum, thank you so much for this! – cmacholz Feb 10 at 5:07
add comment
enter at least 15 characters