Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created February 13, 2015 19:05
Show Gist options
  • Save mikejolley/bf70a6cbe0610d4b3528 to your computer and use it in GitHub Desktop.
Save mikejolley/bf70a6cbe0610d4b3528 to your computer and use it in GitHub Desktop.
WP Job Manager Resumes & WooCommerce Subscriptions - If resumes require a 'has_active_subscription' capability, limit access to only those with an active subscription.
// Hook into user_has_cap filter. This assumes you have setup resumes to require the capability 'has_active_subscription'
add_filter( 'user_has_cap', 'has_active_subscription_capability_check', 10, 3 );
/**
* has_active_subscription_capability_check()
*
* Filter on the current_user_can() function.
*
* @param array $allcaps All the capabilities of the user
* @param array $cap [0] Required capability
* @param array $args [0] Requested capability
* [1] User ID
* [2] Associated object ID
*/
function has_active_subscription_capability_check( $allcaps, $cap, $args ) {
// Only interested in has_active_subscription
if ( empty( $cap[0] ) || $cap[0] !== 'has_active_subscription' ) {
return $allcaps;
}
$user_id = $args[1];
$subscription_product_id = '116'; // Customise me!
if ( WC_Subscriptions_Manager::user_has_subscription( $user_id, $subscription_product_id ) ) {
$allcaps[ $cap[0] ] = true;
}
return $allcaps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment