將您的外掛連結新增到 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 → Change Default Members Profile Landing Tab
Change Default Members Profile Landing Tab

By default, BuddyPress will load the Activity tab when clicking on a member』s profile link. If you would like to change this default landing tab to something else than the Activity tab you can do so.
Constants such as this are best placed in files that load before BP is fully loaded. Add the code below to either wp-config.php or bp-custom.php (which you would create in your plugin folder as this file does not exist by default). file. When adding the code to wp-config.php, place it after define(『DB_COLLATE』, 」); and before the authentication keys. If you add this line to the bottom of wp-config.php, it will not work.
/**
* Change BuddyPress default Members landing tab.
*/
define('BP_DEFAULT_COMPONENT', 'profile' );

This example will load the 『profile』 tab instead of the 『activity』 screen it』s fairly easy and you can take any other option from the tabs-menu.

修改登錄檔單

修改登錄檔單

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.

將外掛選項新增到 BuddyPress 設定頁面

將外掛選項新增到 BuddyPress 設定頁面

Codex Home → BuddyPress Plugin Development → Adding Plugin』s Options to BuddyPress Settings Page
Adding Plugin』s Options to BuddyPress Settings Page

When writing your plugin you may need to give the community administrator the ability to set some options about it to eventually let him customize some behaviors. When you have a lot options, you would use the WordPress built in add_options_page() or add_submenu_page() functions to create your plugin』s settings page.
But, If your plugin only needs a very few options to be set by the community administrator, it can be interesting to use BuddyPress settings administration screen to add your own fields.
Sections

Choosing the best BuddyPress settings section
Adding your field to bp_main settings section
Adding a new settings section
Additionnal resources

Choosing the best BuddyPress settings section
BuddyPress settings are divided into sections. Depending on your plugins features you can choose to include your setting in the best one. For instance, if your plugin is extending the Groups component, you can choose the 『bp_groups』 section.
Available BuddyPress settings sections
Adding your field to bp_main settings section
The following code illustrates how you could add your setting field to the 『bp_main』 BuddyPress settings section.
/**
* Your settings main function
*/
function bp_plugin_admin_settings() {

add_settings_field(
/* the option name you want to use for your plugin */
'bp-plugin-option-name',

/* The title for your setting */
__( 'BP Plugin Setting', 'bp-plugin-domain' ),

/* Display function */
'bp_plugin_setting_field_callback',

/* BuddyPress settings */
'buddypress',

/* BuddyPress setting section
Here you are adding a field to 'bp_main' section.
As shown on the image, other available sections are :
- if xprofile component is active : 'bp_xprofile',
- if groups component is active : 'bp_groups',
- if legacy forums component is active : 'bp_forums',
- if activity component is active : 'bp_activity'
*/
'bp_main'
);

/* This is where you add your setting to BuddyPress ones */
register_setting(
/* BuddyPress settings */
'buddypress',

/* the option name you want to use for your plugin */
'bp-plugin-option-name',

/* the validatation function you use before saving your option to the database */
'bp_plugin_setting_field_validate'
);

}

add_action( 'bp_register_admin_settings', 'bp_plugin_admin_settings' );

/**
* This is the display function for your field
*/
function bp_plugin_setting_field_callback() {
/* if you use bp_get_option(), then you are sure to get the option for the blog BuddyPress is activated on */
$bp_plugin_option_value = bp_get_option( 'bp-plugin-option-name' );

// you can use the checked() function to easily add a checked attribute to the checkbox if needed
?>
<input id="bp-plugin-option-name" name="bp-plugin-option-name" type="checkbox" value="1" />

<input id="bp-plugin-option-name" name="bp-plugin-option-name" type="checkbox" value="1" />

<?php
}

Here is a screenhot of your custom section in BuddyPress settings.
A custom section for your plugin』s option

Additional resources

add_options_page()
add_submenu_page()
add_settings_section()
add_settings_field()
add_settings_section()

在成員個人資料上顯示個人資料欄位

在成員個人資料上顯示個人資料欄位

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.

如何透過 WP 使用者螢幕將成員新增到群組

如何透過 WP 使用者螢幕將成員新增到群組

Codex Home → Developer Resources → User Submitted Guides → How to Add Members to a Group via WP Users Screen
How to Add Members to a Group via WP Users Screen

Where we are: users page in the admin screen
What we want to do: add one or n members to a group
What we want to get: an Add to BP Group menu in the Bulk Actions menu box
What we need: a group ID, one or more users. Don』t forget to check the members you want to add!
Where to find the group ID ? Hoovering the 「edit」 button on the groups page will show the ID in the browser bottom left corner, something like http://example.com/wp-admin/admin.php?page=bp-groups&gid=9&action=edit (group ID is 9)

Add this action hook into your theme』s functions.php to provide functionality on a theme basis or add the code to your bp-custom.php file to provide the functionality to your site regardless of theme being used.
Action hook
Caution: This snippet can only be used on servers running PHP 5.3 and above. For previous versions see below please.
function add_users_to_bpgroup() {
if( bp_is_active('groups') ):

if( isset( $_REQUEST['action'] ) && isset( $_REQUEST['bp_gid'] ) && isset( $_REQUEST['allusers'] ) ) {
$group_id = $_REQUEST['bp_gid'];
$users = $_REQUEST['allusers'];

foreach ( $users as $user_id ) {
groups_join_group( $group_id, $user_id );
}
}
//form submission
add_action( 'admin_footer', function() { ?>

jQuery("select[name='action']").append(jQuery('Add to BP Group'));
jQuery("#doaction").click(function(e){
if(jQuery("select[name='action'] :selected").val()=="groupadd") { e.preventDefault();
gid=prompt("Enter a Group ID","1");
jQuery(".wrap form").append('').submit();
}
});

<?php
});

endif;
}
add_action ( 'load-users.php', 'add_users_to_bpgroup' );

Use this if your server is running PHP

jQuery("select[name='action']").append(jQuery('Add to Group'));
jQuery("#doaction").click(function(e){
if(jQuery("select[name='action'] :selected").val()=="groupadd") { e.preventDefault();
gid=prompt("Enter a Group ID","1");
jQuery(".wrap form").append('').submit();
}
});

<?php
};
add_action( 'admin_footer', 'add_users_to_bpgroup_js' );

防止垃圾郵件傳送者註冊

防止垃圾郵件傳送者註冊

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.

主題相容性

主題相容性

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 預設主題 – BP 1.9 向前發展

BP 預設主題 – BP 1.9 向前發展

Codex Home → BuddyPress Theme Development → BP Default Theme – moving forward with BP 1.9
BP Default Theme – moving forward with BP 1.9

Updated BP default theme usuage for BP 1.9 onwards

The BP default theme has served a hugely useful purpose in providing a true out of the box experience for users, allowing for a standard to work from either in re-skinning or for building child themes from. With the successful rollout of the 『Theme Compatibility』 functionality in BP 1.7 a decision was made by the core development team that it was time to officially retire the default bundled theme from the core application. As of BP 1.9 users will note that the listing of the bp-default theme is removed from the WP 『Appearance > Themes』 screens (although it will still appear if you currently use the theme)

What does this mean?
In terms of future development of the theme, this will be limited to providing critical fixes and security updates where necessary but there will be no further development on the theme template files. The theme will continue to be physically bundled in the core download for the immediate future but it is envisioned that at some point the theme files will be removed altogether and hopefully be provided as a standalone theme downloaded from the WP theme repo.
How do these changes affect current theme use?
Have no worries, if you currently use the default theme on your site things will continue to work just as before. BP has the provision to check whether bp-default is activated or whether a theme has overloaded bp-default templates to the top level of their theme and will then disable theme compatibility mode.
There is one important change in behaviour to take into account if you are currently using bp-default and you need to revert to another theme for testing you will find the option to select bp-deault has been removed, don』t panic though to ensure you can still change themes and re-select bp-default you will need to add a simple one line piece of code

?1add_filter( 'bp_do_register_theme_directory', '__return_true' );

Add this line to your bp-custom.php file located in the WP plugins directory ( this file does not exist by default and needs to be manually created and must begin with <?php at the very top of the file.)

Related Resources

Official Announcement: The Future Of The BP-Default Theme