Answer the question
In order to leave comments, you need to log in
How to give access to only four records, open the rest only after payment?
Hello! Please advise how to make it so that only 4 records are available to the user, and after payment everything else? What plugin to use?
Answer the question
In order to leave comments, you need to log in
It can be done without a plugin. Create a user role "Pro" for example, assign after payment.
add_action( 'after_switch_theme', 'add_pro_role' );
function 'add_pro_role() {
add_role( 'pro', 'Pro', ['read' => true]);
}
// далее простая функция для вывода роли
function get_user_role($user_id) {
global $wp_roles;
$roles = array();
$user = new WP_User( $user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
$roles[] .= translate_user_role($wp_roles->roles[$role]['name']);
}
return implode(', ',$roles);
}
// Опредяляем роль
$current_user_id = get_current_user_id();
$current_user_role = get_user_role( $current_user_id );
// если Про
if($current_user_role == 'Pro') {
// Выводим скрытый контент
} else {
// Выводим отлуп, например
echo 'Вам не разрешено просматривать данную страницу';
}
так же перед этим не мешает проверить залогинен ли вообще пользователь (is_user_logged_in())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question