Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Exclude post categories and make a user profile private

  • @creaturis

    Participant

    I have some categories that could contain adult content. I would like to exclude them so they won’t show up in the activity section. is this possible?

    also, is there a feature in 1.6 that makes a user profile completely private same as with groups? thanks in advance.

Viewing 9 replies - 1 through 9 (of 9 total)
  • @imath

    Moderator

    Hi @creaturis,

    I think the best solution is to avoid recording an activity if the post has one of the category you want to exclude from activity stream, in the functions.php file of your active theme, you can try this code :

    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 );

    In 1.6 you can set the visibility of profile fields to logged in users, friends or anyone. I haven’t seen the behavior you’re describing.

    @creaturis

    Participant

    thanks a lot. yup that was pretty much what i had in mind. a profile that only friends can see :).

    I tried the function. but doesn’t seem to work. Probably my fault. anyways I changed the 9 and 2 id to the categories I wanted removed from activity but they still show up. do I have to change something else in this code also?

    I really appreciate that you wrote and/or shared this code btw.

    @imath

    Moderator

    Ok.

    the id’s are to paste in this array $ids_to_exclude = array( 4, 1, 2... );

    do not touch this code :
    add_action( 'save_post', 'do_not_record_this_category', 9, 2 );

    9 means we’ll fire this hook with the priority of 9 as bp_blogs_record_post fires a bit later at priority 10. The 2 means how many vars we want to get from the hook ‘save_post’.

    @creaturis

    Participant

    I always make stupid mistakes like these. :D. I really should start reading “php for dummies” .

    works amazingly well now, and thank you for explaining the function the 9 and 2 has. you got great coding skills.

    @imath

    Moderator

    @creaturis you’re welcome & thanks :)

    @valuser

    Participant

    Could this be done for tags ?

    say something like array( 'taxonomy' => 'post_tag','field' => 'slug', 'terms' => 'nono' )

    Would really appreciate it if imath’s code in post 2 above could be adapted for tags ?

    @valuser

    Participant

    Tried this but it stopped everything getting to the activity stream

    function do_not_record_this_tag( $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;
    
    $post_tags = get_the_post_tag( $post_id );
    $in = false;
    
    if( count($post_tags) > 0 ) {
    
    foreach ( $post_tags as $post_tag ) {
    if( in_array( $post_tag->tag_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_tag', 9, 2 );

    any ideas ???

    @imath

    Moderator

    Hello @valuser

    Yes it can be done for tags. The trouble with your snippet is that you’re calling a function that doesn’t exist in WordPress : get_the_post_tag() !! So i adapted your function using this one https://codex.wordpress.org/Function_Reference/get_the_tags and instead of tag ids i think it’s more convenient to use tag slugs :

    function do_not_record_this_tag( $post_id, $post ){
    
    /* this is the array that contains the category to exclude */
    $tag_slugs_to_exclude = array( 'exclude' );
    
    if ( 'publish' != $post->post_status )
    return false;
    
    $post_tags = get_the_tags( $post_id );
    $in = false;
    
    if( !empty( $post_tags ) ) {
    
    foreach ( $post_tags as $post_tag ) {
    if( in_array( $post_tag->slug, $tag_slugs_to_exclude ) )
    $in = true;
    
    }
    
    /* if the post has at least one excluded tag, 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_tag', 9, 2 );

    if you prefer id then replace the tag slugs by ids in the array $tag_slugs_to_exclude and replace $post_tag->slug by $post_tag->term_id

    @valuser

    Participant

    Many Many thanks. Thrilled to bits.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Exclude post categories and make a user profile private’ is closed to new replies.
Skip to toolbar