K
K
kirill2709992021-09-24 20:38:01
Payment systems
kirill270999, 2021-09-24 20:38:01

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

1 answer(s)
I
Ivan K, 2021-09-28
@kirill270999

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);
}

In places where you need to hide content for ordinary users - a simple design
// Опредяляем роль
$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())

It's simple and no plugins are needed. According to this principle, by the way, personal accounts are made and much more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question