This tutorial demonstrates how to display custom meta fields for a business profile in your WordPress theme. It includes an example to hook into the bp_business_profile_after_category_section action to display meta fields like Business Type, Visibility, Tagline, Special Offers, and Established Date.
Code Implementation
Step 1: Add Custom Fields (If Not Already Added)
Ensure you have already added and saved custom fields to your business profile. Here’s an example of custom fields added in the Create and Manage screens:
// Add custom fields to the Create and Manage Screens
add_action( 'bp_business_create_after_field', 'bp_business_add_custom_fields' );
add_action( 'business_profile_after_general_settings', 'bp_business_add_custom_fields' );
function bp_business_add_custom_fields( $business_id = 0 ) {
// If $business_id is not provided or is 0, retrieve it dynamically
if ( ! $business_id ) {
$business_id = get_the_ID();
}
// Fetch saved meta values for the fields
$business_type = get_post_meta( $business_id, 'business_type', true );
$business_visibility = get_post_meta( $business_id, 'business_visibility', true );
$business_tagline = get_post_meta( $business_id, 'business_tagline', true );
$business_offers = get_post_meta( $business_id, 'business_offers', true );
$business_date = get_post_meta( $business_id, 'business_date', true );
?>
Step 2: Save Custom Fields
Ensure the custom fields are saved when creating or editing a business profile.
// Save custom fields on both create and manage screens
add_action( 'save_post_business', 'bp_business_save_custom_fields', 10, 3 );
function bp_business_save_custom_fields( $post_id, $post, $update ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( $post->post_type !== 'business' ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Fields and their sanitization
$fields = [
'business_type' => 'sanitize_text_field',
'business_visibility' => 'sanitize_text_field',
'business_tagline' => 'sanitize_text_field',
'business_offers' => 'sanitize_textarea_field',
'business_date' => 'sanitize_text_field',
];
foreach ( $fields as $key => $sanitizer ) {
if ( isset( $_POST[ $key ] ) ) {
$value = call_user_func( $sanitizer, $_POST[ $key ] );
update_post_meta( $post_id, $key, $value );
}
}
}
Step 3: Display Custom Fields in the Template
Hook into the bp_business_profile_after_category_section action to display the custom fields in the template.
// Display custom meta fields after the Category Section
add_action( 'bp_business_profile_after_category_section', 'bp_display_custom_meta_fields', 10, 1 );
function bp_display_custom_meta_fields( $business_id ) {
// Fetch custom meta values
$business_type = get_post_meta( $business_id, 'business_type', true );
$business_visibility = get_post_meta( $business_id, 'business_visibility', true );
$business_tagline = get_post_meta( $business_id, 'business_tagline', true );
$business_offers = get_post_meta( $business_id, 'business_offers', true );
$business_date = get_post_meta( $business_id, 'business_date', true );
// Display custom meta fields if available
?>
Example Output
When the meta fields are populated, the output may look like:
Styling
You can style the output with CSS by targeting the classes added to each section:
.bp-business-custom-fields-section {
margin-top: 20px;
padding: 10px;
background-color: #f9f9f9;
}
.bp-business-custom-field {
margin-bottom: 10px;
}
.bp-business-custom-field strong {
font-weight: bold;
color: #333;
}
This completes the tutorial to display custom meta fields in your business profile template.
