Allowing users to manually add or remove themselves to/from WordPress multisite blogs

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.