Graphics Cove

How to restrict link posting by user role in buddyPress

When running a BuddyPress-powered community, you might want to give different privileges to different user roles. For example, you might want to restrict certain users from posting links, while allowing more trusted members to share them freely. Here I will guide you through adding a small code snippet to your theme’s functions.php file to block users with specific roles from posting links in BuddyPress posts and comments.

The Problem

In a typical community, allowing all users to post links can lead to spam or malicious content. However, you might want to allow more trusted users, such as "premium" members, to share links. The goal is to block users with a “base” role from posting any URLs, while still allowing "premium" users to do so without restriction.

Restricting Buddypress Links

By adding a custom function to your functions.php file, you can create a simple but effective role-based content filter. Here's how it works:

  • Detect the User’s Role: When a user submits a post or comment, we’ll check their role (e.g., “base” or “premium”).
  • Scan for Links: If the user’s role is “base,” the content will be scanned for links.
  • Block Link Posts for Restricted Roles: If a user with the “base” role tries to post content with a link, the post will be blocked and an error message will be shown.

Add the following code to your theme’s functions.php file:

function restrict_links_for_role($content, $user_id) {
    // Get the user by ID
    $user = get_userdata($user_id);

    // Check if the user has the 'base' role
    if (in_array('base', $user->roles)) {
        // Check if content contains a link
        if (preg_match('/https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', $content)) {
            // Block the post/comment if it contains a link
            bp_core_add_message(__('You cannot post links with your current role.'), 'error');
            return false; // Prevent post/comment from being saved
        }
    }

    // Allow post/comment for other roles
    return $content;
}

// Hook into BuddyPress filters for posts and comments
add_filter('bp_activity_post_content', 'restrict_links_for_role', 10, 2);
add_filter('bp_activity_comment_content', 'restrict_links_for_role', 10, 2);

How the code works

  • The function restrict_links_for_role checks the user’s role when they submit content.
  • If the user has the “base” role and their content contains a URL, the post or comment is blocked, and they see a message saying, “You cannot post links with your current role.”
  • Users with other roles, such as "premium", are free to post links.

Customisation options

You can easily modify this code to fit your needs if you need different roles or a different error message.

  • Change role names: If you use different roles, simply replace 'base' or 'premium' with the appropriate role name.
  • Update the error message: Customize the message that users see when they attempt to post a link.

© 2010 - 2025 Graphicscove