Skip to content

Instantly share code, notes, and snippets.

@rahularyan
Created November 11, 2017 03:30
Show Gist options
  • Save rahularyan/ce3fb514f1e63a91611b564535732f56 to your computer and use it in GitHub Desktop.
Save rahularyan/ce3fb514f1e63a91611b564535732f56 to your computer and use it in GitHub Desktop.
<?php
/**
* Override ap_user_can_ask function.
*
* @param string|boolean $ret Return empty string to continue operation.
* @param integer $user_id
* @return string|false
*/
function my_filter_ap_user_can_ask( $ret, $user_id ) {
$allow = true;
$private_categories = array( 'literasi-perbendaharaan' ); // categories subscribers cannot see
if ( is_single() ) {
$cats = wp_get_object_terms( get_queried_object()->ID, ‘question_category’, array( 'fields' => 'slugs' ) ); // get the categories associated to the required post
if ( array_intersect( $private_categories, $cats ) ) {
// post has a reserved category, let’s check user
$allow = check_user();
}
} elseif ( is_tax( 'question_category', $private_categories ) ) {
// the archive for one of private categories is required, let’s check user
$allow = check_user();
}
// Return empty string to allow normal operation.
return $allow ? '' : false;
}
add_filter( 'ap_user_can_ask', 'my_filter_ap_user_can_ask' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment