Show Q&A activity in the Buddypress activity stream?

Hi. Over on the WPMU Q&A site Mason and Scribu talk about a snippet that might help to show Q&A activity in the Buddypress activity stream: http://premium.wpmudev.org/blog/questions/do-qas-show-up-in-buddypress-activity-stream/#answer-53132

I've not been able to figure out where this code would go, can anyone point me in the right direction?

Alternatively, has anyone else actually achieved what I'm trying to do, and if so how did you do it?

Thanks.

    Philip John

    Hiya,

    I believe you should be able to post that code into your theme's functions.php file. Make sure to include the edits that Scribu suggested.

    Unfortunately Scribu is no longer working on this plugin but I'll ask Moha who is to look at adding this in a future release.

    Thanks!
    Phil

    mattbailey
    • 93
    • #1,193

    Thanks Phil, I'll give that a try and post here how I get on.

    One thing that's confusing me is the class in that snippet is called 'E2E_News_Feed', but there's no reference in any of the Buddypress or Q&A plugin code to this class. Do I need to 'call' that class somehow, or do anything to activate it or re-name it, or should it just work? Obviously I need to make those other edits mentioned by Scribu as well.

    My Buddypress/Q&A integration is coming along very nicely but this is the one thing that I'm struggling with and, arguably, the most important.

    It would definitely make a great feature to add in a future release of the Q&A plugin, although my site will be long finished by then (I hope) ;)

    Philip John

    The E2E stuff is exactly what needs changing in Scribu's suggested edits so you should complete those and you'll find it should work.

    Phil

    mattbailey
    • 93
    • #1,193

    Right, so here's the edited code. Simply drop it in your theme's 'functions.php' and voila, questions you ask should now appear in your Buddypress activity stream.

    However, if somebody answers one of your questions it doesn't show up in your activity stream. I'm going to see if I can figure that bit out, but if anyone has any pointers they would be most appreciated...

    function record_ask_question( $post_id, $post, $user_id = false ) {
    	global $bp, $wpdb;
    
    	$post_id = (int)$post_id;
    	$blog_id = (int)$wpdb->blogid;
    
    	if ( !$user_id )
    		$user_id = (int)$post->post_author;
    
    	/* This is to stop infinite loops with Donncha's sitewide tags plugin */
    	if ( (int)$bp->site_options['tags_blog_id'] == (int)$blog_id )
    		return false;
    
    	/* Don't record this if it's not a post */
    	//if ( $post->post_type != 'question' )
    	if ( 'question' != $post->post_type )
    		return false;
    
    	if ( 'publish' == $post->post_status && '' == $post->post_password ) {
    		if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
    			/* Record this in activity streams */
    			$post_permalink = get_permalink( $post_id );
    
    			$activity_action = sprintf( __( '%s asked a new question: %s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
    			$activity_content = $post->post_content;
    
    			bp_blogs_record_activity( array(
    				'user_id' => (int)$post->post_author,
    				'action' => apply_filters( 'bp_blogs_activity_new_post_action', $activity_action, &$post, $post_permalink ),
    				'content' => apply_filters( 'bp_blogs_activity_new_post_content', $activity_content, &$post, $post_permalink ),
    				'primary_link' => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink, $post_id ),
    				'type' => 'new_question',
    				'item_id' => $blog_id,
    				'secondary_item_id' => $post_id,
    				'recorded_time' => $post->post_date_gmt
    			));
    		}
    	} else
    		bp_blogs_remove_post( $post_id, $blog_id );
    
    	bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    
    	do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
    }
    add_action( 'save_post', 'record_ask_question', 10, 2 );
    Philip John

    Excellent, thanks for posting that! I'll drop you some rep points :)

    mattbailey
    • 93
    • #1,193

    Thanks very much :D

    I've done a bit more work and now the code adds a user's answers to their own stream as well as their questions. However, at the moment I haven't figured out how to associate answers with questions on the stream in general, i.e. if someone answers one of my questions it shows up in their stream, but not in mine.

    Here's my updated code (sorry for the length of it, would it be better put somewhere else in future?). As before, just copy and paste this into your 'functions.php' file.

    I've also had to create a new version of the 'the_answer_form' function so that when someone posts an answer the parent question title is also recorded in the activity stream (otherwise you get a blank title). You'll need to rename the 'the_answer_form' function to 'the_answer_form_new' in both 'edit-answer.php' and 'single-question.php' in order to use this new function though.

    <?php
    
    // Add Q&A activity to the Buddypress activity stream
    function record_ask_question( $post_id, $post, $user_id = false ) {
    	global $bp, $wpdb;
    
    	$post_id = (int)$post_id;
    	$blog_id = (int)$wpdb->blogid;
    
    	if ( !$user_id )
    		$user_id = (int)$post->post_author;
    
    	/* This is to stop infinite loops with Donncha's sitewide tags plugin */
    	if ( (int)$bp->site_options['tags_blog_id'] == (int)$blog_id )
    		return false;
    
    	/* Don't record this if it's not a post */
    	$check_post_type = $post->post_type;
    	if ( ( $check_post_type != 'question' ) && ( $check_post_type != 'answer' ) )
    		return false;
    
    	if ( 'publish' == $post->post_status && '' == $post->post_password ) {
    		if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
    			/* Record this in activity streams */
    			$post_permalink = get_permalink( $post_id );
    
    			// Get the title of the post
    			$the_post_title = $post->post_title;
    			if ( $the_post_title == '' ) {
    				// If the title is empty we're probably answering a question, so let's get the parent title instead
    				$the_post_title = get_the_title($post->post_parent);
    			}
    
    			$activity_action = sprintf( __( '%s Question activity: %s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $the_post_title . '</a>' );
    			$activity_content = $post->post_content;
    
    			bp_blogs_record_activity( array(
    				'user_id' => (int)$post->post_author,
    				'action' => apply_filters( 'bp_blogs_activity_new_post_action', $activity_action, &$post, $post_permalink ),
    				'content' => apply_filters( 'bp_blogs_activity_new_post_content', $activity_content, &$post, $post_permalink ),
    				'primary_link' => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink, $post_id ),
    				'type' => 'question_activity',
    				'item_id' => $blog_id,
    				'secondary_item_id' => $post_id,
    				'recorded_time' => $post->post_date_gmt
    			));
    		}
    	} else
    		bp_blogs_remove_post( $post_id, $blog_id );
    
    	bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    
    	do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
    }
    add_action( 'save_post', 'record_ask_question', 10, 2 );
    
    // New 'the_answer_form' function
    function the_answer_form_new() {
    	global $wp_query;
    
    	if ( is_qa_page( 'edit' ) ) {
    		$answer = $wp_query->posts[0];
    
    		if ( !current_user_can( 'edit_post', $answer->ID ) )
    			return;
    	} else {
    		$the_post_parent_title = get_the_title($post->post_parent);
    		$answer = (object) array(
    			'ID' => '',
    			'post_parent' => get_the_ID(),
    			'post_title' => $the_post_parent_title,
    			'post_content' => ''
    		);
    	}
    
    ?>
    <form id="answer-form" method="post" action="<?php echo qa_get_url( 'archive' ); ?>">
    	<?php wp_nonce_field( 'qa_answer' ); ?>
    
    	<input type="hidden" name="qa_action" value="edit_answer" />
    	<input type="hidden" name="question_id" value="<?php echo esc_attr( $answer->post_parent ); ?>" />
    	<input type="hidden" name="answer_id" value="<?php echo esc_attr( $answer->ID ); ?>" />
    	<input type="hidden" name="post_title" value="<?php echo esc_attr( $answer->post_title ); ?>" />
    
    	<p><textarea name="answer"><?php echo esc_textarea( $answer->post_content ); ?></textarea></p>
    
    	<?php the_qa_submit_button(); ?>
    </form>
    <?php
    }
    
    ?>
    Philip John

    sorry for the length of it, would it be better put somewhere else in future?

    You might want to try using pastebin.

    Phil

    deltakid
    • 63
    • #1,823

    Thanks a lot Matt. This seems to not work with bp 1.5 though. Really important feature imo.

Join 370109 happy members

Get access to our entire collection of epic plugins, astonishing themes & genius support

Become a member