You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BuddyPress中创建自定义文章类型时添加自定义通知失败求助

Troubleshooting Your BuddyPress Custom Notifications for Project/Bid CPTs

Hey there! Let's walk through the most common issues that might be preventing your custom BuddyPress notifications from working with your project or bid custom post types (CPTs). I'll break down key checks based on the tutorial you referenced:

1. Double-Check Component Registration

First, make sure your custom component is properly registered and hooked. The custom_filter_notifications_get_registered_components function needs to be attached to the bp_notifications_get_registered_components filter correctly. Here's what a working implementation should look like:

function custom_filter_notifications_get_registered_components( $components ) {
    // Add your unique custom component slug (e.g., 'project_bids')
    $components[] = 'project_bids';
    return $components;
}
// Critical: Hook the filter so BuddyPress recognizes your component
add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );
  • Ensure your component slug is unique and matches exactly what you use in other parts of your notification code.
  • Avoid wrapping this hook in conditionals that might restrict it to admin pages only—BuddyPress needs this to run site-wide.

2. Validate Notification Trigger Logic

The next critical step is making sure you're triggering the notification at the right time (when a project/bid is created/updated). You need to hook into the correct CPT action and call bp_notifications_add_notification() with all required parameters.

For example, if you want to notify a project owner when a new bid is submitted:

function custom_send_bid_notification( $post_id, $post, $update ) {
    // Skip auto-drafts, updates, or non-published bids (adjust as needed)
    if ( $post->post_status !== 'publish' || $update ) {
        return;
    }
    // Confirm this is your bid CPT
    if ( 'bid' !== get_post_type( $post_id ) ) {
        return;
    }

    // Get the project author (notification recipient)
    $project_id = get_post_meta( $post_id, 'related_project_id', true ); // Use your actual meta key
    $recipient_id = get_post_field( 'post_author', $project_id );
    $sender_id = get_current_user_id();

    // Don't notify the user if they submitted the bid themselves
    if ( $recipient_id === $sender_id ) {
        return;
    }

    // Add the notification to BuddyPress
    bp_notifications_add_notification( array(
        'user_id'           => $recipient_id,
        'item_id'           => $post_id,
        'secondary_item_id' => $sender_id,
        'component_name'    => 'project_bids', // Must match your registered component
        'component_action'  => 'new_bid_submitted', // Unique action identifier
        'date_notified'     => bp_core_current_time(),
        'is_new'            => 1,
    ) );
}
// Hook into the bid CPT's publish action
add_action( 'publish_bid', 'custom_send_bid_notification', 10, 3 );
  • Confirm you're using the correct CPT action (e.g., publish_project, save_post_bid, or transition_post_status).
  • Double-check that component_name exactly matches the slug you registered earlier.
  • Ensure user_id is set to the intended recipient (not the current user unless that's your goal).

3. Confirm Notification Formatting

BuddyPress needs to know how to display your notification. You must hook a formatting function to the bp_notifications_get_notifications_for_user filter. Here's an example:

function custom_format_bid_notifications( $notifications, $user_id ) {
    // Loop through notifications to target your component/action
    foreach ( $notifications as $notification_id => $notification ) {
        if ( 'project_bids' === $notification->component_name && 'new_bid_submitted' === $notification->component_action ) {
            $bid_post = get_post( $notification->item_id );
            $sender = get_userdata( $notification->secondary_item_id );
            
            if ( $bid_post && $sender ) {
                // Build the notification text with a link to the bid
                $notification_link = get_permalink( $bid_post->ID );
                $notifications[ $notification_id ] = sprintf(
                    __( '%s submitted a new bid on your project. %sView Bid%s', 'your-text-domain' ),
                    $sender->display_name,
                    '<a href="' . esc_url( $notification_link ) . '">',
                    '</a>'
                );
            }
        }
    }
    return $notifications;
}
add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_bid_notifications', 10, 2 );
  • Make sure component_name and component_action match exactly what you used in bp_notifications_add_notification().
  • Verify that the permalink for your CPT is generated correctly (check for custom rewrite rules that might break links).

4. Debugging Steps to Pinpoint the Issue

If the above checks don't fix it, try these debugging tricks:

  • Enable WP_DEBUG in your wp-config.php to catch PHP errors:
    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true );
    
  • Check the wp_bp_notifications table in your database: If entries exist but aren't showing up, the issue is in the formatting function. If no entries exist, the trigger logic is broken.
  • Add error_log() statements in your trigger and formatting functions to trace execution:
    error_log( 'Bid notification trigger ran for post ID: ' . $post_id );
    
  • Ensure BuddyPress is fully activated, and users have notifications enabled in their profile settings.

内容的提问来源于stack exchange,提问作者Ranjit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:20:53