Making WordPress SEO plugin compatible with BuddyPress Part 2
I hope that you might have read my previous post on making WordPress SEO plugin compatible with BuddyPress. If not, please do read the post here.
Based on the suggestions and feedback, I have updated the code to be more meaningful. In the end of this post, I am going to propose a new strategy too, and the future enhancement will depend on your suggestions.
My special thanks to Martin, Marc, Justina, Giorgos and all of you who provided your valuable feedback on my previous post.
So, what this code is all about:-
- It fixes canonical url issue on BuddyPress Components pages
- It provides better support for better group forum meta description when using bbpress plugin for group forums
- It fixes the title issue when flushing is enabled in WordPress SEO plugin
- And last, It also supports our very own BuddyBlog single post meta description in more meaningful way
Step1:- Fixing Titles for BuddyPress generated pages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * For WordPress Seo 3.0+ */ function buddydev_disable_wp_seo_title_filter() { if ( class_exists( 'WPSEO_Frontend' ) && is_buddypress() ) { $instance = WPSEO_Frontend::get_instance(); if ( has_filter( 'wp_title', array( $instance, 'title' ) ) ) { remove_filter( 'wp_title', array( $instance, 'title' ), 15, 3 ); } if ( has_filter( 'pre_get_document_title', array( $instance, 'title' ) ) ) { remove_filter( 'pre_get_document_title', array( $instance, 'title' ), 15 ); } } } add_action( 'bp_template_redirect', 'buddydev_disable_wp_seo_title_filter' ); |
Step2: Fix the canonical URL
The WordPress SEO plugin uses url of directory page for all the component pages. We need these to be more specific.
There is another issue, In WordPress, there is no definite way to know the absolute url of current page( and by page, I did not mean WordPress pages). add_query_arg ย function does provide relative url if we pass false parameters( May discuss some other day). So, I used a code snippet from WebCheetSheet and modified it for our purpose. Here is the code to get current page url.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * Get current page url without query string * based on http://webcheatsheet.com/PHP/get_current_page_url.php * modified to exclude the query string * * @return string url */ function bpdev_get_current_url() { $page_url = 'http'; if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) { $page_url .= 's'; } //append // $page_url .= '://'; if ( $_SERVER['SERVER_PORT'] != '80' ) { $page_url .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI']; } else { $page_url .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; } //find if the url has query variables $query_pos = strpos( $page_url, '?' ); //let us exclude that section from url if ( $query_pos ) { $page_url = substr( $page_url, 0, $query_pos ); } return $page_url; } |
And then, hooked our function to 'wpseo_canonical' filter as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | add_filter( 'wpseo_canonical', 'bpdev_fix_cannonical_url_for_bp' ); function bpdev_fix_cannonical_url_for_bp( $url ) { if ( bp_is_blog_page() || bp_is_directory() ) { return $url; } //in other case, return current url without the query string $url = bpdev_get_current_url();// //$url = add_query_arg(array( 'foo'=>false ) );//, get_home_url()); return $url; } |
That's it for fixing the canonical url.
Step3:- Better meta descriptions
I am just revisiting the code from my previous post. This code is much better and supports better meta for group forums and buddyblog.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | //generate meta description add_filter( 'wpseo_metadesc', 'bpdev_bp_items_metadesc' ); function bpdev_bp_items_metadesc( $desc ) { // if it is not buddypress page or buddypress directory pages let the plugin do its work if ( bp_is_blog_page() || bp_is_directory() ) { return $desc; } //we do not cover directory as directory meta can be customized from pages->Edit screen //now, let us check if we are on members page if ( bp_is_user() ) { //what should me the description, I am going to put it like Profile & Recent activities of [user_dsplay_name] on the site [sitename] //if you are creative, you can use some xprofile field and xprofile_get_field_data to make it better $desc = sprintf( '%s\'s Profile & Recent activities on %s ', bp_get_displayed_user_fullname(), get_bloginfo( 'name' ) ); //here we can do it based on each of the component or action using bp_si_current_component(;'component name') && bp_is_current_action('action_name') //here I am showing an example for BuddyBlog component //on buddyblog single post if ( function_exists( 'buddyblog_is_single_post' ) && buddyblog_is_single_post() ) { //get the post id // //buddyblog currently does not have api to get single post, I will add that in next version, for now, We will do the chore ourself $post_id = 0; //make sure //check the strategy if ( buddyblog_use_slug_in_permalink() ) { $slug = bp_action_variable( 0 ); $post_id = buddyblog_get_post_id_from_slug( $slug ); } else { $post_id = intval( bp_action_variable( 0 ) ); } if ( $post_id ) { $desc = WPSEO_Meta::get_value( 'metadesc', $post_id ); //let us update description } } //I have another strategy that I will propose at the end of this post to make it super easy } elseif ( bp_is_active( 'groups' ) && bp_is_group() ) {//for single group //let us use group description $post_id = false; //by default, use group description $group = groups_get_current_group(); $desc = $group->description; //are we looking for forum? if ( bp_is_current_action( 'forum' ) && function_exists( 'bbpress' ) ) { $forum_ids = bbp_get_group_forum_ids();//we will get an array of ids if ( $forum_ids ) { $post_id = array_pop( $forum_ids ); } //check if we are at single topic if ( bp_is_action_variable( 'topic', 0 ) && bp_action_variable( 1 ) ) { //we are on single topic, get topic id //get the topic as post $topics = get_posts( array( 'name' => bp_action_variable( 1 ), 'post_type' => bbp_get_topic_post_type(), 'per_page' => 1 ) ); //get the id if ( ! empty( $topics ) ) { $post_id = $topics[0]->ID; } } } //end of forum post finding //if the post id is given if ( $post_id ) { $desc = WPSEO_Meta::get_value( 'metadesc', $post_id ); //let us update description } //check if the forum is active and get the current post id /meta }//end of group section //do it for other components //ok, so you can go and do the same for all the other pages //finally return the description return $desc; } |
Future:-
Will you like to have an easy metabox in your post edit screen ( say members directory page ) ย that allows you to easily enter meta for single user pages ( like friends, groups, etc )? I am thinking of writing a base plugin which can be used by any plugin to add their own specific meta boxes and then, we can fetch these meta boxes in one go by passing the component, action, action variables(It will save all the complex code above) . Do you think that is a good idea? If yes, what are the things you would like to enter on that page?
Please do share your thoughts.
PS: Please do visit again for the next two free components: Signup Avatar plugin tomorrow and next day for BuddyDocs theme ย ๐
Way cool – just giving it a shake down now (while setting up some split tests hahaha)
Looking real good though, Brajesh
Thank you Martin. You were the one who motivated me to complete this part ๐
Pleasure mate.
I actually joined up here and left another comment which is waiting in the moderation Q too.
Also left you some feedback as well as another message.
Hi Martin,
Thank you for supporting us and helping with the feedback. I am sorry, I was not looking at comments for a couple weeks properly, so might have missed your comment. Going to look on it today ๐
Thats the real job!!. Thanks. Turn this into a plugin, name it " The ultimate fix for having BP work with WP-SEO" and a million downloads wont be a problem ๐
So it fixes canonicals where the issue I had with single images urls in BP gallery is no more.
Now BP gallery images have their own urls and can be shared among social networks and others.
Still I have a problem with group forum titles as they are not generated (global forums works fine) and member titles . Still are not generated. Am I doing something wrong?
I pasted the code in BP-custom.php and I use the recommended by WP-SEO title instead of
I hope code above is not stripped out.
Over the future , If the metabox solution eliminates the above custom code, then I'm in favour of it. It sounds nice if you can enter a custom meta description,title, etc, at a BP page and that can be used by other plugins.
On the other hand you might also wont to consider writing a BP SEO plugin as there is nothing decent out there.
Hi Giorgos,
Thank you for the generous comment ๐
Hoping for something better in future again. For now, as we have been discussing in forum, I hope that the thing about description is clear.
About the title, I missed that thing in my update. It does not take seo title, It takes title generated by BuddyPress. I will update this tutorial again this weekend with the seo title feature ๐
I am not an SEO expert, I just know few things about it, that's why I never dived into writing SEO plugin. I still hope that we can have some basic sort of plugin for sure ๐
Wonderful work Brajesh. Thank you. I look forward to your update this weekend too.
This fix may help some people. For my site, to get the BP auto-generated titles to show for BP Group Forums, I need to replace the first line of code above:
i.e.:
add_action( 'template_redirect', 'bpdev_wpseo_title_fix_for_bp' );
with:
add_action( 'wp_loaded', 'bpdev_wpseo_title_fix_for_bp' );
which is the previous first line of code from https://buddydev.com/buddypress/making-wordpress-seo-plugin-compatible-with-buddypress/
For some reason the code using the action hook 'template_redirect' doesn't work for my site. I'm putting the code in the functions.php of my child theme of BP Default. Any ideas why Brajesh?
Didn't work for me ๐
Neither option produces Titles for Group Forums
Hi Brajesh,
Is this still a live project for you? Is your update you mention above coming any time soon?
Hi thanks for the update,
Im having the Same problem, the group forums are still not getting custom titles,
ive added the code to functions in the child theme
this worked halfway for me
http://latinosparis.com/simplecode/el-problema-con-buddypress-y-wordpress-seo-by-yoast-solucionado/
Can't read the language myself but I hope it helps Brajesh put some added polish to his fine work, so far ๐
Thank you so much for this. Just to be clear, this, part 2 supplants what you wrote in part 1? so we should implement the code here and ignore part 1? just want to make sure that's clear.
Thanks again.
Hi Wilson,
It is a replacement of part one. Just ignore part 1 codes.
Thanks. Sorry for the n00b question but exactly where should one cut/paste the 4 code snippets? inside wp? buddypress? the seo plugin? can you please be specific?
oh and a small correction, it is "webcheatsheet" not "webcheetsheet"
๐
Thank You Wilson for asking.
Please put it in your theme's functions.php or bp-custom.php in your plugins directory.
Yeah, much the same as @giorgos the Group Forum topic titles aren't being used unfortunately. This is a huge start in fixing the most popular SEO plugin to work with the best Social WP plugin so kudos to @brajesh
HI Martin,
The problem was I did not use seo title for forum/forum posts. I just let BuddyPress generate them. I will have another update to handle that case soon ๐
Great job @sbrajesh!
Thank you Ben ๐
Thanks to this approach I can now remove the many if statements I had in my header.php file around the tag.
My post should read title tag but using angle brackets stripped out the word title ๐
Thank you Henry,
Glad that it helped ๐
Adding the step 1 solution leave my titles completely empty, Any idea of solving this?
Please try the fix that I mentioned in my 12 Sept comment above. It worked for me. I'd be interested to hear if it works for you too….and also to hear what Brajesh has to say about it.
Thanks Marc, I had already tried it. It wont make any difference. Title remained empty
Has there been any updates to this yet ?
the drawback is also that google webmaster tools starts showing me html issues with duplicated titles and descriptions.
Hi Brajesh
The meta titles generated by BP aren't unique enough. Can we filter the titles BP generates in the same way you've filtered the meta description? If so, do you know the filter name I can use?
Thanks again for this useful snippet!
After using your filter to stop WP SEO title generation on BP pages I've now come up with a way to filter the titles BP generates to make them more suited to my site Is this how you would do it? Hoping this code paste works….
// modify meta titles generated by BP pages
function modify_bp_meta_titles( $title, $title, $sep, $seplocation ) {
if ( bp_is_directory() ) {
$title = sprintf( __( '%s directory', 'buddypress' ), bp_get_name_from_root_slug() );
}
return $title;
}
add_filter( 'bp_modify_page_title', 'modify_bp_meta_titles', 10, 4 );
Humm, although my filter is working for the members directory, when I change it to target buddyblog page titles it doesn't work? any help would be appreciated ๐
e.g.
if ( bp_is_buddyblog_component() ) {
$title = bp_get_displayed_user_fullname() . ' – ' . get_bloginfo( 'name' );
}
Apologies for all the posts on this thread, done 4 already today. Just wanted to let you know I solved the issue. I just went into the BuddyBlog code and see you already have a function to filter the BP title 'buddyblog_modify_page_title'
I just had to give my function a priority larger than 20 for it to work how I needed it to ๐
Hi Brajesh,
Just so we all know what to expect, please could you tell us if this is still a live project for you.
The reason I ask, is that you mentioned above that you were planning to update this code in the weekend in the middle of September. So I, and I guess many others too, would be pleased to hear your plans please. Many thanks.
Has anyone found a solution because marks not had a reply so it looks like Brajesh has stopped working on this,
Any news from anyone on this? It would be very sad if progress has stopped on this. Please post if you have any suggestions on what we do now.
Hi Brajesh,
Is this code still interesting to use since all the new versions of Yoast Seo (for example) ? Does buddypress still lack a lot in SEO ?
THx for your time and aweosme site !
Hi Evrard,
It is not tested with the Yoast SEO 3.0+
Will do the testing and let you know in a day or two.
Thank you
Brajesh
Can't thank you enough for your time and support ! Thx a lot ! looking forward to read your answer on this point ๐
Hi @Brajesh, any news on this point ?
Thx a lot.
Hi,
Thank you for the follow up. The title fix will not work with Yoast Seo 3.0+. There is an easy solution though. I will post tonight or early tomorrow as a blog post and will link here.
Thank you
Brajesh
Hey @Brajesh Have you found a solution to make it compatible with the current version of Yoast??
Thanks
Hi Dale,
Apologies for the delayed reply. It was a simple fix but I forgot to update the post. Have updated it now.
Thank you
Brajesh
Hi @Brajesh, any news on this point ?
Am also waiting for this fix. Thanks.
Apologies for the delayed reply, Have updated the post to fix the title issue.
Hey Brajesh,
Were you ever able to figure out that easy solution? I notice when I go to my profile on this website the title is correct. I am trying to make Yoast SEO (3.4.2) work with my current BuddyPress (2.6.2) setup.
I am also willing to pay for it as a plugin if you decide to put it in your repository.
Thanks
Hi Core,
I have updated the post. Please see the step1 for the fix on title.
Thank you
Brajesh
Thank you! This helped so much.
You are most welcome ๐
Quick question… is there a way to exclude one page by it's id from using this? I would like my "Groups" page to not have it's title and meta overwritten.
Hi Brajesh,
this seems to be the only useful working solution, great work, thanks a lot, even the meta hack makes sense.
i just noticed one thing when i checked the meta data, if you use Yoastยดs Social Meta Cards, it seems that the title meta is not modified, maybe you can check this in your next version?
tnx, Eric
Hi Eric,
Thank you for the comment.
I am glad it helped.
I was not using the social meta cards. Will certainly look into it for the future update.
Cant get this to work, can I not just pay you to fix my site, BuddyPress is driving me nuts!!
Sorry about the delayed reply. I hope you have got it resolved by now. If not, Please post in our forum and we will assist.
Hi!
Thanks for good solution.
You have one error.
You should check that topic, forum or post description is not empty.
Example:
$tmp_forum_desc = WPSEO_Meta::get_value( 'metadesc', $post_id );
if ( $tmp_forum_desc ) {
$desc = $tmp_forum_desc;
}