Created
December 28, 2015 22:33
-
-
Save UltimateWoo/eab4fc30635176add621 to your computer and use it in GitHub Desktop.
Check if the current user has an active WooCommerce subscription
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check if the current user has an active subscription. | |
* Redirect the user if no active subscription and the current post is a singule forum. | |
* This code should not be copied and pasted as is. It is only to demonstrate the wcs_user_has_subscription() function. | |
* | |
* wcs_user_has_subscription( $user_id = 0, $product_id = '', $status = 'any' ) | |
* @param int (optional) The ID of a user in the store. If left empty, the current user's ID will be used. | |
* @param int (optional) The ID of a product in the store. If left empty, the function will see if the user has any subscription. | |
* @param string (optional) A valid subscription status. If left empty, the function will see if the user has a subscription of any status. | |
*/ | |
add_action( 'template_redirect', 'ultimatewoo_check_user_subscriptions' ); | |
function ultimatewoo_check_user_subscriptions() { | |
$has_sub = wcs_user_has_subscription( '', '', 'active' ); | |
if ( ! $has_sub && is_singular( 'forum' ) ) { | |
wp_redirect( get_permalink( 2626 ) ); | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment