会员类型

会员类型

Codex Home → Developer Resources → Member Types
Member Types

BuddyPress 2.2 introduced the concept of member types. This functionality is outlined below.
Registering member types
BuddyPress itself does not register any member types. Plugins and themes can register member types using the bp_register_member_type() or the bp_register_member_types() function:
function bbg_register_member_types() {
bp_register_member_type( 'student', array(
'labels' => array(
'name' => 'Students',
'singular_name' => 'Student',
),
) );
}
add_action( 'bp_register_member_types', 'bbg_register_member_types' );

The first parameter of bp_register_member_type() is a string identifier for the member type, used internally by BP as the canonical name of the type. This name should be unique. The second parameter is a list of generic config arguments.
As of BP 2.2, only the 『labels』 parameter is supported. Future versions of BuddyPress will add additional parameters for better customization and development.
Registering a member type will also enable a meta box so administrators can set a member』s type when editing a user』s 「Extended Profile」 in the WP admin dashboard.
Member Specific Directory
BuddyPress 2.3 introduces the member-type-specific directories. It adds the new 『has_directory』 argument for bp_register_member_type() allowing developers to specify whether a list of members matching a given type 『foo』 should be available at http://example.com/members/type/foo/. A slug can be passed to 『has_directory』 to customize the URL used for the member type』s directory.
function bbg_register_member_types_with_directory() {
bp_register_member_type( 'student', array(
'labels' => array(
'name' => 'Students',
'singular_name' => 'Student',
),
'has_directory' => 'custom-name'
) );
}
add_action( 'bp_register_member_types', 'bbg_register_member_types_with_directory' );

Note that plugins registering member types must do so at the new hook 『bp_register_member_types』 in order to be able to customize the 『has_directory』 value (from its default of true). bp_has_members() automatically detects the presence of a member type in a URL. When no member type of the form example.com/members/type/foo/ is found, URLs of the form example.com/members/?member_type=foo will be detected.
Querying by member type
A common task is to retrieve a list of members of a given type (or set of types). bp_has_members() and BP_User_Query accept a 'member_type' parameter, which can be a single member type or an array/comma-separated list of member types. This will filter query results to those members matching the type. Example:
// Fetch all students and teachers.
$member_args = array(
'member_type' => array( 'student', 'teacher' ),
);
if ( bp_has_members( $member_args ) ) { // ...

Fetching and setting individuals』 member types
When BuddyPress detects that member types have been registered, it will display a Member Type metabox on the user』s Community Profile in Dashboard > Users. Administrators can use this interface to view or change a user』s member type.
BuddyPress also provides simple functions for fetching and setting member types programatically. To get the member type of a user, use bp_get_member_type():
// Get the member type of user 5412.
$member_type = bp_get_member_type( 5412 );

Set a user』s member type using bp_set_member_type():
// Set the member type of user 5412 to 'student'.
$member_type = bp_set_member_type( 5412, 'student' );

为新功能更新自定义主题

为新功能更新自定义主题

Codex Home → BuddyPress Theme Development → Updating Custom Themes For New Functionality
Updating Custom Themes For New Functionality

This section is your guide to BuddyPress theme updates, by version release.
From time to time Buddypress creates new functionality that requires templates and markup adjustments, adds new JavaScript scripting and perhaps requires CSS properties supporting these additions.
If your BuddyPress installation is a particularly custom one, that is, you have overloaded template files to your theme or child theme, maybe copied JS and CSS files as well and even running your own BuddyPress functions setup you will not necessarily inherit these new features, this section will list these changes grouped under version release numbering detailing just what template files, js, css you need to update in your theme.

Theme Updates for BP 2.7
Template updating and changes

Theme Updates for BP 2.2
Template updating and changes

Theme Updates for BP 2.1
Template updating and changes

模板更新 2.1

模板更新 2.1

Codex Home → BuddyPress Theme Development → Updating Custom Themes For New Functionality → Template Updates 2.1
Template Updates 2.1

BuddyPress 2.1 introduced three new features that specifically require template updates in order that they function.

@mentions Interface
Password Strength Meter
Activity Show Filters

@mentions Auto Suggest
@mentions introduces a drop down panel that will auto suggest user names to select from the characters you start typing. For this feature to work you will need to update your templates in two specific places or files

buddypress/activity/entry.php
buddypress/activity/post-form.php

In both of these files you need to locate the textarea elements and add the class 『bp-suggestions』 respectively:
?1<textarea id="ac-input-" class="ac-input bp-suggestions" name="ac_input_">

?1

@mentions also requires two new JS files and CSS styles to support the display of the dropdown, these files are loaded from the core directories so themes shouldn』t have to worry about including these.
Password Strength
Password Strength Meter This feature requires adjustments and new files as follows:

buddypress-functions.php
buddypress/members/register.php
buddypress/members/single/settings/general.php (user account password update screen)
css/buddypress.css
js/password-verify.js

The buddypress-functions.php adjustment most likely won』t be required but if it is you need to make these changes.
Just below the wp_enqueue_script(『comment-reply』) at around line 257 add:
?1234567891011121314// Maybe enqueue password verify JS (register page or user settings page)        if ( bp_is_register_page() || ( function_exists( 'bp_is_user_settings_general' ) && bp_is_user_settings_general() ) ) {            $min      = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';            $filename = "password-verify{$min}.js";             // Locate the Register Page JS file            $asset = $this->locate_asset_in_stack( $filename, 'js' );             // Enqueue script            $dependencies = array_merge( bp_core_get_js_dependencies(), array(                'password-strength-meter',            ) );            wp_enqueue_script( $asset['handle'] . '-password-verify', $asset['location'], $dependencies, $this->version);        }
register.php requires the addition of a new element to hold the password strength display, and also note the additional classes on the existing input controls, as follows:
?12

?1
general.php requires similar changes as register.php as follows:
?123  

 
Styles are required primarily to provide a background color separation between the various strength strings returned and those added to Buddypress are as follows – although you may want to modify these to suit your theme styling
?1234567891011121314151617181920212223242526272829303132333435#buddypress #pass-strength-result {    background-color: #eee;    border-color: #ddd;    border-style: solid;    border-width: 1px;    display: none;    margin: 5px 5px 5px 0;    padding: 5px;    text-align: center;    width: 150px;}#buddypress .standard-form #basic-details-section #pass-strength-result {    width: 35%;}#buddypress #pass-strength-result.error,#buddypress #pass-strength-result.bad {    background-color: #ffb78c;    border-color: #ff853c !important;    display: block;}#buddypress #pass-strength-result.good {    background-color: #ffec8b;    border-color: #fc0 !important;    display: block;}#buddypress #pass-strength-result.short {    background-color: #ffa0a0;    border-color: #f04040 !important;    display: block;}#buddypress #pass-strength-result.strong {    background-color: #c3ff88;    border-color: #8dff1c !important;    display: block;}
Lastly is the addition of a JS file 『password-verify.js』 this can be copied over from the bp legacy js directory or simply left in place as themes should inherit it』s functionality.
Activity 『Show』 filters
In 2.1 the activity filters were updated to dynamically generate the filter options, this allows for additional options to be passed in by plugins and CPT』s
Previously these filters were hardcoded select options in activity/index.php, groups/single/activity.php, and members/single/activity.php now those hardcoded options are replaced with a single function call:
?1bp_activity_show_filters()

It』s important to retain the first option though which resets the display to 『Everything』. On the groups activity page we need to pass an additional parameter to tell the function it』s the groups component so we add:
?1bp_activity_show_filters('group')

A full and detailed explanation of these changes is available in this codex page:
Activity Dropdown Filters in Templates

主题兼容性

主题兼容性

Codex Home → BuddyPress Theme Development → Theme Compatibility
Theme Compatibility

The pages under this section provide an overview of the BuddyPress theme compatibility layer first introduced in version 1.7.
An overview of theme compatibility is provided as a primer followed by a detailed explanation of the template hierarchy provided and expanded on in BP 1.8 finally we provide a guide page for those of you wanting to upgrade your installs from the now redundant Template Pack Plugin approach to this far more flexible theme compatibility approach.
Theme Compatibility – an introduction.
Theme Compatibility – the template Hierarchy in detail.

bp_activity_add()

bp_activity_add()

Codex Home → Developer Resources → Function Examples → bp_activity_add()
bp_activity_add()

bp_activity_add() is used to insert new activity items into the database.
Usage
$activity_id = bp_activity_add( $args );

Parameters

$args
An array that describes the activity item that you』re creating. Possible values:

'id'
(optional) Pass a numerical id to update an existing activity item
'action'
An HTML string summarizing the activity item, which is used by the template when displaying the activity item. Usually contains links to related users, groups, etc. E.g., 'Bill created the group TMNT'
'content'
(optional) The string content of the activity item. In the case of a blog post, for example, you might use an excerpt from the post content. In some cases, it』s appropriate for activity items not to have any content for this field – e.g., when a user creates a new group.
'component'
A string denoting the unique 「component」 that this activity item is associated with. BuddyPress components are 『groups』, 『members』, etc. If you are writing a BuddyPress plugin, you might consider having a single component name for all activity in your plugin.
'type'
A string denoting the specific kind of activity being created. This should be more specific than 'component'. For example, when creating a group, BuddyPress posts an activity item with the component 『groups』 and the type 『created_group』. 'type' is used for filtering, as in the 「New Groups」, 「New Blog Posts」, etc dropdown on Activity directories.
'primary_link'
(optional) The primary URL associated with the activity item. BuddyPress uses this value when creating activity RSS feeds, to tell the feed reader where to find the original item. If you omit this value, the fallback value is the permalink page for the single activity item.
'user_id'
(optional) The unique numeric id of the user associated with the activity item. In BuddyPress, the primary use of this value is to filter which items appear on the Activity tab of an individual member』s profile. If the value is omitted, it will default to the id of the currently logged-in user. Note that you can pass 0 for items that may be associated with no user at all.
'item_id'
(optional) The numeric ID of the primary item associated with this activity item. For example, when recording the creation of a group, BuddyPress sets the 'item_id' to the numeric id of the newly created group. This value can be used for filtering.
'secondary_item_id'
(optional) A second numeric ID for further filtering. For example, when recording a new blog comment, BuddyPress sets the 'item_id' to the ID of the blog, and the 'secondary_item_id' to the ID of the comment.
'recorded_time'
(optional) The time the activity is recorded. Should be in GMT, MySQL format. (In PHP, date( 'Y-m-d H:i:s', $time ).) Defaults to the current time.
'hide_sitewide'
(optional) 'hide_sitewide' is used in a few different ways:

To prevent duplicate entries when viewing the sitewide activity stream. For instance, when a new friendship is established, two activity items are created: one with 'user_id' set to the user who sent the request and 'item_id' set to recipient, and the other one vice versa. We need both items so that both users see the item on their own profiles, but it』s not desirable to see both of them on the sitewide stream, so the second one is marked 'hide_sitewide' => true.
To prevent activity items from hidden or private groups from appearing in sitewide activity streams. That is, when an activity item is posted to a non-public group, 'hide_sitewide' is set to false. Then, when viewing the group』s activity stream (which is accessible only to members), BP includes 'hide_sitewide' items; in contrast, the sitewide stream excludes these items.

Thus, 'hide_sitewide' is used simultaneously for preventing duplicates and for privacy. Please be sure you understand the way that 'hide_sitewide' works before attempting to use it for the purposes of privacy in your own plugins.
'is_spam'
(optional) Set to true to mark an item as spam.

Return value
New database row activity ID
Source File
bp_activity_add() is located in bp-activity/bp-activity-functions.php

将您的插件链接添加到 BuddyPress 会员工具栏菜单

将您的插件链接添加到 BuddyPress 会员工具栏菜单

Codex Home → BuddyPress Plugin Development → Adding Your Plugin』s Link to the BuddyPress Member Toolbar Menu
Adding Your Plugin』s Link to the BuddyPress Member Toolbar Menu

If you』ve added a new navigation item to the single Member page, you may want to add the same link to the Toolbar Menu.
Let』s say you added a tab called 『Dogs』 – this will add the same link to the Toolbar Menu.
Put it in the same file that adds your tab on the Member page.
Or put it in your theme』s functions.php or bp-custom.php or your plugin file.
function your_bp_admin_bar_add() {
global $wp_admin_bar, $bp;

if ( !bp_use_wp_admin_bar() || defined( 'DOING_AJAX' ) )
return;

$user_domain = bp_loggedin_user_domain();
$item_link = trailingslashit( $user_domain . 'dogs' );

$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-dogs',
'title' => __( 'Dogs', 'your-plugin-domain' ),
'href' => trailingslashit( $item_link ),
'meta' => array( 'class' => 'menupop' )
) );

// add submenu item
$wp_admin_bar->add_menu( array(
'parent' => 'my-account-dogs',
'id' => 'my-account-dogs-poodles',
'title' => __( 'Poodles', 'your-plugin-domain' ),
'href' => trailingslashit( $item_link ) . 'poodles'
) );
}
add_action( 'bp_setup_admin_bar', 'your_bp_admin_bar_add', 300 );

To remove an item from the Toolbar Menu, for example: activity, place this code in one of the locations mentioned above:
function your_bp_admin_bar_remove() {
global $wp_admin_bar;

$wp_admin_bar->remove_menu( 'my-account-activity' );

}
add_action( 'bp_setup_admin_bar', 'your_bp_admin_bar_remove', 301 );

To find out what should go in the remove_menu argument, look at the browser code generated for the Toolbar. For 『Activity』, it looks like this:

修改注册表单

修改注册表单

Codex Home → Getting Started → User Submitted Guides → Modifying the Registration Form
Modifying the Registration Form

Once you』ve installed WordPress and BuddyPress, created WordPress pages for BuddyPress to use for Registration and Activation, and then enabled 「Anyone can register」 (under Settings > General > Membership), you』re ready to let users sign up for your new site.
Tip: If you』re already logged in to your new site, visiting mysite.net/register will redirect you to the site home page. Not to worry, that』s the expected behavior, since there』s no point in registering if you』re already registered, right? So fire up a second browser that』s not logged into the site, and point it at your registration page. By default, it』ll look something like this.

The section 「Account Details」 is the portion required for WordPress; the other portion, 「Profile Details,」 is the part BuddyPress adds.

BuddyPress offers a tool to customize the Profile Details section. You can access it by visiting the WordPress administration area and going to Users > Profile Fields. The only field that appears by default is Name (Primary), which is a required field. This field serves as the user』s display name on the site; it is displayed at the top of the user』s profile and also in activity items.

Profile fields can collect a variety of information from your users. BuddyPress 1.8 offers the following field types: text box, multi-line text box, date selector, radio buttons, drop-down select box, multi select box and checkboxes. Let』s add a group of radio buttons to collect your users』 favorite color upon signup. First, click on the 「Add New Field」 button.

In this example, you』ve added the 「field title」 (used as the label on the registration form) and the 「field description」 (used as explanatory text on the registration form if not empty), decided that this isn』t a required field and entered your first color option.

You might need more options, so you can click the link 「Add another option」 to add more options until you』re satisfied.

Whoops, if you didn』t enter the colors in the right order, you can click and drag the options to correct the order. If you need to remove an option, you can click the [x] to the right of the option.

Now you can set the default visibility setting, add an option for the user to override the visibility settings, and save the new field.

The new field appears in the form administration area. Using your other browser (the one that isn』t logged into the site), check your work by reloading the registration form.

You can change the order the profile fields appear on the registration form by clicking and dragging to reorder the fields.

While the new user』s favorite color is interesting, you』ve decided that it』s not important enough to clog up the registration form. Instead, you』d like to have a way for the user to fill that out after she』s registered. This is where profile field groups come into play. Back in the WP profile field admin, notice that the fields are organized under a tab that reads 「Base (Primary)」. This refers to the profile field group that the field belongs to. Fields in the 「Base」 group are displayed on the registration form. You can add another group (or more) to collect other information. We』ll add a new group by clicking on the link 「Add New Field Group」 next to the page title 「Profile Fields.」

Give the new group a title and description, then click on 「Create Field Group.」

Now, you can move the 「Favorite Color」 field from the 「Base」 group to the 「More Details」 group by dragging it to the 「More Details」 tab.

The 「Favorite Color」 field is no longer displayed on the registration form but can be edited via the BuddyPress user profile.

在成员个人资料上显示个人资料字段

在成员个人资料上显示个人资料字段

Codex Home → BuddyPress Theme Development → User Submitted Guides → Displaying Extended Profile Fields on Member Profiles
Displaying Extended Profile Fields on Member Profiles

BuddyPress has some nice built-in tools for collecting data from your members. You』ll be calling the 「extended profile data」 or 「xProfile」 in BuddyPress-speak in your theme. You can retrieve and display this data without modifying template files by attaching a new function to an existing action hook provided by BuddyPress.
For example, Carlos has registered your site and filled out the 「Preferred Color」 xProfile field you added in the profile form you built. And you』d like to display his preference in the header of his member profile. His member profile looks like this by default.

You can add Carlos』 color preference to the profile header by attaching a new function to a hook that BuddyPress provides for you to use. (Read more about action hooks.)
Looking through the BuddyPress template files, you』ll find a file called member-header.php: wp-content/plugins/buddypress/bp-templates/bp-legacy/member-header.php. In that file, look for a line which starts with do_action(…) for the right place to hook your new function to. Line 56 includes 'do_action( 'bp_profile_header_meta' )'. You can add the new code in your theme』s functions.php file, following the example provided at the WordPress Codex: add_action( $hook, $function_to_add, $priority, $accepted_args );
So you will start with:
add_action( 'bp_profile_header_meta', 'display_user_color_pref' );
function display_user_color_pref() {
//This is where the code to display the user data will go.
}

Then you』ll use the BuddyPress function bp_profile_field_data() to retrieve and display the preference. The function bp_profile_field_data fetches and prints the value to screen (like the_content() does) and expects arguments in an array like this:
$args = array(
'field' => 'Favorite Color', // Field name or ID.
'user_id' => bp_displayed_user_id() // Default
);

Since you want to show the color preference of the displayed user (the member whose profile you』re viewing) and bp_profile_field_data uses that member』s ID for its default, you only need to specify the ID or field name of the extended profile field. You can check this value by visiting the Users > Profile Fields page in the WordPress admin. The field name is displayed as the field』s title, and, if you hover over 「Edit,」 the field』s ID is visible in the url: http://example.com/wp-admin/users.php?page=bp-profile-setup&group_id=2&field_id=2&mode=edit_field

In this case, you can use 『Favorite Color』 or the ID, 2:
function display_user_color_pref() {
echo 'I choose this color: ';
$args = array(
'field' => 'Favorite Color', // Field name or ID.
);
bp_profile_field_data( $args );
}

or
function display_user_color_pref() {
echo 'I choose this color: ';
$args = array(
'field' => 2, // Integers do not need to be enclosed in quotes.
);
bp_profile_field_data( $args );
}

But there』s a snag: what if the user didn』t input a favorite color?

You can use the function bp_get_profile_field_data instead to retrieve the value. Then, if there is a value, you』ll be able to display it. The complete code:
add_action( 'bp_profile_header_meta', 'display_user_color_pref' );
function display_user_color_pref() {
$args = array(
'field' => 'Favorite Color', // Field name or ID.
);
$favorite_color = bp_get_profile_field_data( $args );

if ($favorite_color) {
echo 'I choose this color: ' . $favorite_color;
}

}

If the member』s favorite color isn』t specified, then the function outputs nothing.

防止垃圾邮件发送者注册

防止垃圾邮件发送者注册

Codex Home → Getting Started → User Submitted Guides → Preventing Spammer Registration
Preventing Spammer Registration

Spammers and sploggers pose a serious risk to online communities. Without some protection in place, your fledgling community runs the risk of being overrun by spammers trying to sell fake Uggs and Oakleys. Since fake users can easily make up 98% of all new user account requests, it』s useful to have a game plan for stopping or slowing these fake registrations.
BuddyPress by default requires a user to activate his account via an e-mail sent to the address provided at signup. This simple check will weed out the sploggers and spammers who are using e-mail addresses harvested from the web that they do not have access to, but will not stop users who do have access to a working e-mail address.
WangGuard works by checking new users』 e-mail addresses against a database of e-mails that have been used by spammers on other sites. If the new request uses a known bad address, WangGuard will prevent the user from registering. In addition, it adds some tools that allow your users to flag spam users.
If you』d like to be a little more hands-on, BuddyPress Registration Options requires that a site administrator approve each new registration request. (This plugin will work with WangGuard.)
There are simple changes you can make to the registration form to help identify spambots, too.
Honeypots work by creating hidden fields on the registration form that spam bots can』t resist, then checking for input in those fields upon form submission. 「Humanity tests」 ask the user to respond to a question that should stump a spambot, for example, 「What color is snow?」. Finally, CAPTCHAs are ubiquitous and challenge the user to figure out what letters are shown in a distorted image. Which of these options you choose (you could technically employ all three) is a matter of preference. Honeypots have the advantage of being the least intrusive; 「real」 users won』t even know they』re there. Humanity tests might surprise your users because they』re unusual, but stop spambots effectively. CAPTCHAs are everywhere, so, while they might be annoying, at least they annoy your users in a familiar way. These three strategies will only stop spambots, though; human spammers will be able to defeat any of them.