Skip to main content

Posts

Showing posts from April, 2021

How to make woocommerce pages accessible to logged in users only

Sometimes we need to set up private online shop. The easiest and fastest way to set up an online shop is to set up WordPress with WooCommerce. In WordPress, we can easily add a functionality that prevents guest users to access the products/shop page. Here is the code which needs to be in functions.php in theme files. function guest_user_redirect() {     if (! is_user_logged_in() && (is_woocommerce() || is_cart() || is_checkout())) {         wp_redirect(home_url());          exit;     } } add_action('template_redirect', 'guest_user_redirect');

Create two separate menu items for logged in and logged out users for WordPress

We can place two separate menu items for same position in WordPress. That means, if a user logged into the website, the user will see the available menus for logged in users. When the user log out from the website, he/she will se different menu which is for all guests. To do that, simply create two menu from WordPress dashboard. It could contain any name of your choice. For example, the name of the menus could be "for logged in users" and "for logged out users". Then, you have to declare the menu locations, for example:  register_nav_menus( array( 'loggedin' => esc_html__( 'Logged In Menu', 'theme_name' ), 'loggedout' => esc_html__( 'Logged Out Menu', 'theme_name' ), ) ); This will add the options on menus page. Next, after assigning the menus to the respective menu locations, we have to show the menu in the theme:  $args = array(                         'theme_location' => 'loggedin', ...