自定义标签、消息和 URL

自定义标签、消息和 URL

Codex Home → Getting Started → Customizing → Customizing Labels, Messages, and URLs
Customizing Labels, Messages, and URLs

Do you want to change various labels and messages in BuddyPress, but don』t want to hack the core files to do so? This page explains how to use a language translation file to customize BuddyPress labels, messages, and URL slugs.

Note: This is not intended for full language translations. If you intend BuddyPress to be used in a language other than English, see the Language Translations page.

Why use a language file?

The most compelling reason to use this method of changing labels and messages is to avoid hand-changing labels and messages in plugin and theme files. Even though theme customizations are recommended and encouraged, it is nice to have your customizations in one place. Overall, creating a custom language file is a better long-term solution than hand-changing plugin or theme files since you can make changes in one place for all labels and messages used in BuddyPress.

Labels and Messages

Here are the steps to create your own custom messages and labels without doing a full language translation.

This example uses en_US as the sample language definition and thus buddypress-en_US.mo is the name of the language file you will create. You』ll need to replace 『en_US』 with the locale of your WordPress site.

1. Create a customized PO file

Open the POT file in wp-content/plugins/buddypress/buddypress.pot, make a copy of the file and rename it to buddypress-en_US.po.

Then make the changes you want in a text editor or by using a specialized language localization editor like PoEdit (this is recommended if you are not comfortable using a text editor to make changes to the .po file).

Save your .PO file and generate a .MO file. The .MO file should be similarly named – buddypress-en_US.mo.

2. Uploading the PO and MO files

Now that you have generated your .MO file, move both the PO and MO files to /wp-content/languages/plugins/ on your server.

If the /wp-content/languages/plugins/ folder does not exist, create it!

3. Load the MO language file

WordPress 4.6 changed how language files are enqueued, preferring the GlotPress-provided 「canonical」 language file over a custom language file. The simplest way to make sure your new .mo file will be used is via a plugin available on the WordPress plugin repository: WPT Custom Mo File. Once you』ve activated the plugin, visit WP Admin > Tools > WPT Custom Mo File to upload your custom .mo file.

4. Test and verify your changes by loading BuddyPress.

Translating with PoEdit

PoEdit is a lightweight application for editing and creating .po and .mo files.

If you are not comfortable working at the command line then it is highly recommended to use this application.You can download PoEdit from http://www.poedit.net/download.php.

1) Download, install and start PoEdit

2) Select the BuddyPress translation file

On the main menu select 「File > New catalog from POT file」, navigate to the BP languages folder (/wp-content/plugins/buddypress/bp-languages/)and select the 『buddypress.pot』 file.

This will open a settings dialog and you will be asked to fill in some details such as project name which we』ll skip.

Click on 『OK』 and you』ll be asked to save your language file.

Save your language file as 「buddypress-en_US.po」. If you』re using a different language on WordPress, change 「en_US」 to the locale of your WordPress install.

3) Changing strings and saving the changes

Now, you will see a list of all the BuddyPress language strings you can modify.

Click on any string you wish to change and make the adjustment in the bottom half of the screen.

Go ahead and repeat this process for the other labels you want to change and when you』re done, hit 「Save」.

4) Uploading the PO and MO files

Follow steps #2 and #3 above.

Updating your custom language file

When a new version of BuddyPress is released, a new BuddyPress POT file is generated.

To remain up-to-date, you will need to merge this new file with your custom .po catalog.

You can do this by following these instructions:

1) Start PoEdit and go to 「File > Open」. Select your custom .po catalog file you created.

2) Now, go to 「Catalog > Update from POT file…」, and navigate to the /plugins/buddypress/buddypress.pot file (For older versions of BuddyPress, this file is located at /plugins/buddypress/bp-languages/buddypress.pot).

3) Opening the file will show an 「Update Summary」 window, which shows you the new strings added and the obsolete strings that will be removed. Click on 「OK」 and these changes will be merged into your .po file.

4) Then, simply save your catalog file once more to generate a new .mo file.

Slugs

For example, if you are running a sports website with BuddyPress, you could rename the URL slug example.com/members to example.com/players by changing the url of the corresponding component page in the WordPress admin -> pages.

To change slugs on the sub nav on a member page add these defines to a file bp-custom.php and place this file in /plugins/bp-custom.php

define( 'BP_SETTINGS_SLUG', 'settings' );
define( 'BP_XPROFILE_SLUG', 'profile' );

Loco Translate – Plugin solution

Loco Translate allows site admins to modify WordPress translations files of any theme and any plugin within the WordPress Dashboard. It also provides localization tools for developers, such as extracting strings and generating templates.

Go to Loco Translate plugin page

Notes

This is not intended for full language translations. If you intend BuddyPress to be used in a language other than English, see the Language Translations page.

This solution is arguably better than using the define ( 'WPLANG', ''); declaration in your wp-config.php file since we are really just trying to customize BuddyPress and not WordPress MU as a whole.

This page was inspired by a forum topic on BuddyPress.org called I want to speak Spotlish. View that thread for discussion and other suggestions.

群组成员循环

群组成员循环

Codex Home → Developer Resources → Loops Reference → Group Members Loop
Group Members Loop

The group members loop can be used to display members that have joined a group. It can be used nested inside the bp_has_groups() loop, or on its own with a group_id parameter.
Standard Loop

This group has no members.

Accepted Parameters
The bp_group_has_members() function will accept a number of parameters that will manipulate the data being returned.

group_id required
The ID of the group to fetch members for. Required when either: outside of the normal BuddyPress groups URL (/group/i-love-bp/members/) or not nested within a bp_has_groups() loop.

Default value: false

per_page optional
The number of members to display on a page before they are paginated to the next page.

Default value: 10

max optional
The total number of members to return.

Default value: false (no limit)

exclude_admins_mods optional
If you set this to true, only users with normal access (not a group administrator or moderator) will be returned in the members list.

Default value: true

exclude_banned optional
If you set this to true, any users that are banned from the group will not be returned in the members list.

Default value: true

Advanced Usage
Fetch all the members from the group with ID 15 and show 10 per page.

修改工具栏

修改工具栏

Codex Home → Getting Started → User Submitted Guides → Deprecated Guides → Modifying the Toolbar
Modifying the Toolbar

Note: Modifying the Admin Bar requires a basic understanding of WordPress Hooks,Actions and Filters. If you aren』t familiar with these items, you can find more information on the WordPress Plugin API page.
The admin bar is a collection of individual functions, each of which implements a specific top level menu item that is displayed. If you look at the code in: /bp-core/bp-core-adminbar.php you』ll see these funcitons such as bp_adminbar_blogs_menu(). That creates the 『My Blogs』 menu item and all the items within the drop down menu.
We』re going to modify the 『My Blogs』 menu without changing the core code, thus helping to assure that our modifications don』t break when upgrading. We are going to sort the 『My Blogs』 menu items to have them appear in a specific order and eliminate the items that show up as 『Subscriber』.
The admin bar is triggered by wordpress 『actions』. These 『actions』 are events that are triggered throughout wp and bp. The admin bar responds to the action 『wp_footer』 which wp themes trigger at the end of each page in the theme footer. The admin bar registers it』s need to do something with the call to:
add_action( 'wp_footer', 'bp_core_admin_bar', 8 );

When the action 『wp_footer』 is triggered it will call the function named 『bp_core_admin_bar』. Looking at the function:
function bp_core_admin_bar() {
global $bp, $wpdb, $current_blog, $doing_admin_bar;

$doing_admin_bar = true;

if ( (int) get_site_option( 'hide-loggedout-adminbar' ) && !is_user_logged_in() )
return false;

echo '<div id="wp-admin-bar">';

// **** Do bp-adminbar-logo Actions ********
do_action( 'bp_adminbar_logo' );

echo '<ul class="main-nav">';

// **** Do bp-adminbar-menus Actions ********
do_action( 'bp_adminbar_menus' );

echo '</ul>';
echo '</div>';
}

At first glance it doesn』t seem to do much. It does though. It defines its own action and triggers that action at the end of the function:
// **** Do bp-adminbar-menus Actions ********
do_action( 'bp_adminbar_menus' );

At the bottom of bp-core-adminbar.php you』ll notice a collection of add_action() calls that respond to 『bp_adminbar_menus』.
add_action( 'bp_adminbar_menus', 'bp_adminbar_login_menu', 2 );
add_action( 'bp_adminbar_menus', 'bp_adminbar_account_menu', 4 );
add_action( 'bp_adminbar_menus', 'bp_adminbar_blogs_menu', 6 );
add_action( 'bp_adminbar_menus', 'bp_adminbar_notifications_menu', 8 );
add_action( 'bp_adminbar_menus', 'bp_adminbar_authors_menu', 12 );
add_action( 'bp_adminbar_menus', 'bp_adminbar_random_menu', 100 );

Each of the individual menu items responds to 『bp_adminbar_menus』. The one we are going to change is bp_adminbar_blogs_menu().
To change a function that is triggered by an action you have to unhook the current function that responds to the action and supply your own function that replaces the core function and then replace the add_action() call with one that responds to the same hook as the original function. This is how we』ll do that:
Original action hook:
add_action( 'bp_adminbar_menus', 'bp_adminbar_blogs_menu', 6 );

Remove the action hook:
remove_action('bp_adminbar_menus', 'bp_adminbar_blogs_menu', 6);

Replace the original action hook:
add_action('bp_adminbar_menus', 'my_adminbar_blogs_menu', 6);

Create a replacement function:
function my_adminbar_blogs_menu() {
if ( is_user_logged_in() ) {
global $bp;

if ( function_exists('bp_blogs_install') ) {

if ( !$blogs = wp_cache_get( 'bp_blogs_of_user_' . $bp->loggedin_user->id, 'bp' ) ) {
$blogs = get_blogs_of_user( $bp->loggedin_user->id );
wp_cache_set( 'bp_blogs_of_user_' . $bp->loggedin_user->id, $blogs, 'bp' );
}

$blogs = filter_blogs_by_role($blogs);

[snip] the rest of the original function [snip]
}

I』ve made one change to the original function.
$blogs = filter_blogs_by_role($blogs);

After the call to get_blogs_of_user() we get an array of 『blog』 objects back from wp where the user has any role. I want to sort these blog objects to my liking and then just allow the rest of the function to run and display them.
I』ve decided to call my sort function filter_blogs_by_role() and it takes the array of blog objects, does something to them and then returns that filtered array of user blog objects. The rest of the function goes happily along to display our filtered results.
I』m not going to delve into what the filter_blogs_by_role() function does. I』ll just repeat it here. It sorts the blog objects by role with 『Admin』 appearing before all the other roles.
// sort array of blog objs by $blog->role according to the order of $blog_roles
// roles not included in $blog_roles array will not be displayed
function filter_blogs_by_role($blogs){
global $bp, $blog_roles;

$blog_roles[] = __( 'Admin', 'buddypress' );
$blog_roles[] = __( 'Editor', 'buddypress' );
$blog_roles[] = __( 'Author', 'buddypress' );
$blog_roles[] = __( 'Contributor', 'buddypress' );
$blog_roles[] = __( 'Subscriber', 'buddypress' );

// get roles
foreach ($blogs as $blog){
$blog->role = get_blog_role_for_user( $bp->loggedin_user->id, $blog->userblog_id );
}

// eliminate roles not in $blog_roles
foreach ($blogs as $key => $value){
if (!in_array($value->role, $blog_roles))
unset($blogs[$key]);
}

// sort by $blog_roles sequence if there are any left
if ($blogs){
usort($blogs, 'compare_roles');
}

return $blogs;
}

// helper function
function compare_roles($a, $b){
global $blog_roles;

foreach($blog_roles as $key => $value){
if($a->role==$value){
return 0;
break;
}

if($b->role==$value){
return 1;
break;
}
}

Now we have a replacement function called my_adminbar_blogs_menu(). We』ve got to hook it up so that it actually does replace the orignal call to bp_adminbar_blogs_menu();
Any custom code that we create can be placed in a file called bp-custom.php. It gets loaded with all the rest of bp automatically by bp during each page load. If you don』t have one already then create one in /wp-content/plugins/. So we』ll pretend that we have done just that and that our three new functions are going to be loaded with the rest of bp.
bp-custom.php

So we have our shiny new replacement function and we now need to determine how best to hook it up to the rest of the universe and make it work. There really isn』t a 『best』 way to do this. There are several ways to do it though.
I like to keep all my custom bp code in bp-custom.php so that』s how I』m going to implement this. Since the admin bar responds to 『wp_footer』, I』m going to hook that event and do some things before the admin bar is displayed.
In bp-custom.php:
function my_alter_bp_adminbar(){
remove_action('bp_adminbar_menus', 'bp_adminbar_blogs_menu', 6);
add_action('bp_adminbar_menus', 'my_adminbar_blogs_menu', 6);
}
add_action('wp_footer','my_alter_bp_adminbar',1);

I』ve told wp that when it triggers the action 『wp_footer』 call the function my_alter_bp_adminbar() and do it with a priority of 『1』. A priority of 『1』 makes sure that it gets called before other functions that respond to 『wp_footer』.
If I add that to the other three functions I just stuffed into bp-custom.php then it will replace the original menu function with my new menu function and alter the behavior of bp when it displays the 『My Blogs』 menu.
Confusing enough for you? Welcome to event driven programming.
Other Articles on Modifying the Admin Bar
Limit Blog Creation to Admins

私信循环

私信循环

Codex Home → Developer Resources → Loops Reference → Private Messages Loop
Private Messages Loop

The private messages loop can be used to output a user』s private messages, both from their inbox and sentbox.
Standard Loop

There are no messages to display.

Accepted Parameters
The bp_has_message_threads() function will accept a number of parameters that will manipulate the data being returned.

user_id optional
The ID of the user to fetch messages for. By default this will be the user_id of the user currently logged in.

Default value: $bp->loggedin_user->id

box optional
Which box should the loop display messages from?

Accepted arguments: inbox, sentbox, notices
Default value: inbox

per_page optional
The number of messages to display on a page before they are paginated to the next page.

Default value: 10

max optional
The total number of messages to return.

Default value: false (no limit)

Advanced Usage
Fetch inbox messages for the user of ID 12. Get a maximum of 5 messages.

循环参考

循环参考

Codex Home → Developer Resources → Loops Reference
Loops Reference

Just as you use 「The Loop」 to loop through posts in WordPress, each content type in BuddyPress has its own custom loop.
By using these loops in your template, you can output any kind of information and really customize how content is displayed within your BuddyPress theme.
The default BuddyPress template files makes use of all the loops avaliable in BuddyPress. It provides a good example of using the loops in real use situations.

个人资料字段循环

个人资料字段循环

Codex Home → Developer Resources → Loops Reference → Profile Fields Loop
Profile Fields Loop

The profile data loop is the most complex out of all the custom BuddyPress loops. It』s actually two loops in one, the first is to loop through profile field groups, and the second to loop through profile fields in that profile field group.
Standard Loop

This user does not have a profile.

Accepted Parameters
The bp_group_has_profile() function will accept a number of parameters that will manipulate the data being returned.

profile_group_id optional
By default all groups and all fields will be displayed. If you provide the ID of a profile field group, then only the fields in this group will be displayed.

Default value: false

user_id optional
The ID of the user you want to fetch the profile data for. This is required if you are outside a member profile URL (/members/andy/…), otherwise it is the ID of the displayed user.

Default value: bp_displayed_user_id()

member_type optional
Limit fields by those restricted to a given member type, or array of  member types. If $user_id is provided, the value of $member_type will be overridden by the member types of the provided user. The special value of 『any』 will return only those fields that are unrestricted by member type – i.e., those applicable to any type.

Default value: false

hide_empty_groups optional
By default empty groups will not be displayed. If you provide the 0 value, then all the groups will be displayed.

Default value: true

hide_empty_fields optional
By default, only show empty fields if we』re on the Dashboard, or we』re on a user』s profile edit page, or this is a registration page. If you provide the 0 value, then all the fields will be displayed on your page.

Default value: !is_network_admin() && !is_admin() && !bp_is_user_profile_edit() && !bp_is_register_page()

fetch_fields optional
Whether to fetch each group』s fields.

Default value: false

fetch_fields_data optional
Whether to fetch data for each field. Requires a $user_id.

Default value: false

exclude_groups optional
Comma-separated list or array of group IDs to exclude.

Default value: array()

exclude_fields optional
Comma-separated list or array of field IDs to exclude.

Default value: array()

update_meta_cache optional
Whether to pre-fetch xprofilemeta for all retrieved groups, fields, and data.

Default value: true

Advanced Usage
Fetch all the profile data for the user with ID 10.

Fetch the profile data for fields in the profile group ID 2 for the user with ID 10.

Fetch the profile data for fields in the profile group ID 2.

Fetch all the profile data, even empty fieds and groups, for current user.

Fetch all the profile data for the user with ID 10, excluding fields with ID 5, 6, 7.

提高性能

提高性能

Codex Home → Getting Started → Improving Performance
Improving Performance

Due to the highly dynamic nature of social networks, without caching BuddyPress will add extra load on your server.
Thankfully, there are lots of options for increasing the performance of your server that take very little time to set up.
Reduce Database Hits
With many different content objects on each page, BuddyPress needs to pull a wide range of content from the database. The number of hits to the database is always a consideration and has been kept to a minimum where possible.
An excellent way to reduce the number of database hits per page even further is to turn on the persistent cache in the form of object caching.
To do this you will need to add: define( 'WP_CACHE', true ); into your wp-config.php file above the 「stop editing」 line.
You will then need to choose the type of persistent caching you would like to use. The most notable options are file based, memcached. If you』re unsure go with the file based caching.

File based
Memcached

You will need to make sure you have object-cache.php in your wp-content directory and a writable wp-content/cache/ directory for file based caching.
Reduce Memory Usage
An excellent way to significantly reduce memory usage is to install op-code caching on your server. There are two well known:

eAccellerator
XCache

Tweak Your Server
Tweaking your server settings can have a huge impact on performance. Even the smallest of adjustments can yield impressive results. Here are some articles that you may find helpful:

WP Codex: WordPress Optimization
WordPress optimization guide

Please feel free to edit this page and add any tactics you have found that increase your hardware performance.

好友 (Friends)

好友 (Friends)

Codex Home → Developer Resources → Action Reference → Friends
Friends

bp-friends.php

friends_screen_my_friends
friends_screen_requests
friends_screen_friend_finder
friends_screen_notification_settings
friends_format_activity
friends_format_notifications
friends_friendship_requested
friends_friendship_deleted
friends_friendship_accepted
friends_friendship_rejected
friends_remove_data

bp-friends/bp-friends-templatetags.php

friends_header_tabs

核心组件

核心组件

Codex Home → Developer Resources → Function Examples → Core Component
Core Component

Core component functions can be used by any official component, add-on component, WordPress or BuddyPress plugin. The core is always required, so you can be sure that the functions are always present.

bp_core_add_nav_item()

bp_core_add_nav_item()

Codex Home → Developer Resources → Deprecated → bp_core_add_nav_item()
bp_core_add_nav_item()

Deprecated
This function is deprecated in BuddyPress 1.1. Refer to bp_core_new_nav_item() going forward. Use of this function in BuddyPress versions greater than 1.1 is not recommended, but it will still work by internally calling bp_core_new_nav_item() to compensate.
Description
Adds a navigation item to the main navigation array used in BuddyPress themes.
Parameters
$nameString | Required | The display name for the navigation item.
$slugString | Required | The URL slug for the navigation item.
$css_idString | Optional | Default: $slug value | The ID to give the navigation item in HTML for CSS styling.
$add_to_usernav Boolean | Optional | Default: TRUE | Should this navigation item be added to all user』s profiles, not just the user who is logged in?
Notes
This function is deprecated in BuddyPress 1.1.
Last Updated by John James Jacoby on September 9, 2009 – 4:19pm EST