On Multisite Privacy is there away to change the access denied page to have a custom landing page?

I would like to change the multisite privacy plugins access denied page to a custom page so that I can show a slide show and a message or login box is that possible?

  • Vinod Dalvi

    Hi @Melissa,

    I hope you are well today and thank you for your question.

    You can customize this page by adding following code in the functions.php file of your child theme and changing the code in the function custom_additional_privacy_deny_message to whatever you want to.

    remove_action('template_redirect', 'additional_privacy');
    remove_action('wp_loaded', 'additional_privacy');
    
    if ( spo_is_mobile_app() ) {
        add_action('template_redirect', 'custom_additional_privacy');
    } else {
        add_action('wp_loaded', 'custom_additional_privacy');
    }
    
    function custom_additional_privacy() {
        global $blog_id, $user_id, $current_blog, $wp, $dm_map;
    
        if ( $current_blog->public == '-4' && !is_user_logged_in() && isset( $_GET['privacy'] ) && '4' == $_GET['privacy'] ) {
            wp_enqueue_script( 'jquery' );
        }
    
        // Domain Mapping
        if( class_exists('domain_map') && isset($_GET['build']) && isset($_GET['uid']) && addslashes($_GET['build']) == date("Ymd", strtotime('-24 days') )) {
            return;
        }
    
        $register_url = apply_filters( 'wp_signup_location', site_url( 'wp-signup.php' ) );
        $register_part = str_replace(site_url('/'), PATH_CURRENT_SITE, $register_url);
        $privacy = $current_blog->public;
    
        $_request_path = parse_url("http://example.com/{$_SERVER['REQUEST_URI']}", PHP_URL_PATH);
    
        if ( is_numeric($privacy) && $privacy < 0 &&
            !stristr($_request_path, 'wp-activate') &&
            !stristr($_request_path, $register_part) &&
            !stristr($_request_path, 'wp-login') &&
            !stristr($_request_path, 'wp-admin') &&
            !stristr($_request_path, 'xmlrpc') &&
            !stristr($_request_path, 'wp-cron') ) {
    
            $_redirect_to = preg_replace( '/^'.str_replace('/', '\/', $current_blog->path).'/', '', trailingslashit($_SERVER['REQUEST_URI'])).'/';
            $_redirect_to = site_url($_redirect_to);
    
            if (isset($dm_map) && method_exists($dm_map, 'swap_mapped_url')) {
                $_redirect_to = $dm_map->swap_mapped_url($_redirect_to);
            }
    
            $_redirect_to = trailingslashit( $_redirect_to );
    
            switch( $privacy ) {
                case '-1': {
                    if ( ! is_user_logged_in() ) {
                        spo_redirect( wp_login_url( $_redirect_to )."&privacy=1" );
                        exit();
                    }
                    break;
                }
                case '-2': {
                    if ( ! is_user_logged_in() ) {
                        spo_redirect( wp_login_url( $_redirect_to )."&privacy=2" );
                        exit();
                    } else {
                        if ( ! current_user_can( 'read' ) ) {
                            custom_additional_privacy_deny_message( '2' );
                        }
                    }
                    break;
                }
                case '-3': {
                    if ( ! is_user_logged_in() ) {
                        spo_redirect( wp_login_url( $_redirect_to )."&privacy=4" );
                        exit();
                    } else {
                        if ( ! current_user_can( 'manage_options' ) ) {
                            custom_additional_privacy_deny_message( '3' );
                        }
                    }
                    break;
                }
                //single password
                case '-4': {
                    $spo_settings           = get_option( 'spo_settings' );
                    $value                  = wp_hash( get_current_blog_id() . $spo_settings['blog_pass'] . 'blogaccess yes' );
    
                    if ( !current_user_can( 'read' ) ) {
                        if ( !isset( $_COOKIE['spo_blog_access'] ) || $value != $_COOKIE['spo_blog_access'] ) {
                            spo_redirect( wp_login_url( $_redirect_to )."&privacy=4" );
                            exit();
                        }
                    }
                    break;
                }
            }
        }
        $file_value = hash_hmac('md5', "{$blog_id} file access yes", LOGGED_IN_SALT);
        setcookie( "spo_{$blog_id}_fa", $file_value, time() + 1800, $current_blog->path);
    }
    
    function custom_additional_privacy_deny_message( $privacy ) {
        do_action( 'additional_privacy_deny_message', $privacy );
    
    	header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
    	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    	header('Pragma: no-cache');
    	?>
    	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    	<html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<title><?php _e('Site Access Denied', 'sitewide-privacy-options'); ?></title>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    		<style media="screen" type="text/css">
    		html { background: #f1f1f1; }
    
    		body {
    			background: #fff;
    			color: #333;
    			font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, Verdana, sans-serif;
    			margin: 2em auto 0 auto;
    			width: 700px;
    			padding: 1em 2em;
    			-moz-border-radius: 12px;
    			-khtml-border-radius: 12px;
    			-webkit-border-radius: 12px;
    			border-radius: 12px;
    		}
    
    		a { color: #2583ad; text-decoration: none; }
    
    		a:hover { color: #d54e21; }
    
    		h1 {
    			font-size: 18px;
    			margin-bottom: 0;
    		}
    
    		h2 { font-size: 16px; }
    
    		p, li {
    			padding-bottom: 2px;
    			font-size: 13px;
    			line-height: 18px;
    		}
    		</style>
    	</head>
    	<body>
    	<h2><?php _e('Site Access Denied', 'sitewide-privacy-options'); ?></h2>
        <?php
        if ( $privacy == '2' ) {
            $msg = __( 'This site may only be viewed by users who are subscribed to this site.', 'sitewide-privacy-options' );
        } elseif ( $privacy == '3' ) {
            $msg = __( 'This site may only be viewed by administrators.', 'sitewide-privacy-options' );
        }
        ?>
        <p>
            <?php echo apply_filters( 'additional_privacy_deny_message', $msg )?>
        </p>
        </body>
        </html>
    	<?php
    	exit();
    }

    Kind Regards,
    Vinod Dalvi