Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am in process of writing a plugin that has front-end shortcodes that display a user's information/data. If the user is not logged in, I need to redirect to a front-end login page/form. What would be the best practice here? My plugin creates the pages that I use as well as add the shortcodes to those pages. So, if there is a way to "protect" those pages I would love to know.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Not sure about the best practices, but I have a few custom login-sensitive pages which simply display a message if user is not logged and is trying to view the page directly:

$logged_in = is_user_logged_in();

if($logged_in) {
?>
<article id="post">
        <?php the_content(); ?>
</article>
<?php
} else {
    _e('You are not logged in. Please ', 'abc');
    echo '<a href="' . site_url( 'login' ) . '">'. __('log in','abc') .'</a> ' . __('or','abc') . ' ';
    echo '<a href="' . site_url( 'register' ) . '">'. __('register','abc') .'</a>.';
}

Note: both the login and register pages are custom pages as well.

share|improve this answer
    
Thanks for the response. Is there a way to redirect the user to my front-end login page without getting the "Warning Headers Already Sent" message? –  ClockwiseQ May 30 '13 at 2:27
    
Hm. By the looks of it this snippet should work. If you want conditions then a plugin could be the way to go, such as this one. –  dalbaeb May 30 '13 at 2:34
add comment

You can't redirect users from a shortcode. Shortcodes run too late-- well after content has been sent. But given that your plugin is creating the Pages you should be able to do this:

function protect_plugin_page_wpse_101230() {
  if(is_page('page-slug') && !is_user_logged_in()) {
    wp_safe_redirect('your-login-page-url');
  }
}
add_action('template_redirect','protect_plugin_page_wpse_101230');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.