-1

I currently have a very basic WordPress site with a few pages.

There are no user accounts, only my own admin account.

What I'm looking for:

  1. A possibility for visitors to sign up i.e. register their own customer account. I don't need detailed account information, just an email address and perhaps a name and/or company name.

  2. Restrict certain content to logged in visitors only. Preferably when someone is not logged in I would want the page to show 'this information is only accessible for customers - log in or sign up here'. And if someone is logged in, show the actual content.
    Caveat: I'm dealing with a Woocommerce site and one of the things I'd like to restrict to logged in users only is the Shop page. But as I understood this is not a normal page with regular content in Wordpress. I guess its content is generated or controlled through the Woocommerce plugin. So is there a way to pull this off?

I'd assume this is a very common pattern in WordPress and this is probably very simple. But being a total WordPress newbie with zero experience, I wouldn't know how/where to start.

RocketNuts
  • 9,958
  • 11
  • 47
  • 88

1 Answers1

1

User Registration


Those are default WordPress behaviors. Enabling registration can be done via the admin control panel.

  • Settings General Membership, ✓ Anyone can register

Once this is done, users can registered @ http://localhost/www/wordpress/wp-login.php?action=register

Conditional Tags


Conditional Tags can be used in your Template Files to alter the display of content depending on the conditions that the current page matches. They tell WordPress what code to display under specific conditions. Conditional Tags usually work with PHP if/else Conditional Statements.

Restricting content to logged in user can be done via the is_user_logged_in() function.

Determines whether the current visitor is a logged in user.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Use Case Scenario


<?php

if ( is_user_logged_in() ) {

  // ... logged-in user content

} else {

  // ... non-logged-in user content

};

WooCommerce Conditional Tags


You can use is_shop() to determine if the current page is a shop page.

Returns true when on the product archive page (shop).

Usually the shop page is built around the archive-product.php page template. the following should englobe the content from the archive-product.php page template. You don't have to specify is_shop as we're building the conditional statement on the archive-product.php page template.

<?php

if ( ! defined( 'ABSPATH' ) ) {

    exit; 

};

/**
 * Basic logged-in restricted `archive-product.php` page template example.
 */ 
get_header();

if ( is_user_logged_in() ) {

  // ... logged-in user content

} else {

  // ... non-logged-in user content

};

get_footer();

Template hierarchy


As you're new to WordPress you should try to understand the basics in term of template hierarchy. You should refer to https://developer.wordpress.org/themes/basics/template-hierarchy/. WooCommerce works the same.

Template hierarchy
amarinediary
  • 4,930
  • 4
  • 27
  • 45
  • Thanks, this is very helpful! One addition question though, I actually want to restrict a Woocommerce shop page. But it seems this is not a regular content page that I can edit within WordPress. I edited my question accordingly. Is there an easy way to apply this method in Woocommerce as well? (preferably in a way that keeps working if WordPress or Woocommerce get updated in the future) – RocketNuts May 06 '21 at 05:42
  • The template used as the shop page is the `archive-product.php` or fallback to index. You can use `is_shop()` as a conditional statement. – amarinediary May 06 '21 at 14:26