|
/ Documentation /Custom Code Snippets/ How to Restrict Any Space or Post from SureDash?

How to Restrict Any Space or Post from SureDash?

SureDash is built with flexibility in mind, and developers can easily control access to Spaces or individual Posts using custom code.

This guide will help third-party developers or advanced users add restrictions to any Space or Post within SureDash, based on their own rules (like user roles, meta fields, or any custom logic).

Use Case

You might want to restrict a:

  • Specific Space (e.g. only visible to logged-in users)
  • Individual Post inside a Space (e.g. available to users with a particular role or membership)

This can be done by using a custom filter and redirecting the user when your condition is not met.

Code Example

Below is a sample code snippet that you can copy into your plugin or your child theme’s functions.php file. This code restricts access to a specific Space and Post, and redirects users to a custom URL if they do not meet the condition.

Gist Link: SureDash Restrict Access Snippet

add_action( 'template_redirect', function () {
    // Get current post and check if it's a SureDash Space or Post
    if ( is_singular( 'suredash_space' ) || is_singular( 'suredash_post' ) ) {
        $post_id = get_the_ID();

        // Your custom logic goes here
        // Example: Check if user is logged in
        if ( ! is_user_logged_in() ) {
            // Optional: Add exceptions for specific Space/Post IDs
            $excluded_ids = [ 123, 456 ]; // Replace with your own IDs
            if ( in_array( $post_id, $excluded_ids ) ) {
                return;
            }

            // Redirect to a custom page
            wp_redirect( home_url( '/restricted-access/' ) );
            exit;
        }
    }
} );

What This Code Does

  • It checks if the current page is a SureDash Space or SureDash Post.
  • Then, it runs a check using your custom logic (e.g. whether the user is logged in).
  • If the condition is not met, it redirects the user to a specific page like /restricted-access/.

Customize It Further

You can replace the logic inside the code to suit your needs:

Check for specific user roles:

if ( ! current_user_can( 'subscriber' ) ) {

Use SureMembers Access Groups (if using SureMembers):

if ( ! function_exists( 'suremembers_user_has_access_group' ) || ! suremembers_user_has_access_group( get_current_user_id(), 101 ) ) {

Important Notes

  • Make sure the page you’re redirecting to (/restricted-access/) exists and gives users a proper message.
  • Use this only if you’re comfortable editing PHP code or building custom plugins.
  • Test this on a staging site first to avoid accidental lockouts.

If you’re building an integration or plugin that extends SureDash, this method lets you fully control who sees what content, with full flexibility on how you define your conditions.

Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

On this page
Scroll to Top