uo_restrict_content_no_access
Filters the content displayed when a user lacks access to restricted content.
add_filter( 'uo_restrict_content_no_access', $callback, 10, 2 );
Description
Filters the content displayed when a user lacks access via the `[member_style]` shortcode. Developers can use this hook to customize the "no access" message, add alternative content, or even completely modify the output for restricted users. The hook receives the shortcode attributes and the original content.
Usage
add_filter( 'uo_restrict_content_no_access', 'your_function_name', 10, 2 );
Parameters
-
$atts(mixed) - This parameter is an empty string and is not used by the hook.
-
$contents(mixed) - This parameter contains an array of attributes passed to the shortcode.
Return Value
The filtered value.
Examples
<?php
/**
* Example function to handle content restriction when a user does not have access.
*
* This filter is triggered when the 'uo_restrict_content' shortcode determines
* that the current user should not see the content. It allows for custom
* messages or redirects to be displayed instead.
*
* @param string $no_access_message The default message or value to return (often empty string).
* @param array $atts The attributes passed to the 'uo_restrict_content' shortcode.
* @param string $contents The content enclosed within the 'uo_restrict_content' shortcode.
*
* @return string The HTML to display to the user who does not have access.
*/
add_filter( 'uo_restrict_content_no_access', function ( $no_access_message, $atts, $contents ) {
// If the user is not logged in, display a generic message asking them to log in.
if ( ! is_user_logged_in() ) {
return '<div class="uo-content-restriction-message">Please <a href="' . esc_url( wp_login_url( get_permalink() ) ) . '">log in</a> to access this content.</div>';
}
// If the user is logged in but doesn't meet the group requirements (handled by the shortcode logic),
// we can display a more specific message.
// For example, if the shortcode specified required groups:
if ( isset( $atts['user_groups'] ) && 'all' !== $atts['user_groups'] ) {
$required_groups = explode( ',', $atts['user_groups'] );
$user_groups = learndash_get_users_group_ids( get_current_user_id() );
$has_access = false;
if ( is_array( $user_groups ) && ! empty( $user_groups ) ) {
foreach ( $required_groups as $required_group_id ) {
if ( in_array( trim( $required_group_id ), $user_groups ) ) {
$has_access = true;
break;
}
}
}
if ( ! $has_access ) {
return '<div class="uo-content-restriction-message">You do not have access to this content. Please ensure you are in the correct group(s).</div>';
}
}
// Fallback to a generic message if no specific conditions are met, or if the default empty string is preferred.
return '<div class="uo-content-restriction-message">Content restricted.</div>';
}, 10, 3 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/shortcodes/member-style-shortcode.php:49
src/classes/shortcodes/member-style-shortcode.php:54
src/classes/shortcodes/member-style-shortcode.php:62
src/classes/shortcodes/member-style-shortcode.php:97
src/classes/shortcodes/member-style-shortcode.php:102
src/classes/shortcodes/member-style-shortcode.php:110
public function uo_restrict_content( $atts, $contents = null ) {
//check tha the user is logged in
if ( is_user_logged_in() ) {
if ( ! current_user_can( 'administrator' ) ) {
$user = wp_get_current_user();
$user_id = $user->ID;
$user_groups = learndash_get_users_group_ids( $user_id );
$atts = shortcode_atts(
array(
'user_groups' => 'all',
),
$atts,
'uo_restrict_content' );
if ( 'all' === $atts['user_groups'] ) {
return do_shortcode( $contents );
} else {
$group_ids = explode( ',', $atts['user_groups'] );
if ( is_array( $group_ids ) ) {
foreach ( $group_ids as $g_id ) {
if ( in_array( $g_id, $user_groups ) ) {
return do_shortcode( $contents );
}
}
return apply_filters( 'uo_restrict_content_no_access', '', $atts, $contents );
} else {
if ( in_array( $atts['user_groups'], $user_groups ) ) {
return do_shortcode( $contents );
} else {
return apply_filters( 'uo_restrict_content_no_access', '', $atts, $contents );
}
}
}
} elseif ( current_user_can( 'administrator' ) ) {
return do_shortcode( $contents );
}
} else {
return apply_filters( 'uo_restrict_content_no_access', '', $atts, $contents );
}
}