Exclude Auto Blog posts from buddypress Activity Stream

Is there anyone that can help me with my buddypress, I am trying to stop the activity from two sites that use autoblog to feed the site and is showing in the buddypress activity, here is the forum but nobody is answering

https://premium.wpmudev.org/forums/topic/exclude-auto-blog-posts-in-activity-stream#post-527640

  • Vaughan

    hiya

    i'm not sure if this will work or not, it's from quite an oldish thread.

    add this to your themes functions.php

    you will also need to make sure that you set autoblog to import from those feeds into a a specific category, so we are essentially blocking that category only.

    function do_not_record_this_category( $post_id, $post ){
    
    /* this is the array that contains the category to exclude */
    $ids_to_exclude = array( 4 );
    
    if ( ‘publish’ != $post->post_status )
    return false;
    
    $categories = get_the_category( $post_id );
    $in = false;
    
    if( count($categories) > 0 ) {
    
    foreach ( $categories as $category ) {
    
    if( in_array( $category->cat_ID, $ids_to_exclude) )
    $in = true;
    
    }
    
    /* if the post has at least one excluded category, then we remove the BuddyPress hook that records an activity */
    if( $in )
    remove_action( ‘save_post’, ‘bp_blogs_record_post’, 10, 2 );
    
    }
    
    }
    
    add_action( ‘save_post’, ‘do_not_record_this_category’, 9, 2 );

    add your categories you want to block in this line $ids_to_exclude = array( 4 );

    hope this helps

  • Vaughan

    hiya

    yes, they are supposed to be the id's not the slug names.

    you can find the category ids by going to dashboard > posts > categories

    then hover over the category you want the id for, it will display the url. something like below

    http://domain.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=1&post_type=post

    the id number is the tag ID in this case it's 1

    hope this helps

  • DavidM

    Ah, so you're trying to use this across sites in a Multi-site setup? That's pretty complicated because the category ids won't be the same on the various sites.

    Perhaps Vaughn might know a simpler solution but I think you might actually need something custom developed for that.

    The Code Snippets does allow for Multi-site use, though I've never tested its functionality in Multi-site myself. I'm guessing you could possibly check for blog ids and specify distinct category ids on a per blog basis, but it's a bit much.

    I've not tested how the 'save_post' hook would work in this particular case, but I suspect the following, for example, might work.

    function do_not_record_this_category( $post_id, $post ){
    
    global $blog_id;
    
    // check for blog id
    if ($blog_id == 1) {
    
    /* this is the array that contains the category to exclude */
    $ids_to_exclude = array( 2 );
    
    if ( ‘publish’ != $post->post_status )
    return false;
    
    $categories = get_the_category( $post_id );
    $in = false;
    
    if( count($categories) > 0 ) {
    
    foreach ( $categories as $category ) {
    
    if( in_array( $category->cat_ID, $ids_to_exclude) )
    $in = true;
    
    }
    }
    
    if ($blog_id == 2) {
    
    /* this is the array that contains the category to exclude */
    $ids_to_exclude = array( 3 );
    
    if ( ‘publish’ != $post->post_status )
    return false;
    
    $categories = get_the_category( $post_id );
    $in = false;
    
    if( count($categories) > 0 ) {
    
    foreach ( $categories as $category ) {
    
    if( in_array( $category->cat_ID, $ids_to_exclude) )
    $in = true;
    
    }
    }
    
    /* if the post has at least one excluded category, then we remove the BuddyPress hook that records an activity */
    if( $in )
    remove_action( ‘save_post’, ‘bp_blogs_record_post’, 10, 2 );
    
    }
    
    }
    
    add_action( ‘save_post’, ‘do_not_record_this_category’, 9, 2 );

    It's tricky, really. I'll see if I can get around to running a test on a local setup, but I think a custom developed solution would be best, as this method (meaning the code I just posted) is really just a hack.

  • DavidM

    @WrSantiago, I didn't notice earlier but the previously pasted code had the wrong apostrophes in them, thus why they wouldn't work at all.

    I've adjusted that but also altered the whole function to take as input, a list of category slugs, separated by commas.

    However, in my current testing, it's only working on the main site, sub-site posts still show in the stream even when the excluded category slug is used. I'm trying to debug this currently, but at the very least, the snippet should work fine for your main site.

    <?php
    
    // code derivative of snippet by imath at:
    // http://buddypress.org/support/topic/possible-to-exclude-post-categories-in-activity-stream-and-make-a-user-profile-private-like-with-pri/
    
    function exclude_category_slugs_from_activity_stream( $post_id, $post ){
    
    	// add in any categories to exclue from activity stream, separated by commas, no spaces
    	$category_slugs_to_exclude = "testing,autoblog";
    
    	// create the array that contains the category ids to exclude
    	$ids_to_exclude = array();
    	// create new array by splitting category slugs by comma
    	$category_slug_array = split( ",", $category_slugs_to_exclude );
    	// iterate over category_slug_array
    	foreach ( $category_slug_array as $category ) {
    		// get the category id based on the slug
    		$idObj = get_category_by_slug( $category );
    		$id = $idObj->term_id;
    		// push the category id onto the exclude array
    		array_push ( $ids_to_exclude, $id );
    	}
    
    	// check post status to make sure it's published, exit if not
    	if ( 'publish' != $post->post_status )
    		return false;
    
    	// get the post's categories
    	$categories = get_the_category( $post_id );
    	$in = false;
    
    	// check if the post has any categories, do nothing if not
    	if( count($categories) > 0 ) {
    		// iterate over categories
    		foreach ( $categories as $category ) {
    			// check if any excluded category exists within post's categories
    			if( in_array( $category->cat_ID, $ids_to_exclude) )
    				$in = true;
    		}
    	}
    
    	/* if the post has any excluded category, remove BuddyPress hook that records an activity */
    	if( $in )
    	remove_action( 'save_post', 'bp_blogs_record_post', 10, 2 );
    
    }
    
    // add our function to save_post action hook
    add_action( 'save_post', 'exclude_category_slugs_from_activity_stream', 9, 2 );
    ?>
  • DavidM

    Okay, so the issue had something to do with network activation using the Code Snippets plugin. Best thing to do is not use Code Snippets. Before proceeding you should delete the snippet from that plugin then deactivate the plugin.

    And from there, you can just do the following:

    1. Create a new file in wp-content/mu-plugins/
    2. Name it: exclude-category-slugs-from-activity-stream.php
    3. Copy the code above and paste it in the file, then Save.

    Being in the /mu-plugins/ folder will automatically network activate it and it should just work from there.

    Works perfectly in my local tests, though I've not yet tried it with Autoblog posts. It should work fine though, assuming Autoblog posts use the 'save_post' hook.

    To be clear also, this method uses category slugs rather than ids. This accounts for differences in category ids across sites. Just use the same slugs for the categories you'd like to exclude from the stream and it should work great.

  • WrSantiago

    Hello @DavidM

    I have a little problem or better a relapse, the code above just stop working, I am having a dirty buddypress activity stream again.

    Just to remind you, I had two blogs using autoblog feeding massive amounts of entries going into buddy press activity stream.

    Any Ideas of what could make this happened?

    I always try to keep up with wordpress updates so this might be the case for this relapse. A plugin issue?

    Thank You
    W.

  • DavidM

    Hi @WrSantiago,

    The latest version of BuddyPress uses a different action hook, as noted here:
    http://buddypress.trac.wordpress.org/ticket/2546

    I believe the following should work though:

    <?php
    function exclude_category_slugs_from_activity_stream( $new_status, $old_status, $post ) {
    
    	// Only record published posts
    	if ( 'publish' !== $new_status ) {
    		return;
    	}
    
    	// Don't record edits (publish -> publish)
    	if ( 'publish' === $old_status ) {
    		return;
    	}
    
    	// add in any categories to exclue from activity stream, separated by commas, no spaces
    	$category_slugs_to_exclude = "testing,autoblog";
    
    	// create the array that contains the category ids to exclude
    	$ids_to_exclude = array();
    	// create new array by splitting category slugs by comma
    	$category_slug_array = split( ",", $category_slugs_to_exclude );
    	// iterate over category_slug_array
    	foreach ( $category_slug_array as $category ) {
    		// get the category id based on the slug
    		$idObj = get_category_by_slug( $category );
    		$id = $idObj->term_id;
    		// push the category id onto the exclude array
    		array_push ( $ids_to_exclude, $id );
    	}
    
    	// get the post's categories
    	$categories = get_the_category( $post->ID );
    	$in = false;
    
    	// check if the post has any categories, do nothing if not
    	if( count($categories) > 0 ) {
    		// iterate over categories
    		foreach ( $categories as $category ) {
    			// check if any excluded category exists within post's categories
    			if( in_array( $category->cat_ID, $ids_to_exclude) )
    				$in = true;
    		}
    	}
    
    	// Don't record posts from filtered categories
    	if( $in )
    		return;
    
    	return bp_blogs_record_post( $post->ID, $post );
    }
    
    add_action( 'plugins_loaded', function() {
    	remove_action( 'transition_post_status', 'bp_blogs_catch_published_post', 10, 2 );
    	add_action( 'transition_post_status', 'exclude_category_slugs_from_activity_stream', 10, 3 );
    });
    ?>

    It doesn't conditionally remove the action hook, but outright replaces it.

    Cheers,
    David

  • DavidM

    Hi @WrSantiago,

    That's odd! Seems to have an issue with the anonymous function. Let's change that then. Could you please replace line 51 onward with this?

    add_action( 'plugins_loaded', 'bp_blogs_catch_filtered_published_post' );
    
    function bp_blogs_catch_filtered_published_post() {
    	remove_action( 'transition_post_status', 'bp_blogs_catch_published_post', 10, 2 );
    	add_action( 'transition_post_status', 'exclude_category_slugs_from_activity_stream', 10, 3 );
    }
    ?>

    Thanks,
    David

  • DavidM

    Hi @WrSantiago,

    I'm really not sure what happened there but I suspect it might have to do with that being a mu-plugin.

    Could I get you try the following?

    1. Delete that file from your /mu-plugins/ folder.
    2. Create a new file in your /wp-content/plugins/ folder.
    3. Name that file bp-restrict-categories.php.
    4. Copy and paste the following into that file:

    EDIT: Updated code posted below

    5. Go to yoursites.com/wp-admin/network/plugins.php.
    6. Locate BP Restrict Categories from Activity Stream and Network Activate it.

    I've done some extensive testing with that and it works perfectly in my network. Should work just as well for you but we can consider other possible issues if not.

    Thanks,
    David

  • DavidM

    Hi @WrSantiago,

    Really sorry for the delay and confusion with this one. I actually tested out the previous code pretty thoroughly on my initial test site and it worked great. But I just tried it with a new test site and it didn't work there.

    Basically, it was trying into a hook that really wouldn't be optimal, though I've still no idea how it worked on my previous site.

    Here's an adjusted snippet that should work perfectly either as a mu-plugin or network activated plugin. I've not tested it with Single-sites but it should work there as well.

    <?php
    /*
    Plugin Name: BP Restrict Categories from Activity Stream
    Plugin URI: https://premium.wpmudev.org/forums/topic/exclude-auto-blog-posts-from-buddypress-activity-stream
    Description: Restrict posts from certain categories from being posted to BP Activity Stream
    Author: DavidM
    Version: 1.0
    Author URI: https://premium.wpmudev.org/
    */
    
    // code derivative of snippet by imath at:
    // http://buddypress.org/support/topic/possible-to-exclude-post-categories-in-activity-stream-and-make-a-user-profile-private-like-with-pri/
    
    function exclude_category_slugs_from_activity_stream( $new_status, $old_status, $post ) {
    
    	// Only record published posts
    	if ( 'publish' !== $new_status ) {
    		return;
    	}
    
    	// Don't record edits (publish -> publish)
    	if ( 'publish' === $old_status ) {
    		return;
    	}
    
    	// add in any categories to exclue from activity stream, separated by commas, no spaces
    	$category_slugs_to_exclude = "testing,autoblog";
    
    	// create the array that contains the category ids to exclude
    	$ids_to_exclude = array();
    	// create new array by splitting category slugs by comma
    	$category_slug_array = split( ",", $category_slugs_to_exclude );
    	// iterate over category_slug_array
    	foreach ( $category_slug_array as $category ) {
    		// get the category id based on the slug
    		$idObj = get_category_by_slug( $category );
    		$id = $idObj->term_id;
    		// push the category id onto the exclude array
    		array_push ( $ids_to_exclude, $id );
    	}
    
    	// get the post's categories
    	$categories = get_the_category( $post->ID );
    	$in = false;
    
    	// check if the post has any categories, do nothing if not
    	if( count($categories) > 0 ) {
    		// iterate over categories
    		foreach ( $categories as $category ) {
    			// check if any excluded category exists within post's categories
    			if( in_array( $category->cat_ID, $ids_to_exclude) )
    				$in = true;
    		}
    	}
    
    	// Don't record posts from filtered categories
    	if( $in )
    		return;
    
    	return bp_blogs_record_post( $post->ID, $post );
    }
    
    add_action( 'bp_init', 'bp_blogs_catch_filtered_published_post' );
    
    function bp_blogs_catch_filtered_published_post() {
    	if ( bp_is_active( 'blogs' ) ) {
    		remove_action( 'transition_post_status', 'bp_blogs_catch_published_post', 10 );
    		add_action( 'transition_post_status', 'exclude_category_slugs_from_activity_stream', 10, 3 );
    	}
    }
    ?>

    Could you let me know how that works for you?

    Thanks for your patience, I'm sure this'll sort it for ya! :slight_smile:

    Cheers,
    David

  • DavidM

    Hi @WrSantiago,

    Hmm, that's really strange. I actually tested that under numerous conditions. I'll be happy to look into it on your site.

    Could you send me a message here?
    https://premium.wpmudev.org/contact/

    You can select "I have another question", then for the subject include "ATTN David", and for the message a link to this thread along with any other details.

    Looking to get this sorted for you! :slight_smile:

    Cheers,
    David

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.