Search code examples
phpwordpresspluginsmultisite

How can I add a slug to the end of "siteurl" in this Wordpress redirect plugin?


On Wordpress multisite I am using the "redirect-multisite-user-to-their-own-site" plugin for users who login from the frontend to be redirected to their own specific blog. However, this plugin only redirects to the user's blog's homepage (e.g. mulstisite.com/users-site), rather than the dashboard (e.g. mulstisite.com/users-site/wp-admin). I want the user to be redirected to their dashboard.

In the plugin's code (see below, included in full), they have wp_redirect($user_target_site->siteurl);. I don't know PHP well yet and need it to somehow go to $user_target_site->siteurl + /wp-admin. How can I accomplish this? I am happy to edit the plugin and create my own plugin to prevent it being overridden by plugin updates.

    <?php
    /*
    Plugin Name: Redirect multisite user to their own site
    Plugin URI: https://wordpress.org/plugins/redirect-multisite-user-to-their-own-site/
    Description: If the current user in a multisite environment accesses a subsite to which access has not been granted, then redirect the user back to their own site. This plugin requires PHP 5.3 or newer because it uses PHP namespaces.
    Author: Mark Howells-Mead
    Version: 1.1.1
    Author URI: https://www.permanenttourist.ch/
    Text Domain: redirect-multisite-user-to-their-own-site
    */

    namespace MHM\MultisiteRedirectuser;

    class Plugin
    {
public function __construct()
{
    load_plugin_textdomain('redirect-multisite-user-to-their-own-site');
    if (is_multisite()) {
        add_action('parse_request', array($this, 'checkAccess'));
    }
}

public function checkAccess()
{
    global $current_user, $wpdb;
    $current_site_id = (int) $wpdb->blogid;
    $user_sites = get_blogs_of_user($current_user->ID);
    if (is_array($user_sites)) {
        switch (count($user_sites)) {
            case 0:
                if (is_user_logged_in()) {
                    do_action('redirect-multisite-user-to-their-own-site/no-sites', $current_user, $current_site_id);
                }
                break;
            case 1:
                $user_target_site = array_values($user_sites)[0];
                if ((int) $user_target_site->userblog_id !== $current_site_id) {
                    do_action('redirect-multisite-user-to-their-own-site/redirecting', $current_user, $current_site_id, $user_target_site->siteurl);
                    wp_redirect($user_target_site->siteurl);
                    exit;
                }
                break;
            default:
                if (!array_key_exists($current_site_id, $user_sites)) {
                    do_action('redirect-multisite-user-to-their-own-site/not-allowed', $current_user, $current_site_id, $user_sites);
                }
                break;
        }
    }
}
    }

    new Plugin();

Solution

  • As long as you're aware of the implications of changing a plugins code, try changing this line;

    wp_redirect($user_target_site->siteurl);
    

    To this:

    wp_redirect($user_target_site->siteurl . "/wp-admin/");