Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Created July 7, 2018 17:11
Show Gist options
  • Save bdeleasa/50839e802dcef59ab8df84c31d2440a6 to your computer and use it in GitHub Desktop.
Save bdeleasa/50839e802dcef59ab8df84c31d2440a6 to your computer and use it in GitHub Desktop.
Wordpress plugin that redirects any non-logged-in users to the home page if they try to access any portion of WooCommerce.
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @link http://example.com
* @since 0.0.1
* @package WooCommerce_Private_Shop
*
* @wordpress-plugin
* Plugin Name: WooCommerce Private Shop
* Plugin URI: https://briannadeleasa.com
* Description: Makes the WooCommerce products and shop only accessible to logged in users.
* Version: 1.0.0
* Author: Brianna Deleasa
* Author URI: https://briannadeleasa.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: woocommerce-private-shop
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_action( 'template_redirect', 'woocommerce_private_shop_redirect' );
/**
* Redirects the user to the home page if they try and access a
* WooCommerce shop page and they aren't logged into the website.
*
* @since 1.0.0
*
* @param null
* @return null
*/
function woocommerce_private_shop_redirect() {
if (
! is_user_logged_in()
&& ( is_woocommerce() || is_cart() || is_checkout() )
) {
wp_redirect( home_url() );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment