Multisite – redirect from the main site - createIT
Get a free advice now!

    Pick the topic
    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.
    Tags

    Multisite – redirect from the main site

    Multisite – redirect from the main site

    CHALLENGE: WordPress multisite root url should redirect to a subsite

    SOLUTION: add a wp action and wp_safe_redirect()

    Multisite WordPress setup allows to create a multilanguage website. You can set up CMS to have a language version path as a subdirectory, ex: mysite.com/de/ , mysite.com/en/ . But what will happen when trying to access a root site url mysite.com? The main blog multisite homepage will be displayed. We can use the main site as a default landing page / language switcher, or even use it as a separate language version. Another solution will be to redirect every request from the main blog → to the homepage of the sub-site.

    Redirect from the main blog

    Our multisite uses sub-directories configuration. Every language sub-blog uses 2 letter code as a root path. We decided to redirect traffic from the main blog to the EN version (blog_id = 2). That way, if somebody types a url of a raw domain, he will be redirected to the sub-blog.

    // functions.php
    // redirection for: mysite.com → mysite.com/en/
    function redirect_from_main_blog()
    {
        $blog_id = get_current_blog_id();
        if (is_home() || is_404()):
            if ($blog_id == 1 && !is_admin() && !defined('WP_CLI')) {
                $url = get_home_url(2); //redirect to EN site from base url
                echo $url;
                wp_safe_redirect($url, 301);
                exit;
            }
        endif;
    }
    add_action('wp', 'redirect_from_main_blog');

    Request Params conflicts

    Sometimes the main site root url is used by external integrations, like the Pardot Form. On form submission, the site redirects to the main url with response params added to the request. If wp_safe_redirect() is executed, we will lose data from the response. The solution will be to check if param is set and add a code exception.

    function redirect_from_main_blog() {
        $blog_id = get_current_blog_id();
        if(isset($_POST['ct_success_url'])){
            // pardot success redirection fix (HTTP_REFERER)
            return false;
        }
        if(isset($_GET['errorMessage'])){
            // pardot errorMsg redirection fix
            return false;
        }
      (...)
    }

    That’s it for today’s tutorial. Make sure to follow us for more useful tips and guidelines.

    Comments
    2 response
      • Hello,
        simple example of redirecting user on login:
        // Add to your theme’s functions.php file or a site-specific plugin

        function redirect_user_on_login($user_login, $user) {
            // Assuming you have a way to get the user's subsite ID or slug; this is just an example
            $user_subsite_id = get_user_meta($user->ID, 'subsite_id', true);
            
            if (!empty($user_subsite_id)) {
                // Get the subsite URL. This is a simplified example; adjust based on your actual setup
                $subsite_url = get_blogaddress_by_id($user_subsite_id);
        
                // Redirect to the subsite homepage
                wp_safe_redirect($subsite_url);
                exit;
            }
            // If no subsite is found, no redirection is performed, and the execution continues normally
        }
        add_action('wp_login', 'redirect_user_on_login', 10, 2);

        If you would like to detect user subsites:

        function get_user_subsites($user_id) {
            $user_subsites = array();
        
            // Get all sites in the network
            $sites = get_sites();
            foreach ($sites as $site) {
                // Switch to each site context
                switch_to_blog($site->blog_id);
        
                // Check if the user is a member of the current site
                if (is_user_member_of_blog($user_id)) {
                    // Optionally, store more detailed information about the site
                    $user_subsites[] = array(
                        'blog_id' => $site->blog_id,
                        'siteurl' => get_site_url(),
                    );
                }
        
                // Restore original site context
                restore_current_blog();
            }
        
            return $user_subsites;
        }
        
        // Usage example (replace 'user_id' with the actual user ID you're checking)
        $user_id = 1; // Example user ID
        $user_subsites = get_user_subsites($user_id);

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    Automating stock and price updates in WooCommerce
    • Dev Tips and Tricks

    Automating stock and price updates in WooCommerce

    September 23, 2024 by createIT
    Integrating advanced subscription features in WooCommerce
    • Dev Tips and Tricks

    Integrating advanced subscription features in WooCommerce

    September 16, 2024 by createIT
    Fetching Time records from ActiveCollab API
    • Dev Tips and Tricks

    Fetching Time records from ActiveCollab API

    September 9, 2024 by createIT
    Docker Compose for PrestaShop
    • Dev Tips and Tricks

    Docker Compose for PrestaShop

    September 2, 2024 by createIT
    WordPress wizard in admin – step by step
    • Dev Tips and Tricks

    WordPress wizard in admin – step by step

    August 29, 2024 by createIT
    Order Status Sync between PrestaShop and External APIs
    • Dev Tips and Tricks

    Order Status Sync between PrestaShop and External APIs

    August 26, 2024 by createIT
    What is PHP used for in web development 
    • Dev Tips and Tricks

    What is PHP used for in web development 

    August 22, 2024 by createIT
    Automating WooCommerce product availability date
    • Dev Tips and Tricks

    Automating WooCommerce product availability date

    August 15, 2024 by createIT
    WP Quiz Adventure – FAQ
    • Dev Tips and Tricks

    WP Quiz Adventure – FAQ

    August 12, 2024 by createIT

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us