[wp-hackers] Dynamic User Pages - How Does BuddyPress do it?

Nikola Nikolov nikolov.tmw at gmail.com
Wed Oct 9 19:18:28 UTC 2013


The problem with displaying the page even when the username does not exist
is due to the fact that you don't check if it exists or not in your
analytics_rewrite_catch() function. Change your function to this instead:

        function analytics_rewrite_catch() {
            global $wp_query;

            if ( array_key_exists( 'member', $wp_query->query_vars ) && (
$_member = get_user_by('login', $wp_query->query_vars['member']) ) ) {
                get_header();
                the_content();
                include_once( ABSPATH .
'/wp-content/plugins/My_Plugin/members.php');
                get_sidebar();
                get_footer();
                exit;
            }

Notice the "get_user_by('login', $wp_query->query_vars['member'])" part.
You would have to change that in case you are using the user id's
instead(right now it's assuming that you use the user's login name). This
way only if the user requested exists you will load your template(and in
that template you will have the "$_member" variable available which should
be an instance of WP_User, so that you can make the template dynamic).

I hope that helps.


On Wed, Oct 9, 2013 at 9:55 PM, BenderisGreat <greglancaster71 at gmail.com>wrote:

> Trying to dynamically create user pages (front end) without a plugin.
> Similar to how buddypress does it - no custom post types for users, just
> member profile urls.  I tried this:
>
>         add_filter( 'query_vars', 'analytics_rewrite_add_var' );
>
>         function analytics_rewrite_add_var( $vars )
>         {
>             $vars[] = 'member';
>             return $vars;
>         }
>
>         function add_analytic_rewrite_rule(){
>             add_rewrite_tag( '%member%', '([^&]+)' );
>             add_rewrite_rule(
>                 '^member/([^/]*)/?',
>                 'index.php?member=$matches[1]',
>                 'top'
>             );
>         }
>         add_action('init', 'add_analytic_rewrite_rule');
>         add_action( 'template_redirect', 'analytics_rewrite_catch' );
>
>
>         function analytics_rewrite_catch()
>         {
>             global $wp_query;
>
>             if ( array_key_exists( 'member', $wp_query->query_vars ) ) {
>                 get_header();
>                 the_content();
>                 include_once( ABSPATH .
> '/wp-content/plugins/My_Plugin/members.php');
>                 get_sidebar();
>                 get_footer();
>                 exit;
>
>             }
>         }
>
> But it generates the same page for every user, and even loads the page if a
> random username that isnt registered is entered.   How exactly does
> buddypress hook into users to make dynamic pages?  I am looking at the code
> now, but they have so many files.  Any help appreciated.
>
>
>
> --
> View this message in context:
> http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492.html
> Sent from the Wordpress Hackers mailing list archive at Nabble.com.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list