Populating the BuddyPress user Profile page’s title, meta description, and meta keywords with some of the user-generated content, delivered via xprofile fields, seems so much better than the default options, or even what’s available in seopress, the only current BuddyPress SEO plugin.
While we’re at it, setting the on-page SEO data for Activities based on the content is a great move too.
You’ll have to modify the fields based on the data you have available, and what fields feed into which page data, but this code, placed in your theme’s functions.php file, is a good start.
Want to make a plugin based on this code for the lay-men? Feel free.
Note:Â extractCommonWords function based on the function here.
function bp_xprofile_title($title=null, $without_site_name=null){ global $bp, $current_blog; if(defined('BP_ENABLE_MULTIBLOG')){ $blog_title=get_blog_option($current_blog->blog_id,'blogname'); }else { $blog_title=get_blog_option(BP_ROOT_BLOG,'blogname'); } if(bp_is_user_profile()){ $title_array = array(); if($data = xprofile_get_field_data( 'Company Name', $bp->displayed_user->id)) $title_array[] = "$data ({$bp->displayed_user->fullname})"; if($data = xprofile_get_field_data( 'Business Overview', $bp->displayed_user->id)) $title_array[] = $data; $title_array[] = $blog_title; $title = mb_strimwidth(strip_tags( esc_attr( implode( ' | ', $title_array))), 0, 70, '...'); } if (bp_is_activity_component() && is_numeric( $bp->current_action )) { $activity = bp_activity_get_specific( array( 'activity_ids' => $bp->current_action ) ); if ( $activity = $activity['activities'][0]) if(!empty($activity->content)) $title = mb_strimwidth(preg_replace("/[^A-Za-z0-9\s\s+\-]/", "", strip_tags( $activity->content)), 0, 70-3-3-strlen($blog_title), '...') . " | $blog_title"; } return $title; } add_filter('bp_page_title', 'bp_xprofile_title',0); function extractCommonWords($string){ $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www'); $string = strip_tags($string); $string = trim($string); // trim the string $string = preg_replace('/[^a-zA-Z0-9\s\s+\-]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too… $string = strtolower($string); // make it lowercase preg_match_all('/\b.*?\b/i', $string, $matchWords); $matchWords = $matchWords[0]; foreach ( $matchWords as $key=>$item ) { if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) { unset($matchWords[$key]); } } $wordCountArr = array(); if ( is_array($matchWords) ) { foreach ( $matchWords as $key => $val ) { $val = strtolower($val); if ( isset($wordCountArr[$val]) ) { $wordCountArr[$val]++; } else { $wordCountArr[$val] = 1; } } } arsort($wordCountArr); $wordCountArr = array_slice($wordCountArr, 0, 10); return array_keys($wordCountArr); } function bp_xprofile_meta() { global $bp; if(bp_is_user_profile()){ $array = array(); if($data = xprofile_get_field_data( 'Business Category', $bp->displayed_user->id)) $array[] = htmlspecialchars_decode($data); if($data = xprofile_get_field_data( 'Years of Experience', $bp->displayed_user->id)) $array[] = "$data years experience"; if($data = xprofile_get_field_data( 'Location', $bp->displayed_user->id)) $array[] = $data; if($data = xprofile_get_field_data( 'Business Overview', $bp->displayed_user->id)) $array[] = $data; $desc = mb_strimwidth(preg_replace("/[^A-Za-z0-9\s\s+\-&]/", "", strip_tags( implode( ' - ', $array))), 0, 150, '...'); echo '<meta name="description" content="'.$desc.'" />'; if($data = xprofile_get_field_data( 'Profile Details', $bp->displayed_user->id)) $array[] = $data; if($data = xprofile_get_field_data( 'Company Name', $bp->displayed_user->id)) $array[] = $data; $array[] = $bp->displayed_user->fullname; $key_text = preg_replace("/[^A-Za-z0-9\s\s+\-]/", "", strip_tags( implode( ' ', $array))); $keys = extractCommonWords($key_text); echo '<meta name="keywords" content="'.implode(',', $keys).'" />'; } if (bp_is_activity_component() && is_numeric( $bp->current_action )) { $activity = bp_activity_get_specific( array( 'activity_ids' => $bp->current_action ) ); if ( $activity = $activity['activities'][0]) if(!empty($activity->content)){ $content = preg_replace("/[^A-Za-z0-9\s\s+\-]/", "", strip_tags( $activity->content)); echo '<meta name="description" content="'. mb_strimwidth($content, 0, 150, '...').'" />'; $keys = extractCommonWords($content); echo '<meta name="keywords" content="'.implode(',', $keys).'" />'; } } } add_action('wp_head', 'bp_xprofile_meta',1);