1

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();
  • When you say your sites users, do you mean authors? What goes in these feeds? You should never nest functions like that – Tom J Nowell Jan 12 '16 at 2:09
  • They are users with the role of "subscriber". Why not nesting functions? – fubeca Jan 12 '16 at 7:31
  • Also, to clarify, by "unique RSS feed" I mean a unique URL to access the RSS feed. One of the benefits of subscribing to the site is access to the RSS, and by having a unique one for each user, it's easy to turn the feed off when the user is no longer a subscriber. – fubeca Jan 12 '16 at 7:34
  • So your actual problem is that you have a single RSS feed that you need to authenticate users with so that unpaid users can't access it, with the ability to revoke access on a per user basis – Tom J Nowell Jan 12 '16 at 13:00
  • Also, nesting functions is terrible, it doesn't work the way you think it works, and if it did it wouldn't do what you want it to do. That they don't cause a syntax error is purely a side effect of the parser, and they serve no purpose other than to confuse your editor and tools, see stackoverflow.com/questions/415969/… – Tom J Nowell Jan 12 '16 at 13:02
1

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.

|improve this answer|||||

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.