使用 bp_parse_args() 过滤 BuddyPress 模板循环

使用 bp_parse_args() 过滤 BuddyPress 模板循环

Codex Home → Developer Resources → Using bp_parse_args() to filter BuddyPress template loops
Using bp_parse_args() to filter BuddyPress template loops

Prologue
In the past, it has been extremely difficult to filter any BuddyPress template loop.
For example, let』s say I wanted all activity loops to show the last five entries instead of the default of 20. It was possible, but basically you』d either have to requery the activity loop to grab the last five entries and override the 'bp_has_activities' filter or override the activity SQL statement using the 'bp_activity_paged_activities_sql' filter.
Both methods are totally not cool.
Introducing bp_parse_args()!
The introduction of the bp_parse_args() function in BuddyPress 2.0 makes this way simpler. Let』s take a look at the bp_has_activities() loop with the bp_parse_args() function:
https://buddypress.trac.wordpress.org/browser/tags/2.0/bp-activity/bp-activity-template.php#L525
Once you』ve analyzed that a bit, let』s move on to the specific usage of bp_parse_args() in the bp_has_activities() loop:
?1$r = bp_parse_args( $args, $defaults, 'has_activities' );
In particular:

The first argument, $args, are the parameters for the activity loop initially passed by bp_has_activities().
The second argument, $defaults, are some default parameters for the activity loop that are used if the $args variable does not contain them.
The third argument, 'has_activities', is a unique identifier used for this instance of bp_parse_args(). More on this later.

Now that you know how bp_parse_args() is used in bp_has_activites(), let』s analyze the bp_parse_args() function and how we can use this to our advantage.
bp_parse_args() in depth
The bp_parse_args() function can be found here:
https://buddypress.trac.wordpress.org/browser/branches/2.0/bp-core/bp-core-functions.php#L205
In this function, there are two filters:

'bp_before_{$filter_key}_parse_args' – Allows you to filter the initial arguments, $args.
'bp_after_{$filter_key}_parse_args' – Allows you to filter the arguments after $args has been merged with $defaults.

You』re probably wondering what $filter_key is. $filter_key is the third argument from bp_parse_args().
In our case, it is 'has_activities'.
So if I wanted to filter the activity loop parameters, the filters would look like this:

'bp_before_has_activities_parse_args'
'bp_after_has_activities_parse_args'

Now that we』ve taken a look at the bp_parse_args() function and how it applies filters to the arguments. Let』s actually write our override filter!
Filtering bp_parse_args()
Let』s go back to the original issue. I want to filter all activity loops to show only the last five entries instead of the default of 20.
Using the 'bp_after_has_activities_parse_args' filter that we』ve determined above, let』s write our override function:
?1234567// Fetch only the last five entries for all activity loopsfunction my_bp_activities_per_page_5( $retval ) {    $retval['per_page'] = 5;     return $retval;}add_filter( 'bp_after_has_activities_parse_args', 'my_bp_activities_per_page_5' );
So what did we just do? We』re overriding the 'per_page' parameter so only the last five entries are fetched instead of the default of 20. It』s as simple as that!
Note that this overrides all activity loops across BuddyPress. What if I only wanted to fetch the last five entries only on a user』s activity page? Easy, just add your conditionals:
?123456789function my_bp_activities_per_page_5_on_user_activity( $retval ) {    // only fetch the last five entries if we're on a user's activity page    if ( bp_is_user_activity() ) {        $retval['per_page'] = 5;    }     return $retval;}add_filter( 'bp_after_has_activities_parse_args', 'my_bp_activities_per_page_5_on_user_activity' );
You can get uber-creative by filtering all the other parameters in the activity loop:
https://buddypress.trac.wordpress.org/browser/tags/2.2.1/src/bp-activity/bp-activity-template.php#L442
Conclusion
Now that you know how to use the bp_parse_args() function, you should be able to easily filter any BuddyPress template loop!
For convenience, here are the other template loops using bp_parse_args():

Blogs – https://buddypress.trac.wordpress.org/browser/tags/2.2.1/src/bp-blogs/bp-blogs-template.php#L366
Groups – https://buddypress.trac.wordpress.org/browser/tags/2.2.1/src/bp-groups/bp-groups-template.php#L431
Members – https://buddypress.trac.wordpress.org/browser/tags/2.2.1/src/bp-members/bp-members-template.php#L461
Message Threads – https://buddypress.trac.wordpress.org/browser/tags/2.2.1/src/bp-messages/bp-messages-template.php#L372
Notifications – https://buddypress.trac.wordpress.org/browser/tags/2.2.1/src/bp-notifications/bp-notifications-classes.php#L556
XProfile – https://buddypress.trac.wordpress.org/browser/tags/2.0/bp-xprofile/bp-xprofile-template.php#L169

管理用户注册

管理用户注册

Codex Home → Administrator Guide → Manage User Signups
Manage User Signups

There are times when prospective members cannot log in after trying to activate their accounts. BuddyPress has got you covered with a new administrative panel added to the Users screen. You can check the registrant』s status by either:

wp-admin menu Users > click on the Pending link, orwp-admin menu Users > Manage Signups which will bring you directly to the panel.

The Pending users panel lets you see a list of signups that have not yet been activated. Perform common tasks with pending signups:

manually activate accounts,resend activation emails, ordelete the account.

Related resource:

Alternative Registration Workflows

将元框添加到管理员扩展个人资料

将元框添加到管理员扩展个人资料

Codex Home → Developer Resources → Add Meta Box to Admin Extended User Profile
Add Meta Box to Admin Extended User Profile

BuddyPress 2.0 allows admins to edit user profile fields from the Dashboard>>Users>>Edit User page. This extended profile page offers the ability to add your own settings for a user. This page gives a simple example of how to add the meta boxes to the extended profile page. What you add to the meta box can be a myriad of options. NOTE: any meta boxes you add are only editable by an admin. The info does not show on the front end user profile. You can put this example code in your bp-custom.php file.

This function adds our meta box to the the user extended profile page in the admin.
function bp_user_meta_box() {

add_meta_box(
'metabox_id',
__( 'Metabox Title', 'buddypress' ),
'bp_user_inner_meta_box', // function that displays the contents of the meta box
get_current_screen()->id
);
}
add_action( 'bp_members_admin_user_metaboxes', 'bp_user_meta_box' );

 
This function outputs the content of the meta box. The skies the limit here. You can add almost any information to want. You could have a meta box that you enter notes about that user or show information from another plugin.
function bp_user_inner_meta_box() {
?>

This is where you write your form inputs for user settings. Or you can output information pertaining to this user. For example, the Achievements plugin could show the users badges here.

<?php
}

 
This function saves any form inputs you might include in the meta box.
function bp_user_save_metabox() {

if( isset( $_POST['save'] ) ) {

$user_id = isset( $_GET['user_id'] ) ? $_GET['user_id'] : 0;

// you will need to use a $_POST param and validate before saving
$meta_val = isset( $_POST['form_value'] ) ? sanitize_text_field( $_POST['form_value'] ) : '';

// the $meta_val would be a $_POST param from inner meta box form
update_user_meta( $user_id, 'user_meta_key', $meta_val );
}
}
add_action( 'bp_members_admin_update_user', 'bp_user_save_metabox' );

BuddyPress 修复工具管理屏幕

BuddyPress 修复工具管理屏幕

Codex Home → Administrator Guide → BuddyPress Repair Tools Admin Screen
BuddyPress Repair Tools Admin Screen

Occasionally, BP friend counts and other data can get out of sync. The new Tools screen lets admins manually reset these values. Go to wp-admin Menu > Tools > BuddyPress and run repair tools one at a time only, especially on large sites.
When you have a new installation, the default repair tools are for:

Count total members
Repair user 「last activity」 data

As more people register, and when you enable the Friends and Groups components, you』ll find repair tools for the additional data generated per image below:

Count friends for each user
Count groups for each user
Count total members
Repair user 「last activity」 data

BuddyPress 用您的语言

BuddyPress 用您的语言

Codex Home → Getting Started → Customizing → BuddyPress in Your Language
BuddyPress in Your Language

Like WordPress, BuddyPress has the built in capability to be used in any language. The instructions below assume you have already configured WordPress in Your Language and Installing WordPress in Your Language.
Like WordPress, you don』t have to lift a finger to apply BuddyPress translations or translation updates. Most sites are now able to automatically apply these updates in the background. If your site is capable of one-click updates without entering FTP credentials, then your site should be able to automatically update translations.
Is your language missing?
BuddyPress translation packages are only updated if the translation is at 100% translated, if you do not see your language after updating your translations, please consider contributing to the the BuddyPress translation project, to get started see Translating WordPress and post any questions you have on the WordPress Polyglots blog.
Getting the language files
Go to https://translate.wordpress.org/projects/wp-plugins/buddypress and select your preferred language e.g. Hungarian
Select 『all current』 as 『.po』 and click 『export』 and save this file to a folder on your computer, do the same again this time selecting 『.mo』
Using the language files
Rename each file of the files to buddypress-language_COUNTRY.extension
eg.
wp-plugins-buddypress-stable-hu.po to buddypress-hu_HU.po and
wp-plugins-buddypress-stable-hu.mo to buddypress-hu_HU.mo
Using FTP upload both the .po and .mo files to /wp-content/languages/ of your WordPress instalation. If the /wp-content/languages/ folder does not exist, create it.

来自插件的模板重载

来自插件的模板重载

Codex Home → BuddyPress Plugin Development → Template Overload from a Plugin
Template Overload from a Plugin

Note: overloading and / or adding templates from plugins can be quite complex. For more detailed discussion and examples, please see:

Upgrading older plugins that bundle custom templates for BP 1.7
Using BP Theme Compat in Plugins
Theme Compatibility & Template Files

The purpose of this page is to provide a simple example of overloading an existing template part. The example shown will allow you to overload the member-header.php file which creates the BuddyPress profile page header section (avatar, messaging buttons, profile navigation menu etc.) for profile pages where cover images are not used. If you are using cover images on the users profile then the file to overload is cover-image-header.php. Both of these files are located in plugins/buddypress/bp-templates/bp-legacy/members/single.
If you』re using BP 1.7 or greater and not using the bp-default theme, you can use this approach…
It consists of 2 files and a template folder structure:

loader.php
bp-tol.php
templates/members/single/member-header-tol.php

loader.php
/*
Plugin Name: BP Template Overload
Plugin URI: http://philopress.com
Description: Load templates from a plugin
Version: 1.0
Author: shanebp
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

if ( !defined( 'ABSPATH' ) ) exit;

function bp_tol_init() {

define( 'BP_TOL_DIR', dirname( __FILE__ ) );

require( BP_TOL_DIR . '/bp-tol.php' );

}
add_action( 'bp_include', 'bp_tol_init' );

bp-tol.php
// register the location of the plugin templates
function bp_tol_register_template_location() {
return BP_TOL_DIR . '/templates/';
}

// replace member-header.php with the template overload from the plugin
function bp_tol_maybe_replace_template( $templates, $slug, $name ) {

if( 'members/single/member-header' != $slug )
return $templates;

return array( 'members/single/member-header-tol.php' );
}

function bp_tol_start() {

if( function_exists( 'bp_register_template_stack' ) )
bp_register_template_stack( 'bp_tol_register_template_location' );

// if viewing a member page, overload the template
if ( bp_is_user() )
add_filter( 'bp_get_template_part', 'bp_tol_maybe_replace_template', 10, 3 );

}
add_action( 'bp_init', 'bp_tol_start' );

member-header-tol.php
Make a copy of
buddypress/bp-templates/bp-legacy/buddypress/members/single/member-header.php
rename it and make some small change so that it』s obvious that it』s being loaded.
Then save it in the folder structure shown above.

That』s it !
And you can overload member-header-tol.php by placing a copy in your child-theme directory: [theme]/buddypress/members/single/member-header-tol.php

已弃用的指南

已弃用的指南

Codex Home → Getting Started → User Submitted Guides → Deprecated Guides
Deprecated Guides

Pages in this section represent older code and functions that are no longer current and have been deprecated in the core BP plugin.
Generally there will be newer alternative methods to replace these deprecated ones.
These guides are preserved for backwards compatibility as deprecated code may still be in use and the guides here of some relevance and usefulness.

在 WordPress 多站点中安装

在 WordPress 多站点中安装

Codex Home → Getting Started → Installation in WordPress Multisite
Installation in WordPress Multisite

Before installing BuddyPress, please make sure that you』ve checked the minimum server requirements and WordPress version compatibility.
You can install BuddyPress in your Network (multisite) in either of the following setups.
Network-wide
A. BuddyPress root blog in Main Site
B. BuddyPress root blog in Secondary Site
One site of the Network
C. BuddyPress Activated in Main Site only
D. BuddyPress Activated in Secondary Site only
Posts, comments, or activities of the users in sites other than where you activated BuddyPress won』t be recorded in the Activity Streams when you install BuddyPress in only one site of the network.
Special Setups
E. BP_ENABLE_MULTIBLOG – network activated
F. BuddyPress Multisite – network activated
A. Network-wide Activation – BuddyPress root blog in Main Site

Go to Dashboard → Network Admin.
Add BuddyPress through Plugins → Add New.
Activate BuddyPress.
Configure BuddyPress for Multisite.

B. Network-wide Activation – BuddyPress root blog in Secondary Site

Go to Dashboard → Network Admin.
Click on Sites link.
Find the ID number of the site you want to be the root site of your BuddyPress installation.
Open up your installation』s wp-config.php file.
Add
define ( 'BP_ROOT_BLOG', $blog_id );
to your wp-config.php file where $blog_id is the ID number of your chosen site and save the file.
Go to Dashboard → Network Admin.
Add BuddyPress through Plugins → Add New.
Activate BuddyPress. You will be redirected to the BuddyPress Welcome screen.
Configure BuddyPress for Multisite.

C. Activate BuddyPress in the Main Site of the Network only

Go to Dashboard → Network Admin.
Add BuddyPress through Plugins → Add New.
Proceed to the Dashboard of the Main Site.
Navigate to Plugins → Plugins .
Activate BuddyPress. You will be redirected to the BuddyPress Welcome screen.
Configure BuddyPress.

D. Activate BuddyPress in only one of the Secondary Sites of the Network

Go to Dashboard → Network Admin.
Click on Sites link.
Find the ID number of the site you want to be the root site of your BuddyPress installation.
Open up your installation』s wp-config.php file.
Add
define ( 'BP_ROOT_BLOG', $blog_id );
to your wp-config.php file where $blog_id is the ID number of your chosen site and save the file.
Go to Dashboard → Network Admin.
Add BuddyPress through Plugins → Add New.
Navigate to the subsite which you identified in your wp-config.php file earlier
Activate BuddyPress. You will be redirected to the BuddyPress Welcome screen.
Configure BuddyPress.

F. BuddyPress MultiSite
This extends BuddyPress network instance from one site or subsite to multiple BuddyPress instances in a WordPress Multisite installation. This setup is complicated and would require BuddyPress/WordPress expertise plus server administration skills. You would need to install plugins to be able to configure this set up such as:
– BP Multi Network (WordPress plugin repository) or
– BuddyPress Multi Network (BuddyDev.com)
Please post in the respective forums if you would need assistance with either.

⇒ Next: Configure BuddyPress
or
⇒ Next: Configure BuddyPress for Network-wide set ups
⇐Previous: Getting Started