uo_ld_forum_access_restricted_message
Filters the restricted access message shown to users when they attempt to access a LearnDash forum.
add_filter( 'uo_ld_forum_access_restricted_message', $callback, 10, 3 );
Description
Filters the message displayed when LearnDash forum access is restricted. Developers can modify the default message or provide custom content. The hook passes the current message content, the forum ID, and an array of the user's associated forums, allowing for dynamic message generation based on user and forum context.
Usage
add_filter( 'uo_ld_forum_access_restricted_message', 'your_function_name', 10, 3 );
Parameters
-
$content(mixed) - This parameter contains the HTML content of the message displayed to users when forum access is restricted.
-
$forum_id(mixed) - This parameter contains the HTML content that will be displayed to the user when forum access is restricted.
-
$user_forums(mixed) - This parameter contains the ID of the forum for which access is being restricted.
Return Value
The filtered value.
Examples
/**
* Filters the message displayed when forum access is restricted.
*
* This function checks if the current user has access to the specified forum
* based on their associated user forums. If access is restricted, it displays
* a custom message.
*
* @param mixed $content The current message content.
* @param mixed $forum_id The ID of the forum being accessed.
* @param mixed $user_forums The forums the user has access to.
* @return string The modified message content or the original content if no restriction.
*/
function my_custom_ld_forum_access_restricted_message( $content, $forum_id, $user_forums ) {
// If $content is already false, it means access is restricted by default
// and we want to replace the default restriction message.
if ( $content === false ) {
$custom_message = get_post_meta( $forum_id, 'uo_ld_custom_access_denied_message', true );
// If a custom message is set for this forum, use it.
if ( ! empty( $custom_message ) ) {
$message_html = '<div class="uo-ld-restricted-message">';
$message_html .= wp_kses_post( wpautop( $custom_message ) ); // Sanitize and format the message
$message_html .= '</div>';
return $message_html;
} else {
// Fallback to a generic message if no custom message is found.
$generic_message = __( 'Sorry, you do not have access to this forum. Please ensure you are enrolled in the correct course or group.', 'your-text-domain' );
$message_html = '<div class="uo-ld-restricted-message">';
$message_html .= wp_kses_post( wpautop( $generic_message ) );
$message_html .= '</div>';
return $message_html;
}
}
// If access is not restricted (i.e., $content is not false), return the original content.
return $content;
}
add_filter( 'uo_ld_forum_access_restricted_message', 'my_custom_ld_forum_access_restricted_message', 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/includes/learndash-bbpress-functions.php:96
function uo_ld_restrict_forum_access( $retval, $forum_id, $user_id ) {
// Always allow keymasters
$role = bbp_get_user_role( $user_id );
if ( bbp_is_user_keymaster() || $role == 'bbp_moderator' ) {
return true;
}
global $wpdb;
$group_forums = $wpdb->get_results( "Select meta_value as forum_id FROM $wpdb->postmeta WHERE meta_key LIKE 'uo_ld_associated_forum%' AND meta_value = $forum_id" );
if( empty($group_forums) ){
return $retval;
}
$user_forums = get_user_forum_group_access();
$has_access = true;
if ( ! isset( $user_forums[ $forum_id ] ) ) {
$has_access = false;
}
$message_without_access = get_post_meta( $forum_id, 'uo_ld_message_without_access', true );
$content = "<div id='bbpress-forums' class='ld-bbpress-forums'>
<p class='pre-message'>" . $message_without_access . "</p>
</div>";
if ( $has_access === false ) {
$retval = false;
echo apply_filters( 'uo_ld_forum_access_restricted_message', $content, $forum_id, $user_forums );
}
return $retval;
}