0

I am trying out WordPress multisite and have set it up so that the main site on the network allows users to register.

From my (beginner level) understanding of how WordPress multisite works: a user registered on the main site is allowed to view all network content unless it is protected. There is only one user table in the database shared by the network. But users don't have a "role" in sub-sites unless it is explicitly given to them in admin area or by registration or plugins that synchronize users.

So then I set up another few sub-sites, and for them I made the users sync automatically via a plugin, because I want to have some content on them that is restricted to certain user roles. This works fine, no issues here.

But then I tried creating a private sub-site that is viewable only to subscribers. Setting that up was easy. However, what I want to do now is allow users to "manually" join this private sub-site if they want to - instead of me doing that in the admin area.

To clarify, I don't want to sync all users to this sub-site or other private ones. I want to make it entirely optional by putting a list of the network sub-sites on the main site, with "Join" links next to each one. So if logged in users click "Join" they become subscribers in that sub-site and can view its content (and if they click "Leave" they are no longer counted as subscribers and retain only their other roles).

The plugins and a lot of the sample code I found on the internet usually address automatically adding users to sub-sites, or things like allowing registration per individual sub-sites. They don't cover allowing users themselves to manually join/leave multisite blogs.

Only one, long abandoned plugin addressed exactly what I am looking for (called Join My Multisite), but I think the PHP code is outdated it does not function very smoothly. It is not available in the WP repository, but the author kept it on Github.

The WordPress codex mentions an add_user_to_blog function, but I need help understanding how to use it in this context. What I want is simple, but I don't know how to accomplish it.

Can I create a "Join" link that permits my main site users to Join the sub-sites they want to join, or a "Leave" link for them to remove their user role from only that sub-site? If yes, can you show me how, please?

Thank you.

2
  • How are you automatically syncing users? You mention a plugin, but to do what you want some modifications or filtering of that plugin will be necessary. Any answer you get here can't account for that plugin as that would be off-topic, and it wouldn't be standard WordPress – Tom J Nowell Oct 26 '20 at 12:15
  • I used the Multisite User Management plugin for those particular sub-sites, but this automatic syncing is only for them and I only mentioned it to explain my process and what I have been experimenting with. The sub-sites I made private and want to give an opt-in or opt-out option are not controlled by that plugin and they are the ones I want to control independently without a plugin, but by utilizing the add_user_to_blog function (if possible) to give users the choice. – jsmod Oct 26 '20 at 12:23
0

I did it, and it works just as I want it to, but I don't really understand how it worked 🙈 (and I'd like to learn the hows and whys behind it all).

Praying 🤲 plus an answer to another question on WPSE helped me a lot, I just modified it with the functions I wanted to implement.

I placed the following code in functions.php:

// Function to allow network users to manually subscribe to the sub-site

function subscribe_to_site()
{
// Check if the user is authenticated.
if (!is_user_logged_in()) {
return;
}

// Check if we have all necessary data.
if (
empty($_POST['subscribe_to_site_nonce']) || empty($_POST['subscribe']) ||
'Subscribe' !== $_POST['subscribe']
) {
return;
}

// Verify the nonce.
if (!wp_verify_nonce($_POST['subscribe_to_site_nonce'], 'subscribe-to-site')) {
return;
}

// Add user to the current blog
add_user_to_blog(get_current_blog_id(), get_current_user_id(), 'subscriber');

// Redirect back to the previous page.
wp_safe_redirect(wp_get_referer());
exit;
}
add_action('template_redirect', 'subscribe_to_site');

// Function to allow network users to unsubscribe from the sub-site

function unsubscribe_from_site()
{
// Check if the user is authenticated.
if (!is_user_logged_in()) {
return;
}

// Check if we have all necessary data.
if (
empty($_POST['unsubscribe_from_site_nonce']) || empty($_POST['unsubscribe']) ||
'Unsubscribe' !== $_POST['unsubscribe']
) {
return;
}

// Verify the nonce.
if (!wp_verify_nonce($_POST['unsubscribe_from_site_nonce'], 'unsubscribe-from-site')) {
return;
}

// Remove the user from the current blog
remove_user_from_blog(get_current_user_id());

// Redirect back to the previous page.
wp_safe_redirect(wp_get_referer());
exit;
}
add_action('template_redirect', 'unsubscribe_from_site');

And within my template file which I had created for the page/sub-site I was testing this with (on my localhost installation), I used the following:

<?php
global $current_user, $blog_id;

if (!is_user_logged_in())
    echo 'You are not logged in<br>';
elseif (is_user_logged_in() && (!current_user_can('read'))) {
    $current_user = wp_get_current_user();
    echo '<div style="direction:ltr;text-align:center">' . sprintf(__('Hi, %s!'), esc_html($current_user->display_name)) . '<br>You are logged in, but you are NOT subscribed to this blog.';
    echo '<form method="post" action="' . esc_url(home_url()) . '">
        <input name="subscribe" type="submit" id="subscribe-button" value="Subscribe" />' . wp_nonce_field('subscribe-to-site', 'subscribe_to_site_nonce') . '</form></div>';
} else {
    echo '<div style="direction:ltr;text-align:center">' . sprintf(__('Hi, %s!'), esc_html($current_user->display_name)) . '<br>You are subscribed to this blog.';
    echo '<form method="post" action="' . esc_url(home_url()) . '">
        <input name="unsubscribe" type="submit" id="unsubscribe-button" value="Unsubscribe" />' . wp_nonce_field('unsubscribe-from-site', 'unsubscribe_from_site_nonce') . '</form></div>';
}
?>

Both snippets were inspired by the answer I linked to, and I left the comments of the original answer in case anyone else needs the same function and finds them useful as I did.

At the moment, I am only using this on a local installation of WordPress multisite, but I want to use this on a live site later so I hope the code is safe to use.

Honestly I am still amazed that it works. Aah... feeling grateful at a time like this.

Anyway, if any of you have comments on how this answer can be improved, please share them as I am a beginner and would appreciate your assistance and advice.

Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.