settings = Plugin::get_settings( 'users' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !empty( $this->settings['redirect_after_login'] ) ) { add_filter( 'login_redirect', [ $this, 'handle_login_redirect' ], 999, 3 ); add_filter( 'woocommerce_login_redirect', [ $this, 'handle_login_redirect' ], 999, 2 ); } if( !empty( $this->settings['redirect_after_logout'] ) ) { add_filter( 'logout_redirect', [ $this, 'handle_logout_redirect' ], 999 ); } if( !empty( $this->settings['redirect_after_login'] ) || !empty( $this->settings['redirect_after_logout'] ) ) { add_filter( 'allowed_redirect_hosts', [ $this, 'allow_custom_hosts' ] ); } } /** * Adds settings fields for login/logout redirect configuration. * * @param array $fields The existing settings fields structured by categories. * * @return array The modified settings fields with added redirect configurations. */ public function adminease_settings_fields( array $fields ): array { $fields['users']['fields'][] = [ 'type' => 'switch', 'id' => 'redirect-after-login', 'name' => 'adminease[users][redirect_after_login]', 'value' => $this->settings['redirect_after_login'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Redirect after login', 'adminease' ), 'description' => __( 'Control where users are redirected after successfully logging in. By default, WordPress redirects users to the admin dashboard, but you can customize this to send them to any page on your site, such as their profile, a welcome page, or the homepage.', 'adminease' ), 'child_fields' => [ [ 'type' => 'text', 'id' => 'redirect-after-login-url', 'name' => 'adminease[users][redirect_after_login_url]', 'value' => $this->settings['redirect_after_login_url'] ?? '', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Login redirect URL', 'adminease' ), 'field_description' => __( 'Enter the full URL where users should be redirected after login. Leave empty to use WordPress default.', 'adminease' ), 'attributes' => [ 'placeholder' => home_url(), 'data-parent' => 'redirect-after-login', ], ], ], ]; $fields['users']['fields'][] = [ 'type' => 'switch', 'id' => 'redirect-after-logout', 'name' => 'adminease[users][redirect_after_logout]', 'value' => $this->settings['redirect_after_logout'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Redirect after logout', 'adminease' ), 'description' => __( 'Control where users are redirected after logging out. By default, WordPress redirects users to the login page, but you can customize this to send them to any page, such as the homepage or a custom goodbye page.', 'adminease' ), 'child_fields' => [ [ 'type' => 'text', 'id' => 'redirect-after-logout-url', 'name' => 'adminease[users][redirect_after_logout_url]', 'value' => $this->settings['redirect_after_logout_url'] ?? '', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Logout redirect URL', 'adminease' ), 'field_description' => __( 'Enter the full URL where users should be redirected after logout. Leave empty to use WordPress default.', 'adminease' ), 'attributes' => [ 'placeholder' => home_url(), 'data-parent' => 'redirect-after-logout', ], ], ], ]; return $fields; } /** * Lets wp_safe_redirect() (used by wp-login.php) reach the configured URLs, even on another host. * * @param array $hosts Allowed redirect hosts. * * @return array The hosts plus the hosts of the configured URLs. */ public function allow_custom_hosts( array $hosts ): array { foreach( [ 'redirect_after_login', 'redirect_after_logout' ] as $key ) { if( empty( $this->settings[ $key ] ) ) { continue; } $host = wp_parse_url( esc_url_raw( $this->settings[ $key . '_url' ] ?? '' ), PHP_URL_HOST ); if( $host ) { $hosts[] = $host; } } return $hosts; } /** * Filter the redirect URL after login. Runs after two-factor plugins have had their turn. * * @param string $redirect_to The redirect destination URL. * * @return string The configured URL, or the original one when none is set or for the session-expired popup. */ public function handle_login_redirect( $redirect_to ) { $custom_redirect = (string) ( $this->settings['redirect_after_login_url'] ?? '' ); $custom_redirect = esc_url_raw( $custom_redirect ); if( '' === $custom_redirect || isset( $_REQUEST['interim-login'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return $redirect_to; } return $custom_redirect; } /** * Filter the redirect URL after logout. The session ends normally and later logout hooks still run. * * @param string $redirect_to The redirect destination URL. * * @return string The configured URL, or the original one when none is set. */ public function handle_logout_redirect( $redirect_to ) { $custom_redirect = (string) ( $this->settings['redirect_after_logout_url'] ?? '' ); $custom_redirect = esc_url_raw( $custom_redirect ); if( '' === $custom_redirect ) { return $redirect_to; } return $custom_redirect; } }