Skip to:
Content
Pages
Categories
Search
Top
Bottom

bp_notifications_add_notification create 404 issue


  • Antonio
    Participant

    @anth0ny167

    I created many custom notifications on my theme, such as notification on post comments, but after a post is commented and the notification is sent, the listing of posts and of other custom post types display a 404 page.

    This happens only on the listing page of posts, not on the single post pages.

    I really hope in an help.

    This is my code:

    add_action( 'bp_setup_globals', 'my_app_register_blog_notification' );
    
    /**
    * Register component
    */
    function my_app_register_blog_notification() {
    	// Register component manually into buddypress() singleton
    	buddypress()->my_app_blog = new stdClass;
    	// Add notification callback function
    	buddypress()->my_app_blog->notification_callback = 'my_app_blog_format_notifications';
    
    	// Now register components into active components array
    	buddypress()->active_components['my_app_blog'] = 1;
    }
    
    	
    
    /**
    * Format the notification content
    */
    function my_app_blog_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    
    	if ( ! ('my_app_blog_like' === $action || 'my_app_blog_comment' === $action)) {
    		return $action;
    	}
    
    	$post_title = get_the_title( $item_id );
    
    	if ('my_app_blog_comment' === $action) {
    		$custom_title = sprintf( esc_html__( 'New comment received', 'my_app' ), $post_title );
    		$custom_link  = get_permalink( $item_id );
    		if ( (int) $total_items > 1 ) {
    			$custom_text  = sprintf( esc_html__( 'You received "%1$s" new comments', 'my_app' ), $total_items );
    			$custom_link = bp_get_notifications_permalink();
    		} else {
    			$custom_text  = sprintf( esc_html__( 'Your post "%1$s" received a new comment', 'my_app' ), $post_title );
    		}
    
    	}
    
    	// WordPress Toolbar
    	if ( 'string' === $format ) {
    		$message = (!empty($custom_link)) ? '<a href="' . esc_url( $custom_link ) . '" title="' . esc_attr( $custom_title ) . '">' . esc_html( $custom_text ) . '</a>' : $custom_text;
    		$return = apply_filters( 'my_app_wiki_like_format', $message, $custom_text, $custom_link );
    
    		// Deprecated BuddyBar
    	} else {
    		$return = apply_filters( 'my_app_wiki_like_format', array(
    			'text' => $custom_text,
    			'link' => $custom_link
    		), $custom_link, (int) $total_items, $custom_text, $custom_title );
    	}
    
    	return $return;
    
    }	
    
    add_action('comment_post','my_app_comment_notification',10,2);
    
    /*
    * Send the notification after comment inserting
    */
    function my_app_comment_notification($comment_id, $comment_approved) {
    
    	if( 1 === $comment_approved ){
    		$comment_object = get_comment($comment_id);
    		$post_type = get_post_type($comment_object->comment_post_ID);
    		$current_user_id = get_current_user_id();
    
    		if($post_type == 'post') {
    			$post_object = get_post($comment_object->comment_post_ID);
    
    			if(my_app_is_notification_enabled_blog_comment()) {
    
    				bp_notifications_add_notification( array(
    					'user_id'           => $post_object->post_author,
    					'item_id'           => $comment_object->comment_post_ID,
    					'secondary_item_id' => $current_user_id,
    					'component_name'    => 'my_app_blog',
    					'component_action'  => 'my_app_blog_comment',
    					'date_notified'     => bp_core_current_time(),
    					'is_new'            => 1,
    				) );
    			}
    
    		}
    
    	}
    }
  • You must be logged in to reply to this topic.
Skip to toolbar