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()