Redirect user to login before viewing custom post

I have a site with a listings page () and all I'm trying to do is redirect a user who is not logged in to my login

I have a site with a listings page (/) and all I'm trying to do is redirect a user who is not logged in to my login page (/) before they can view an individual listing. If they are logged in then they can go straight to the individual listing.

I'd like to do this without having to use a plugin as this seems like it should be a relatively easy thing to do via php.

Thanks!

I have a site with a listings page (https://gymkoi/class-listings/) and all I'm trying to do is redirect a user who is not logged in to my login page (https://gymkoi/login/) before they can view an individual listing. If they are logged in then they can go straight to the individual listing.

I'd like to do this without having to use a plugin as this seems like it should be a relatively easy thing to do via php.

Thanks!

Share Improve this question asked May 21, 2020 at 17:34 Jon TimmsJon Timms 132 bronze badges 2
  • Quick question, after they login are they then directed to a page of listings or a specific listing? – Tony Djukic Commented May 21, 2020 at 20:12
  • eventually i'd like them to go to the listing they clicked on – Jon Timms Commented May 21, 2020 at 21:41
Add a comment  | 

1 Answer 1

Reset to default 2

You could set up a child theme, and in the single-listing.php template, add conditions around the current code.

<?php
// If the user is not logged in, redirect
if ( false == is_user_logged_in() ) {
    wp_redirect( wp_login_url() );
    exit;
}
// Else the user is logged in; show the listing
else {
    // Paste the regular template code here
}
?>

Everything else will remain public, while just the individual listings will force the user to be logged in before they can view the content. (This will also prevent search engines from seeing the content, so bear that in mind.)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742412303a4439054.html

相关推荐

  • Redirect user to login before viewing custom post

    I have a site with a listings page () and all I'm trying to do is redirect a user who is not logged in to my login

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信