Creating an individual RSS feed for each site user

I’m trying to create a unique RSS feed for each of my site’s users. Here’s the code I’m using in my functions.php to attempt this. I don’t see any php errors when my pages load, but the feeds aren’t being created. Any ideas?

function init_subscribers_feeds() {

    $subscribers = get_users('role=subscriber');

    function create_feed($id) {
        add_feed($id, 'feed_subscribers_do');
    }
    function feed_subscribers_do() {
        get_template_part('feed', 'subscribers');
    }

    foreach ($subscribers as $subscriber) {
        $id = esc_html($subscriber->user_login);
        create_feed($id);
    }

}
init_subscribers_feeds();

Solutions Collecting From Web of "Creating an individual RSS feed for each site user"

I was able to figure this out. Here’s the code, explanation follows.

$subscribers = get_users('role=subscriber');
foreach ($subscribers as $subscriber) {
    $subscriber_id = esc_html($subscriber->user_login);
    add_action('init', function() use($subscriber_id) {
        add_feed($subscriber_id, function() {
            get_template_part('feed', 'subscribers');
        });
    });
    $wp_rewrite->flush_rules($hard);
}

First, I stopped nesting functions, per Tom J Newell’s critique in the comments under the original question.

Second, I found that after adding a new feed the WP rewrite rules needed to be updated. This can be done manually in the settings be re-saving the permalink settings, or using $wp_rewrite as shown above.

Third, to use the variable $subscriber_id I needed to include it in the anonymous function with “use()”.

Finally, rather than trying to separate things out into functions and calling those functions, I just wrapped all the old functions into anonymous functions.

It’s all working perfectly now.