Discussion:
Dynamic User Pages - How Does BuddyPress do it?
BenderisGreat
2013-10-09 18:55:21 UTC
Permalink
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.
Nikola Nikolov
2013-10-09 19:18:28 UTC
Permalink
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.
Post by BenderisGreat
Trying to dynamically create user pages (front end) without a plugin.
Similar to how buddypress does it - no custom post types for users, just
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.
--
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
http://lists.automattic.com/mailman/listinfo/wp-hackers
BenderisGreat
2013-10-09 19:52:19 UTC
Permalink
Excellent! That fixed it. Now for each user I have a dynamic page load,
but how can I populate that page with each users stored data? I was
previously using custom post types, so the data was easy to grab, just get
the author id of the post and populate the pate with that usermeta.

Since it is now dynamic, I am unsure how I would go about populating the
page. I think Iwould need to create a custom query variable when the
username is grabbed, and then I can use the get_query_var.

Is that right?



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492p42495.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Nikola Nikolov
2013-10-09 20:04:55 UTC
Permalink
Well depends on what you want to list about your users.

Is the user data stored as user meta? If so, you can simply do

echo get_user_meta( $_member->ID, 'meta_key1', true ):

You can also try accessing that directly from the $_member object:

echo $_member->meta_key1;

As far as I remember WP_User was using the magical __get() method, which
dynamically loads any un-existing properties of the object.
Post by BenderisGreat
Excellent! That fixed it. Now for each user I have a dynamic page load,
but how can I populate that page with each users stored data? I was
previously using custom post types, so the data was easy to grab, just get
the author id of the post and populate the pate with that usermeta.
Since it is now dynamic, I am unsure how I would go about populating the
page. I think Iwould need to create a custom query variable when the
username is grabbed, and then I can use the get_query_var.
Is that right?
--
http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492p42495.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
BenderisGreat
2013-10-09 20:26:21 UTC
Permalink
Right again, I dont know how I missed that. Now I am faced with one last
problem - using a template was a bad idea because does not load the header
and footer properly on all themes. Is there a way to insert this into the
content of the page rather than load the template?

I previously used


function add_profile( $content ) {
if( is_page('')) include_once('member.php');
else { echo get_the_content(); }
}

add_filter('the_content','add_profile');

but dont think I can really implement that into the current code. But this
worked great because rather than load a whole custom template, it injects
whatever is inside the member.php inside the_content(). Anyway to repeat
that with this dynamic profile?





--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492p42497.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Nikola Nikolov
2013-10-09 20:33:09 UTC
Permalink
What I would probably do is to add a drop-down in your plugin's interface,
asking the user to pick a page that would be used as a profile page.

You can then modify the query in the 'parse_request' action(you will be
passed the $wp object and not $wp_query - keep that in mind) and tell
WordPress to load the page with ID of whatever page the admin has chosen to
be profile page. This way WordPress will still load a normal page
template(and not the 404 template) and you will be able to use the
'the_content' filter to put whatever you want there :)

It's a bit late here, but if you can't figure it out or there's no one else
to guide you along, I'll be able to provide you with a sample code tomorrow
at some point.

Nikola
Post by BenderisGreat
Right again, I dont know how I missed that. Now I am faced with one last
problem - using a template was a bad idea because does not load the header
and footer properly on all themes. Is there a way to insert this into the
content of the page rather than load the template?
I previously used
function add_profile( $content ) {
if( is_page('')) include_once('member.php');
else { echo get_the_content(); }
}
add_filter('the_content','add_profile');
but dont think I can really implement that into the current code. But this
worked great because rather than load a whole custom template, it injects
whatever is inside the member.php inside the_content(). Anyway to repeat
that with this dynamic profile?
--
http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492p42497.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
BenderisGreat
2013-10-11 20:15:59 UTC
Permalink
Thanks again for your help Nikola - I ended up just pasting a whole template
inside the function before and after the include statement. It works for
now, but wont work with any other theme but mine. I was considering
something like echo apply_filters( 'my_filter_name, $content );

Then apply it using add_filter( 'my_filter_name',
'your_function_for_adjusting_the_content' ); Would something like that
work?



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492p42522.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Nikola Nikolov
2013-10-12 09:16:39 UTC
Permalink
You can find the sample code that I made here -
https://gist.github.com/nikolov-tmw/8d803ea9b4adfbd7e221

What this does is that it checks for the presence of the "member" query
variable. If it's set and is referencing a valid user, then we overload the
query and tell WordPress to load a specific page(the one taken from the
get_my_page_id() function) and we also hook to a couple of filters in order
to display the correct information.
If on the other hand it's not a valid username(at the moment - you can
change this to user ID for instance) then a 404 page is displayed instead.

There are a couple of things that you can do to improve these functions(and
you probably should):
- Make the page ID dynamic. Add an option in your plugin's settings where
the user can select which page should be the profile page(right now it's
hard-coded to 2 which is the default "Sample Page" ID).
- Allow the user to override your plugin's template. You can take a look
at how WooCommerce does it(
http://docs.woothemes.com/wc-apidocs/source-function-woocommerce_get_template.html#596-619
) and
borrow and modify the code to fit your needs. This will basically allow the
user to create a file(for instance) "members-page.php" in their theme
directory and change the template to their liking, instead of having to
edit your plugin's file which would get over-written during an update.

If there's anything else to help with - just post it here.

Nikola


On Fri, Oct 11, 2013 at 11:15 PM, BenderisGreat
Post by BenderisGreat
Thanks again for your help Nikola - I ended up just pasting a whole template
inside the function before and after the include statement. It works for
now, but wont work with any other theme but mine. I was considering
something like echo apply_filters( 'my_filter_name, $content );
Then apply it using add_filter( 'my_filter_name',
'your_function_for_adjusting_the_content' ); Would something like that
work?
--
http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492p42522.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
BenderisGreat
2013-10-16 14:18:45 UTC
Permalink
Thank you man, that was a really clean addition. I like how you did the
search and replace for shorter names ('%FNAME%') and the content filter to
insert the code into a page template is great. :) With your additions I
was even able to style some css into the header block next to the h1 and I
duplicated the my_title_filter and renamed it my_header_filter, changed a
couple simple things and now it allows for custom title tags and h1 post
title.

Thanks a million Nikola!




--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492p42550.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Nikola Nikolov
2013-10-16 15:17:11 UTC
Permalink
I'm happy that it helped you :)
Post by BenderisGreat
Thank you man, that was a really clean addition. I like how you did the
search and replace for shorter names ('%FNAME%') and the content filter to
insert the code into a page template is great. :) With your additions I
was even able to style some css into the header block next to the h1 and I
duplicated the my_title_filter and renamed it my_header_filter, changed a
couple simple things and now it allows for custom title tags and h1 post
title.
Thanks a million Nikola!
--
http://wordpress-hackers.1065353.n5.nabble.com/Dynamic-User-Pages-How-Does-BuddyPress-do-it-tp42492p42550.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Continue reading on narkive:
Loading...