Skip to:

Router for root components

  • Hi all,

    I recently needed to code some root components for a project of mine, and after figuring out how and where to hook in order to get them registered I came up with the idea of creating a router for them. For those who’ve gone digging you know that Buddypress does it’s own URL handling which isn’t dependent on what’s in the database. However the separate screen functions (BPs controllers) decide what URL to act on. This means that every screen function has to look at the URL and decide if it’s a go or a no go.

    I have written a router that handles this for you. As long as you follow the conventions (which I’d like to discuss) it will handle the rest for you. This means that all you have to do is define the screen functions and the router will run them for you given the correct URL. If it doesn’t find a screen function it will go looking for a template with that name in your themes folder (component subdir of course). If you want to do your own thing then that’s not a problem either, just hook your screen function in the same way as Buddypress own root components do it and the routers standard behaviour will be overridden.

    I would of course love to share but I’m unsure how big the want/need for this kind of plugin is. So is there anyone else out there writing root components who feels like this might be a good idea?

Viewing 7 replies - 1 through 7 (of 7 total)

  • r-a-y


    I’d be interested to see what you’ve written.



    @smurkas what @r-a-y said, thank you.

    Ok guys I’ve put it up on SVN on Beanstalk. There’s the router here: and there’s a minimal root component to get you going here:

    The router assumes that the root component is registered via the action bp_setup_globals since it uses the global data to figure out what to route to. The root component works out of the box even though it doesn’t actually do anything, it just sets up all the globals and slug. In this case I’ve put in a screen function for the index action. If you don’t actually need to do any business logic I would recommend removing it and adding a folder in your theme corresponding to the component id (in this case bp_min_root) and putting an index.php file in there instead.

    Let me know if it works for you and what you think. I’d love to discuss what conventions to use for this kind of thing.

    Ok I’ve continued to work on this but I’ve run into abit of a snag. It seems that the standard status code from WP for a screen function is 404 unless a template is loaded. This is fine in most cases but in some cases you might not want to return anything and when it comes to AJAX you might want to return a JSON response. Setting the header yourself and putting die() at the end works but that’s pretty ugly and I’d like to “follow the rules” so to speak.

    Does anyone know the best practice or convention for this? Very thankful for any ideas/help. I’ve updated the plugin in the repository to the latest version.



    @smurkas I’m not sure what exactly I’m doing wrong, but I’m not able to get this to work. Currently, I’m building a multisite recipe component and I need a root page for global searching (example: I’m assuming your router should help with this but I’m not able to get it to work. Any ideas?



    @smurkas Nevermind I have figured it out. I wasn’t naming things correctly.




    I would absolutely love to hear how you solved this problem and built the multisite recipe component with global searching. I’m trying to do something similar where each site has a custom post-type ‘testimonials’. I want to pull them all into a ‘root page’ as well where they can also be searched.

    I was going to use the sitewide tags plugin, but it does not seem to work at all with custom post-types yet. Not to mention it would be overkill for my needs.

    Would you care to share a few code snippets?

    thanks for your time

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Router for root components’ is closed to new replies.
Skip to toolbar