bp_private_network_template_redirect()

Redirects users to login page if Private Network is enabled in BuddyBoss settings.

Description

Source

File: bp-core/bp-core-catchuri.php

function bp_private_network_template_redirect() {

	global $wp_query;

	if ( ! is_user_logged_in() ) {

		$enable_private_network = bp_get_option( 'bp-enable-private-network' );

		$page_ids            = bp_core_get_directory_page_ids();
		$terms               = false;
		$privacy             = false;
		$current_page_object = $wp_query->get_queried_object();
		$id                  = isset( $current_page_object->ID ) ? $current_page_object->ID : get_the_ID();
		$activate            = ( bp_is_activation_page() && ( '' !== bp_get_current_activation_key() || isset( $_GET['activated'] ) ) ) ? true : false;

		if ( '0' === $enable_private_network ) {

			if (apply_filters('bp_private_network_pre_check', false)) {
				return;
			}

			// Get excluded list from the settings
			$exclude = bp_enable_private_network_public_content();
			if ( '' !== $exclude ) {
				// Convert string to URL array
				$exclude_arr_url = preg_split("/\r\n|\n|\r/",$exclude);
				foreach ( $exclude_arr_url as $url ) {
					$extract_source = parse_url( $url );
					$path = isset( $extract_source['path'] ) ? $extract_source['path'] :'';
					$query = isset( $extract_source['query'] ) && '' !== $extract_source['query'] ? '?'.$extract_source['query'] :'';
					$url = $path.$query;
					// Check if strict match
					if ( $_SERVER['REQUEST_URI'] === $url ) {
						return;
					} elseif ( strpos( $_SERVER['REQUEST_URI'], $url) !== false ) {
						return;
					}
				}
			}

			if ( get_option( 'users_can_register' ) ) {

				if ( isset( $id ) ) {

					if ( ! bp_is_register_page() && ! $activate && $terms !== $id && $privacy !== $id ) {

						if ( class_exists( 'woocommerce' ) ) {

							$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

							if ( $actual_link !== wc_lostpassword_url() ) {

								if ( 'yes' !== get_option( 'woocommerce_enable_myaccount_registration') && $id !== intval( get_option( 'woocommerce_myaccount_page_id') ) ) {

									$redirect_url = is_ssl() ? 'https://' : 'http://';
									$redirect_url .= $_SERVER['HTTP_HOST'];
									$redirect_url .= $_SERVER['REQUEST_URI'];

									$defaults = array(
										'mode'     => 2,
										// 1 = $root, 2 = wp-login.php.
										'redirect' => $redirect_url,
										// the URL you get redirected to when a user successfully logs in.
										'root'     => bp_get_root_domain(),
										// the landing page you get redirected to when a user doesn't have access.
										'message'  => __( 'Please login to access this website.',
											'buddyboss' ),
									);

									bp_core_no_access( $defaults );
									exit();
								}

							}

						} else {

							$redirect_url = is_ssl() ? 'https://' : 'http://';
							$redirect_url .= $_SERVER['HTTP_HOST'];
							$redirect_url .= $_SERVER['REQUEST_URI'];

							$defaults = array(
								'mode'     => 2,
								// 1 = $root, 2 = wp-login.php.
								'redirect' => $redirect_url,
								// the URL you get redirected to when a user successfully logs in.
								'root'     => bp_get_root_domain(),
								// the landing page you get redirected to when a user doesn't have access.
								'message'  => __( 'You must log in to access the page you requested dfdfd.',
									'buddyboss' ),
							);

							bp_core_no_access( $defaults );
							exit();

						}
					// 404 redirect
					} elseif ( is_404() ) {
						$redirect_url = is_ssl() ? 'https://' : 'http://';
						$redirect_url .= $_SERVER['HTTP_HOST'];
						$redirect_url .= $_SERVER['REQUEST_URI'];

						$defaults = array(
							'mode'     => 2,
							// 1 = $root, 2 = wp-login.php.
							'redirect' => $redirect_url,
							// the URL you get redirected to when a user successfully logs in.
							'root'     => bp_get_root_domain(),
							// the landing page you get redirected to when a user doesn't have access.
							'message'  => __( 'You must log in to access the page you requested dfdfd.',
								'buddyboss' ),
						);

						bp_core_no_access( $defaults );
						exit();
					}

				} else {

					if ( class_exists( 'woocommerce' ) ) {

						$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

						if ( $actual_link !== wc_lostpassword_url() ) {

							if ( 'yes' !== get_option( 'woocommerce_enable_myaccount_registration') && $id !== intval( get_option( 'woocommerce_myaccount_page_id') ) ) {

								$redirect_url = is_ssl() ? 'https://' : 'http://';
								$redirect_url .= $_SERVER['HTTP_HOST'];
								$redirect_url .= $_SERVER['REQUEST_URI'];

								$defaults = array(
									'mode' => 2,
									// 1 = $root, 2 = wp-login.php.
									'redirect' => $redirect_url,
									// the URL you get redirected to when a user successfully logs in.
									'root' => bp_get_root_domain(),
									// the landing page you get redirected to when a user doesn't have access.
									'message' => __( 'You must log in to access the page you requested dfdfd.',
										'buddyboss' ),
								);

								bp_core_no_access( $defaults );
								exit();

							}

						}

					} else {

						$redirect_url = is_ssl() ? 'https://' : 'http://';
						$redirect_url .= $_SERVER['HTTP_HOST'];
						$redirect_url .= $_SERVER['REQUEST_URI'];

						$defaults = array(
							'mode'     => 2,
							// 1 = $root, 2 = wp-login.php.
							'redirect' => $redirect_url,
							// the URL you get redirected to when a user successfully logs in.
							'root'     => bp_get_root_domain(),
							// the landing page you get redirected to when a user doesn't have access.
							'message'  => __( 'You must log in to access the page you requested dfdfd.', 'buddyboss' ),
						);

						bp_core_no_access( $defaults );
						exit();

					}
				}

			} else {

				if ( class_exists( 'woocommerce' ) ) {

					$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

					if ( $actual_link !== wc_lostpassword_url() && ! bp_is_activation_page() ) {

						if ( 'yes' !== get_option( 'woocommerce_enable_myaccount_registration') && $id !== intval( get_option( 'woocommerce_myaccount_page_id') ) ) {

							$redirect_url = is_ssl() ? 'https://' : 'http://';
							$redirect_url .= $_SERVER['HTTP_HOST'];
							$redirect_url .= $_SERVER['REQUEST_URI'];

							$defaults = array(
								'mode' => 2,
								// 1 = $root, 2 = wp-login.php.
								'redirect' => $redirect_url,
								// the URL you get redirected to when a user successfully logs in.
								'root' => bp_get_root_domain(),
								// the landing page you get redirected to when a user doesn't have access.
								'message' => __( 'You must log in to access the page you requested dfdfd.',
									'buddyboss' ),
							);

							bp_core_no_access( $defaults );
							exit();

						}

					}

				} else {

					$redirect_url = is_ssl() ? 'https://' : 'http://';
					$redirect_url .= $_SERVER['HTTP_HOST'];
					$redirect_url .= $_SERVER['REQUEST_URI'];

					$defaults = array(
						'mode'     => 2,
						// 1 = $root, 2 = wp-login.php.
						'redirect' => $redirect_url,
						// the URL you get redirected to when a user successfully logs in.
						'root'     => bp_get_root_domain(),
						// the landing page you get redirected to when a user doesn't have access.
						'message'  => __( 'You must log in to access the page you requested dfdfd.', 'buddyboss' ),
					);

					bp_core_no_access( $defaults );
					exit();

				}

			}

		}

	}

}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.