На публичной странице пользователя (single page/user page) отобразим/скроем элемент (dynamic visibility) в зависимости от роли пользователя этой публичной страницы, а не роли пользователя, который просматривает страницу.
//Динамически отображать элементы в зависимости от роли пользователя на публичной странице пользователя
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $manager ) {
if ( ! class_exists( '\Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base' ) ) {
return;
}
if ( ! class_exists( 'DFCC_Displayed_User_Role' ) ) {
class DFCC_Displayed_User_Role extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base {
public function get_id() {
return 'displayed-user-role';
}
public function get_name() {
return __( 'Displayed User Role is', 'jet-engine' );
}
public function get_group() {
return 'user';
}
public function is_for_fields() {
return false;
}
public function need_value_detect() {
return false;
}
// Список ролей для выбора в настройках условия
public function get_custom_controls() {
global $wp_roles;
$user_roles = array();
foreach ( $wp_roles->roles as $role_id => $role ) {
$user_roles[ $role_id ] = $role['name'];
}
return array(
'displayed_user_role' => array(
'label' => __( 'Displayed user role', 'jet-engine' ),
'type' => 'select2',
'multiple' => true,
'default' => array(),
'options' => $user_roles,
'placeholder' => __( 'Select role...', 'jet-engine' ),
),
);
}
// Пользователь, чья страница сейчас открыта (Profile Builder Single User Page),
// а не тот, кто на неё смотрит.
protected function get_displayed_user() {
$data = jet_engine()->listings->data;
$object = $data->get_current_object();
if ( $object instanceof \WP_User ) {
return $object;
}
// fallback: author archive / другие случаи
$object = $data->get_queried_user_object();
return ( $object instanceof \WP_User ) ? $object : false;
}
public function check( $args = array() ) {
$type = ! empty( $args['type'] ) ? $args['type'] : 'show';
$roles = ! empty( $args['condition_settings']['displayed_user_role'] )
? $args['condition_settings']['displayed_user_role']
: array();
if ( ! is_array( $roles ) ) {
$roles = array( $roles );
}
$user = $this->get_displayed_user();
$has_role = false;
if ( $user ) {
foreach ( $roles as $role ) {
if ( in_array( $role, (array) $user->roles, true ) ) {
$has_role = true;
break;
}
}
}
return ( 'hide' === $type ) ? ! $has_role : $has_role;
}
}
}
$manager->register_condition( new DFCC_Displayed_User_Role() );
} );
И у Вас появится новый параметр в dynamic visibility
